Module: JSONAPI::Parser::DocumentParser
- Defined in:
- lib/easy/jsonapi/parser/document_parser.rb
Overview
TODO: Only parse valid object members, currenlty parses any included members. Document Parsing Logic
Class Method Summary collapse
-
.parse(req_body) ⇒ JSONAPI::Document
Parse the JSONAPI Request into objects.
-
.parse_attributes(attrs_hash) ⇒ JSONAPI::Document::Resource::Attributes
The parsed attributes.
-
.parse_error(err_hash) ⇒ JSONAPI::Document::Error
The parsed error object.
-
.parse_errors(errs_arr) ⇒ Array<JSONAPI::Document::Error>
The parsed error objects.
-
.parse_hash(document_hash) ⇒ Object
Parse the JSONAPI Request into objects.
-
.parse_included(included_arr) ⇒ Array<JSONAPI::Document::Resource] The array of parsed included resources
Array<JSONAPI::Document::Resource] The array of parsed included resources.
-
.parse_links(links_hash) ⇒ JSONAPI::Document::Links
The parsed links object.
-
.parse_meta(meta_hash) ⇒ JSONAPI::Document::Meta
The parsed meta object.
-
.parse_relationship(name, rel_hash) ⇒ JSONAPI::Document::Resource::Relationships::Relationship
The parsed relationship.
-
.parse_relationships(rels_hash) ⇒ JSONAPI::Document::Resource::Relationships
The parsed relationships.
-
.parse_resource(res) ⇒ JSONAPI::Document::Resource
The parsed resource.
-
.parse_resource_identifier(res_id) ⇒ JSONAPI::Document::ResourceId
The parsed resource identifier.
-
.parse_resource_identifiers(res_id_arr) ⇒ JSONAPI::Document::ResourceId | Array<JSONAPI::Document::ResourceId] The parsed resource identifier or array or resource identifiers
JSONAPI::Document::ResourceId | Array<JSONAPI::Document::ResourceId] The parsed resource identifier or array or resource identifiers.
-
.parse_resources(res_arr) ⇒ JSONAPI::Document::Resource | Array<JSONAPI::Document::Resource>
A resource or collection of resources.
-
.parse_top_level_members(document) ⇒ Hash
A hash containing the objects needed to initialize a JSONAPI::Document.
Class Method Details
.parse(req_body) ⇒ JSONAPI::Document
Parse the JSONAPI Request into objects.
17 18 19 20 21 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 17 def self.parse(req_body) return if req_body.nil? document_hash = JSONAPI::Parser::JSONParser.parse(req_body) # parse json string into hash parse_hash(document_hash) end |
.parse_attributes(attrs_hash) ⇒ JSONAPI::Document::Resource::Attributes
Returns The parsed attributes.
83 84 85 86 87 88 89 90 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 83 def self.parse_attributes(attrs_hash) attributes = JSONAPI::Document::Resource::Attributes.new attrs_hash.each do |name, value| cur_attr = JSONAPI::Document::Resource::Attributes::Attribute.new(name, value) attributes.add(cur_attr) end attributes end |
.parse_error(err_hash) ⇒ JSONAPI::Document::Error
Returns The parsed error object.
186 187 188 189 190 191 192 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 186 def self.parse_error(err_hash) error = JSONAPI::Document::Error.new err_hash.each do |name, value| error.add(JSONAPI::Document::Error::ErrorMember.new(name, value)) end error end |
.parse_errors(errs_arr) ⇒ Array<JSONAPI::Document::Error>
Returns The parsed error objects.
176 177 178 179 180 181 182 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 176 def self.parse_errors(errs_arr) errs_hash_arr = [] errs_arr.each do |err_hash| errs_hash_arr << parse_error(err_hash) end errs_hash_arr end |
.parse_hash(document_hash) ⇒ Object
Parse the JSONAPI Request into objects.
27 28 29 30 31 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 27 def self.parse_hash(document_hash) return if document_hash.nil? doc_members_hash = parse_top_level_members(document_hash) JSONAPI::Document.new(doc_members_hash) end |
.parse_included(included_arr) ⇒ Array<JSONAPI::Document::Resource] The array of parsed included resources
Returns Array<JSONAPI::Document::Resource] The array of parsed included resources.
166 167 168 169 170 171 172 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 166 def self.parse_included(included_arr) res_arr = [] included_arr.each do |res| res_arr << parse_resource(res) end res_arr end |
.parse_links(links_hash) ⇒ JSONAPI::Document::Links
Returns The parsed links object.
119 120 121 122 123 124 125 126 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 119 def self.parse_links(links_hash) links = JSONAPI::Document::Links.new links_hash.each do |name, value| cur_link = JSONAPI::Document::Links::Link.new(name, value) links.add(cur_link) end links end |
.parse_meta(meta_hash) ⇒ JSONAPI::Document::Meta
Returns The parsed meta object.
130 131 132 133 134 135 136 137 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 130 def self.() = JSONAPI::Document::Meta.new .each do |name, value| = JSONAPI::Document::Meta::MetaMember.new(name, value) .add() end end |
.parse_relationship(name, rel_hash) ⇒ JSONAPI::Document::Resource::Relationships::Relationship
Returns The parsed relationship.
108 109 110 111 112 113 114 115 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 108 def self.parse_relationship(name, rel_hash) links = parse_links(rel_hash[:links]) if rel_hash[:links] data = parse_resource_identifiers(rel_hash[:data]) if rel_hash[:data] = (rel_hash[:meta]) if rel_hash[:meta] rel_members_hash = { name: name, links: links, data: data, meta: } JSONAPI::Document::Resource::Relationships::Relationship.new(rel_members_hash) end |
.parse_relationships(rels_hash) ⇒ JSONAPI::Document::Resource::Relationships
Returns The parsed relationships.
95 96 97 98 99 100 101 102 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 95 def self.parse_relationships(rels_hash) relationships = JSONAPI::Document::Resource::Relationships.new rels_hash.each do |name, value| rel = parse_relationship(name, value) relationships.add(rel) end relationships end |
.parse_resource(res) ⇒ JSONAPI::Document::Resource
Returns The parsed resource.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 63 def self.parse_resource(res) attributes = parse_attributes(res[:attributes]) if res[:attributes] relationships = parse_relationships(res[:relationships]) if res[:relationships] links = parse_links(res[:links]) if res[:links] = (res[:meta]) if res[:meta] res_members_hash = { type: res[:type], id: res[:id], attributes: attributes, relationships: relationships, links: links, meta: } JSONAPI::Document::Resource.new(res_members_hash) end |
.parse_resource_identifier(res_id) ⇒ JSONAPI::Document::ResourceId
Returns The parsed resource identifier.
160 161 162 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 160 def self.parse_resource_identifier(res_id) JSONAPI::Document::ResourceId.new(type: res_id[:type], id: res_id[:id]) end |
.parse_resource_identifiers(res_id_arr) ⇒ JSONAPI::Document::ResourceId | Array<JSONAPI::Document::ResourceId] The parsed resource identifier or array or resource identifiers
Returns JSONAPI::Document::ResourceId | Array<JSONAPI::Document::ResourceId] The parsed resource identifier or array or resource identifiers.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 143 def self.parse_resource_identifiers(res_id_arr) res_id_hashs = [] case res_id_arr when Array res_id_arr.each do |res_id| res_id_hashs << parse_resource_identifier(res_id) end res_id_hashs when Hash parse_resource_identifier(res_id_arr) else raise 'Data member of resource relationship was not an array or hash' end end |
.parse_resources(res_arr) ⇒ JSONAPI::Document::Resource | Array<JSONAPI::Document::Resource>
Returns A resource or collection of resources.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 50 def self.parse_resources(res_arr) case res_arr when Array res_arr.map { |res| parse_resource(res) } when Hash parse_resource(res_arr) else raise 'The top level data member must be an array of resources or a resource' end end |
.parse_top_level_members(document) ⇒ Hash
Returns A hash containing the objects needed to initialize a JSONAPI::Document.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 36 def self.parse_top_level_members(document) doc_members_hash = {} doc_members_hash[:data] = parse_resources(document[:data]) if document.key?(:data) doc_members_hash[:meta] = (document[:meta]) if document.key?(:meta) doc_members_hash[:links] = parse_links(document[:links]) if document.key?(:links) doc_members_hash[:included] = parse_included(document[:included]) if document.key?(:included) doc_members_hash[:errors] = parse_errors(document[:errors]) if document.key?(:errors) doc_members_hash[:jsonapi] = parse_jsonapi(document[:jsonapi]) if document.key?(:jsonapi) doc_members_hash end |