Class: JSONAPI::Consumer::Parsers::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/consumer/parsers/parser.rb

Class Method Summary collapse

Class Method Details

.parameters_from_resource(params) ⇒ Object

Given a resource hash, returns a Resource.new friendly hash which flattens the attributes in w/ id and type.

Example:

Given: { id: 1. type: 'person', attributes: { first_name: 'Jeff', last_name: 'Ching' }, links: ..., relationships: ... }

Returns: { id: 1, type: 'person', first_name: 'Jeff', last_name: 'Ching' links: ..., relationships: ... }



51
52
53
54
# File 'lib/jsonapi/consumer/parsers/parser.rb', line 51

def parameters_from_resource(params)
  attrs = params.slice('id', 'links', 'meta', 'type', 'relationships')
  attrs.merge(params.fetch('attributes', {}))
end

.parse(klass, response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jsonapi/consumer/parsers/parser.rb', line 5

def parse(klass, response)
  data = response.body.present? ? response.body : {}

  ResultSet.new.tap do |result_set|
    result_set.record_class = klass
    result_set.uri = response.env[:url]
    handle_json_api(result_set, data)
    handle_data(result_set, data)
    handle_errors(result_set, data)
    handle_meta(result_set, data)
    handle_links(result_set, data)
    handle_relationships(result_set, data)
    handle_pagination(result_set, data)
    handle_included(result_set, data)
  end
end