Module: Clicksign::JsonApi::Parser

Defined in:
lib/clicksign/json_api/parser.rb

Class Method Summary collapse

Class Method Details

.build(item) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/clicksign/json_api/parser.rb', line 21

def self.build(item)
  {
    'id' => item['id'],
    'type' => item['type'],
    'attributes' => item.fetch('attributes', {}),
    'relationships' => item.fetch('relationships', {}),
  }
end

.parse(raw) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/clicksign/json_api/parser.rb', line 6

def self.parse(raw)
  raw_data = raw['data']
  data = case raw_data
         when Array then raw_data.map { |item| build(item) }
         when Hash  then [build(raw_data)]
         else            []
         end

  included = Array(raw['included'])
             .select { |item| item.is_a?(Hash) && item['type'] }
             .map { |item| build(item) }

  { data: data, included: included }
end