Sha256: a65a551f5654fa007d96fbc4902f41d016d6c55bd6733472d300dfb728833de3

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'real_savvy/meta'
require 'real_savvy/resource'

class RealSavvy::Document

  attr_reader :document

  def initialize(document, status: nil)
    @document = document
    @status = status
  end

  def data
    @data ||= self.class.process_resources(document['data'], self)
  end

  alias results data

  def result
    data
  end

  def included
    @included ||= self.class.process_resources(document['included'], self)
  end

  def meta
    @meta ||= RealSavvy::Meta.new(document['meta'] || {})
  end

  def errors
    @errors ||= document['errors'] || {}
  end

  def status
    @status
  end

  def objects_lookup
    @objects_lookup ||= [*data, *included].compact.each_with_object({}) { |resource, result| result[resource.hash] = resource }
  end

  def inspect
    document.inspect
  end

  def self.process_resources(resources, document=nil)
    if resources.is_a?(Array)
      resources.map { |object| RealSavvy::Resource.new(object, document)  }
    elsif resources
      RealSavvy::Resource.new(resources, document)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
real_savvy-0.0.12 lib/real_savvy/document.rb