Sha256: c7f85744ed12d191a49717b68972f72dd9fbc5a67a761a3ad01108b8ccab7172

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

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

class RealSavvy::Document

  attr_reader :document

  TYPE_TO_RESOURCE = {
    'properties' => RealSavvy::Resource::Property,
    'listings' => RealSavvy::Resource::Property,
    'collections' => RealSavvy::Resource::Collection,
    'saved_searches' => RealSavvy::Resource::SavedSearch,
    'users' => RealSavvy::Resource::User,
  }.tap do |lookup|
    lookup.default = RealSavvy::Resource::Base
  end.freeze

  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[0]
  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).each_with_object({}) { |resource, result| result[resource.hash] = resource }
  end

  def inspect
    document.inspect
  end

  def self.process_resources(resources, document=nil)
    ::RealSavvy.safe_wrap(resources).map { |object| TYPE_TO_RESOURCE[object['type']].new(object, document)  }
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
real_savvy-0.0.7 lib/real_savvy/document.rb
real_savvy-0.0.6 lib/real_savvy/document.rb
real_savvy-0.0.5 lib/real_savvy/document.rb
real_savvy-0.0.4 lib/real_savvy/document.rb
real_savvy-0.0.3 lib/real_savvy/document.rb