class Lurch::PayloadBuilder

Public Class Methods

new(inflector) click to toggle source
# File lib/lurch/payload_builder.rb, line 3
def initialize(inflector)
  @inflector = inflector
end

Public Instance Methods

build(input, identifier_only = false) click to toggle source
# File lib/lurch/payload_builder.rb, line 7
def build(input, identifier_only = false)
  { "data" => data(input, identifier_only) }
end

Private Instance Methods

attributes_for(resource, identifier_only) click to toggle source
# File lib/lurch/payload_builder.rb, line 31
def attributes_for(resource, identifier_only)
  return {} if identifier_only
  @inflector.encode_keys(resource.attributes)
end
data(input, identifier_only) click to toggle source
# File lib/lurch/payload_builder.rb, line 13
def data(input, identifier_only)
  if input.is_a?(Enumerable)
    input.map { |resource| resource_object_for(resource, identifier_only) }
  else
    resource_object_for(input, identifier_only)
  end
end
relationships_for(resource, identifier_only) click to toggle source
# File lib/lurch/payload_builder.rb, line 36
def relationships_for(resource, identifier_only)
  return {} if identifier_only
  @inflector.encode_keys(resource.relationships) do |value|
    PayloadBuilder.new(@inflector).build(value, true)
  end
end
resource_object_for(resource, identifier_only) click to toggle source
# File lib/lurch/payload_builder.rb, line 21
def resource_object_for(resource, identifier_only)
  return nil if resource.nil?
  {
    "id" => resource.id,
    "type" => @inflector.encode_type(resource.type),
    "attributes" => attributes_for(resource, identifier_only),
    "relationships" => relationships_for(resource, identifier_only)
  }.reject { |_, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
end