Class: JsonAPIObjectMapper::Parser::Document
- Inherits:
-
Object
- Object
- JsonAPIObjectMapper::Parser::Document
- Includes:
- Errors
- Defined in:
- lib/jsonapi-object-mapper/parser/document.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
Attributes included from Errors
Instance Method Summary collapse
- #contains_data_array? ⇒ Boolean
- #deserialize_errors! ⇒ Object
- #deserialize_links(links) ⇒ Object
- #document_data ⇒ Object
-
#initialize(document) ⇒ Document
constructor
A new instance of Document.
Methods included from Errors
Constructor Details
#initialize(document) ⇒ Document
Returns a new instance of Document.
13 14 15 16 17 18 19 20 |
# File 'lib/jsonapi-object-mapper/parser/document.rb', line 13 def initialize(document) parsed_document = (document.is_a?(String) ? ::Oj.load(document) : document) @includes = IncludedResources.load(parsed_document.delete("included")) @links = deserialize_links(parsed_document.delete("links")).freeze @document = parsed_document.freeze @errors = deserialize_errors!.freeze freeze end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
11 12 13 |
# File 'lib/jsonapi-object-mapper/parser/document.rb', line 11 def document @document end |
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
11 12 13 |
# File 'lib/jsonapi-object-mapper/parser/document.rb', line 11 def includes @includes end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
11 12 13 |
# File 'lib/jsonapi-object-mapper/parser/document.rb', line 11 def links @links end |
Instance Method Details
#contains_data_array? ⇒ Boolean
26 27 28 |
# File 'lib/jsonapi-object-mapper/parser/document.rb', line 26 def contains_data_array? document_data.is_a?(Array) end |
#deserialize_errors! ⇒ Object
30 31 32 33 |
# File 'lib/jsonapi-object-mapper/parser/document.rb', line 30 def deserialize_errors! return [] unless @document.key?("errors") Set.new(@document["errors"]) { |error| OpenStruct.new(error) } end |
#deserialize_links(links) ⇒ Object
35 36 37 38 39 |
# File 'lib/jsonapi-object-mapper/parser/document.rb', line 35 def deserialize_links(links) links&.each_with_object(OpenStruct.new) do |(key, value), struct| struct[key.to_s.tr("-", "_")] = value end end |
#document_data ⇒ Object
22 23 24 |
# File 'lib/jsonapi-object-mapper/parser/document.rb', line 22 def document_data @document["data"] || @document end |