Class: JSONAPI::Ruby::Deserializer::Document
- Inherits:
-
Object
- Object
- JSONAPI::Ruby::Deserializer::Document
- Defined in:
- lib/jsonapi-ruby-deserializer/document.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#included ⇒ Object
Returns the value of attribute included.
-
#index ⇒ Object
Returns the value of attribute index.
-
#jsonapi ⇒ Object
Returns the value of attribute jsonapi.
-
#links ⇒ Object
Returns the value of attribute links.
-
#meta ⇒ Object
Returns the value of attribute meta.
Instance Method Summary collapse
- #create_index! ⇒ Object
- #fetch_relation(data) ⇒ Object
-
#initialize(document, link_data: true) ⇒ Document
constructor
A new instance of Document.
- #link_data! ⇒ Object
- #parse_errors!(data) ⇒ Object
- #parse_jsonapi!(data) ⇒ Object
- #parse_links!(data) ⇒ Object
- #parse_meta!(data) ⇒ Object
- #parse_resource!(data) ⇒ Object
Constructor Details
#initialize(document, link_data: true) ⇒ Document
Returns a new instance of Document.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 9 def initialize(document, link_data: true) @jsonapi = parse_jsonapi!(document['jsonapi']) @meta = (document['meta']) @links = parse_links!(document['links']) @data = parse_resource!(document['data']) @included = parse_resource!(document['included']) @index = create_index! @errors = parse_errors!(document['errors']) link_data! if link_data end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
7 8 9 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7 def data @data end |
#errors ⇒ Object
Returns the value of attribute errors.
7 8 9 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7 def errors @errors end |
#included ⇒ Object
Returns the value of attribute included.
7 8 9 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7 def included @included end |
#index ⇒ Object
Returns the value of attribute index.
7 8 9 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7 def index @index end |
#jsonapi ⇒ Object
Returns the value of attribute jsonapi.
7 8 9 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7 def jsonapi @jsonapi end |
#links ⇒ Object
Returns the value of attribute links.
7 8 9 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7 def links @links end |
#meta ⇒ Object
Returns the value of attribute meta.
7 8 9 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 7 def @meta end |
Instance Method Details
#create_index! ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 44 def create_index! return if @included.nil? || @included.empty? {}.tap do |h| @included.each do |resource| resource_identifier = [resource.type, resource.id] h[resource_identifier] = resource end end end |
#fetch_relation(data) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 73 def fetch_relation(data) if data.kind_of?(Array) data.map { |element| index[[element.type, element.id]] || element } else index[[data.type, data.id]] || data end end |
#link_data! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 61 def link_data! return if @included.nil? || @included.empty? (Array(@data) + @included).each do |resource| next if resource.relationships.to_a.empty? resource.relationships.to_a.each do |relation| resource.relationships.send(relation.to_sym).data = fetch_relation(resource.relationships.send(relation.to_sym).data) end end end |
#parse_errors!(data) ⇒ Object
55 56 57 58 59 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 55 def parse_errors!(data) return if data.nil? || data.empty? data.kind_of?(Array) ? data.map! { |h| Errors.new(h) } : Errors.new(data) end |
#parse_jsonapi!(data) ⇒ Object
38 39 40 41 42 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 38 def parse_jsonapi!(data) return if data.nil? || data.empty? Jsonapi.new(data) end |
#parse_links!(data) ⇒ Object
26 27 28 29 30 |
# File 'lib/jsonapi-ruby-deserializer/document.rb', line 26 def parse_links!(data) return if data.nil? || data.empty? Links.new(data) end |