class JSONAPI::Record::Metal

Public Class Methods

collection_uri() click to toggle source

@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
fetch(uri, query = {}) click to toggle source

@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
fetch_collection(uri, query = {}) click to toggle source

@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
fetch_resource(uri, query = {}) click to toggle source

@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
individual_uri(id) click to toggle source

@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
parse(document) click to toggle source

@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
relationship_to_many(name, class_name:) click to toggle source

@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
relationship_to_one(name, class_name:) click to toggle source

@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
resource_attribute_names() click to toggle source

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

collection_path() click to toggle source

@return [String]

# File lib/jsonapi/record/metal.rb, line 118
def collection_path
  type.to_s
end
define_fetch_relationship_method(name, klass, fetch_method) click to toggle source
# 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
individual_path(id) click to toggle source

@param id [String] @return [String]

# File lib/jsonapi/record/metal.rb, line 124
def individual_path(id)
  "#{collection_path}/#{id}"
end
raise_exception_when_errors() { |block| ... } click to toggle source
# 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
store_relationship(name) click to toggle source

@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

collection_uri() click to toggle source

@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
data() click to toggle source
# File lib/jsonapi/record/metal.rb, line 209
def data
  identifier
    .merge(attributes: payload_attributes)
    .merge(relationships: payload_relationships)
    .compact
end
identifier() click to toggle source

@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
individual_uri() click to toggle source

@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
payload_attributes() click to toggle source
# File lib/jsonapi/record/metal.rb, line 216
def payload_attributes
  resource_attributes if resource_attributes.any?
end
payload_relationships() click to toggle source
# 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
persisted?() click to toggle source
# File lib/jsonapi/record/metal.rb, line 183
def persisted?
  persisted
end
relationships_or_default() click to toggle source
# File lib/jsonapi/record/metal.rb, line 227
def relationships_or_default
  relationships || {}
end
resource_attributes() click to toggle source

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
to_payload() click to toggle source

@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
to_relationship() click to toggle source

@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