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

Class Method Details

.parse(req_body) ⇒ JSONAPI::Document

Parse the JSONAPI Request into objects.

Parameters:

  • req_body (String)

    The supplied JSONAPI document with POST, PATCH, PUT, or DELETE.

Returns:

Raises:

  • (JSONAPI::Parser::InvalidDocument)

    if document is invalid.



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.

Parameters:

  • attrs_hash (Hash)

    The attributes hash to parse

Returns:



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.

Parameters:

  • err_hash (Hash)

    The error hash to parse

Returns:



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.

Parameters:

  • errs_arr (Array<Hash>)

    The array of errors to parse

Returns:



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.

Parameters:

  • document_hash (Hash)

    The jsonapi-like ruby hash to parse 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.

Parameters:

  • included_arr (Array<Hash>)

    The array of included resoures to parse

Returns:

  • (Array<JSONAPI::Document::Resource] The array of parsed included resources)

    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

Returns The parsed links object.

Parameters:

  • links_hash (Hash)

    The links hash to parse

Returns:



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.

Parameters:

  • meta_hash (Hash)

    The meta hash to parse

Returns:



130
131
132
133
134
135
136
137
# File 'lib/easy/jsonapi/parser/document_parser.rb', line 130

def self.parse_meta(meta_hash)
  meta = JSONAPI::Document::Meta.new
  meta_hash.each do |name, value|
    cur_meta_member = JSONAPI::Document::Meta::MetaMember.new(name, value)
    meta.add(cur_meta_member)
  end
  meta
end

.parse_relationship(name, rel_hash) ⇒ JSONAPI::Document::Resource::Relationships::Relationship

Returns The parsed relationship.

Parameters:

  • name (String | Symbol)

    The name of the relationship being parsed

  • rel_hash (Hash)

    The relationship to parse

Returns:



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]
  meta = parse_meta(rel_hash[:meta]) if rel_hash[:meta]
  
  rel_members_hash = { name: name, links: links, data: data, meta: meta }
  JSONAPI::Document::Resource::Relationships::Relationship.new(rel_members_hash)
end

.parse_relationships(rels_hash) ⇒ JSONAPI::Document::Resource::Relationships

Returns The parsed relationships.

Parameters:

  • rels_hash (Hash)

    The relationships hash to parse

Returns:



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.

Parameters:

  • res (Hash)

    The resource hash to parse

Returns:



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]
  meta = parse_meta(res[:meta]) if res[:meta]

  res_members_hash = {
    type: res[:type],
    id: res[:id],
    attributes: attributes,
    relationships: relationships,
    links: links,
    meta: meta
  }

  JSONAPI::Document::Resource.new(res_members_hash)
end

.parse_resource_identifier(res_id) ⇒ JSONAPI::Document::ResourceId

Returns The parsed resource identifier.

Parameters:

  • res_id (Hash)

    The resource identifier to parse

Returns:



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.

Parameters:

  • res_id_arr (Hash | Array<Hash>)

    The resource identifier or array of resource identifiers to parse

Returns:



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.

Parameters:

  • res_arr (Array<Hash> | Hash)

    A collection of resources or a resource.

Returns:



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.

Returns:

  • (Hash)

    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] = parse_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