class JSONAPI::Record::Metal
Public Class Methods
@example
User.collection_uri #=> "https://api.example.com/users"
# File lib/jsonapi/record/metal.rb, line 40 def collection_uri URI.join(base_uri, collection_path).to_s end
@param uri [String] @param query [Hash] @return [Hash]
# File lib/jsonapi/record/metal.rb, line 82 def fetch(uri, query = {}) parse(JSONAPI::SimpleClient.fetch(uri, default_headers, query)) end
@param uri [String] @param query [Hash] @return [Array<JSONAPI::Record::Metal>]
# File lib/jsonapi/record/metal.rb, line 96 def fetch_collection(uri, query = {}) fetch(uri, query).map { |attributes| new(attributes) } end
@param uri [String] @param query [Hash] @return [JSONAPI::Record::Metal]
# File lib/jsonapi/record/metal.rb, line 89 def fetch_resource(uri, query = {}) new(fetch(uri, query)) end
@param id [String] @return [String] @example
User.individual_uri("1") #=> "https://api.example.com/users/1"
# File lib/jsonapi/record/metal.rb, line 48 def individual_uri(id) URI.join(base_uri, individual_path(id)).to_s end
@param document [JSONAPI::Types::Info, JSONAPI::Types::Success, JSONAPI::Types::Failure] @return [Hash]
# File lib/jsonapi/record/metal.rb, line 102 def parse(document) JSONAPI::Record::Parser.parse(document) end
@param name [String] the name of the relationship pluralized. @param class_name [String] the class of the relationship as a string.
# File lib/jsonapi/record/metal.rb, line 72 def relationship_to_many(name, class_name:) klass = Object.const_get(class_name) attribute(name, Types::Array(klass).default([].freeze)) define_fetch_relationship_method(name, klass, :fetch_related_collection) store_relationship(name) end
@param name [String] the name of the relationship singularized. @param class_name [String] the class of the relationship as a string.
# File lib/jsonapi/record/metal.rb, line 63 def relationship_to_one(name, class_name:) klass = Object.const_get(class_name) attribute(name, Types::Constructor(klass)) define_fetch_relationship_method(name, klass, :fetch_related_resource) store_relationship(name) end
Returns the names of the resource attributes. @return [Array<Symbol>]
# File lib/jsonapi/record/metal.rb, line 108 def resource_attribute_names attribute_names.reject do |attribute| (JSONAPI::Record::Base.attribute_names + relationship_names) .include?(attribute) end end
Private Class Methods
@return [String]
# File lib/jsonapi/record/metal.rb, line 118 def collection_path type.to_s end
# File lib/jsonapi/record/metal.rb, line 141 def define_fetch_relationship_method(name, klass, fetch_method) define_method("fetch_#{name}") do |query = {}| send(fetch_method, name, klass, query) end end
@param id [String] @return [String]
# File lib/jsonapi/record/metal.rb, line 124 def individual_path(id) "#{collection_path}/#{id}" end
# File lib/jsonapi/record/metal.rb, line 147 def raise_exception_when_errors(&block) yield(block).tap do |resource| if (errors = resource.response_errors) raise JSONAPI::SimpleClient::UnprocessableEntity, errors end end end
@param name [String] @return [Array<Symbol>]
# File lib/jsonapi/record/metal.rb, line 137 def store_relationship(name) relationship_names(relationship_names + [name]) end
Public Instance Methods
@return [String] @example
User.new.collection_uri #=> "https://api.example.com/users"
# File lib/jsonapi/record/metal.rb, line 159 def collection_uri self.class.collection_uri end
# File lib/jsonapi/record/metal.rb, line 209 def data identifier .merge(attributes: payload_attributes) .merge(relationships: payload_relationships) .compact end
@return [Hash] @example
User.new(id: "1").identifier #=> "{ id: "1", type: "users" }"
# File lib/jsonapi/record/metal.rb, line 197 def identifier { id: id, type: self.class.type } end
@return [String] @example
User.new(id: "1").individual_uri #=> "https://api.example.com/users/1"
# File lib/jsonapi/record/metal.rb, line 166 def individual_uri self.class.individual_uri(id) end
# File lib/jsonapi/record/metal.rb, line 216 def payload_attributes resource_attributes if resource_attributes.any? end
# File lib/jsonapi/record/metal.rb, line 220 def payload_relationships relationships_or_default .transform_values { |relationship| relationship.to_hash.slice(:data) } .select { |_key, value| value.any? } .yield_self { |results| results if results.any? } end
# File lib/jsonapi/record/metal.rb, line 183 def persisted? persisted end
# File lib/jsonapi/record/metal.rb, line 227 def relationships_or_default relationships || {} end
Returns the resource attributes. @return [Hash]
# File lib/jsonapi/record/metal.rb, line 179 def resource_attributes attributes.slice(*self.class.resource_attribute_names) end
@return [Hash] @example
User.new(id: "1", name: "Rick").to_payload #=> "{ data: { id: "1", type: "users", attributes: { name: "Rick" } } }"
# File lib/jsonapi/record/metal.rb, line 205 def to_payload { data: data } end
@return [Hash] @example
User.new(id: "1").to_relationship #=> "{ data: { id: "1", type: "users" } }"
# File lib/jsonapi/record/metal.rb, line 190 def to_relationship { data: identifier } end