Sha256: 10fc0ac7182e2bc9ceb14bdd65e829b401f3fe2127c07c6796c13dd461748f58

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 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).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

3 entries across 3 versions & 1 rubygems

Version Path
real_savvy-0.0.10 lib/real_savvy/document.rb
real_savvy-0.0.9 lib/real_savvy/document.rb
real_savvy-0.0.8 lib/real_savvy/document.rb