Class: RubyJsonApiClient::JsonApiSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_json_api_client/serializers/json_api_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#storeObject

Returns the value of attribute store.



6
7
8
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 6

def store
  @store
end

Instance Method Details

#_json_to_model(klass, json) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 16

def _json_to_model(klass, json)
  json.reduce(klass.new) do |model, (field, value)|
    if klass.has_field?(field.to_sym)
      model.send("#{field}=", value)
    end

    model
  end
end

#assert(test, failure_message) ⇒ Object



12
13
14
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 12

def assert(test, failure_message)
  raise failure_message if !test
end

#extract_many(klass, response) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 46

def extract_many(klass, response)
  name = klass.to_s.underscore
  plural = ActiveSupport::Inflector.pluralize(name)
  data = transform(response)

  assert data[plural],
    "No key #{plural} in json response."

  assert data[plural].is_a?(Array),
    "Key #{plural} should be an array"

  data[plural].reduce([]) do |collection, json|
    collection << _json_to_model(klass, json)
  end
end

#extract_many_relationship(parent, name, response) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 62

def extract_many_relationship(parent, name, response)
  # given response this will find the relationship
  # for json api the response will either be
  # 1) in links
  # 2) in linked
  json = transform(response)

  if json['links'] && json['links'][name]
    extract_many_relationship_from_links(name, json['links'][name])

  elsif json['linked'] && json['linked'][name]
    extract_many_relationship_from_linked(name, json['linked'][name])

  else
    raise "You asked for #{name} but it does not exist in links or linked"

  end
end


81
82
83
84
85
86
87
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 81

def extract_many_relationship_from_links(name, link)
  link_url = link['href']
  # since we only have a url pointing to where to pull
  # this info from we need to go back to the store and
  # have it pull this data
  store.load_collection(name, link_url)
end

#extract_relationship_from_linked(klass, name, linked) ⇒ Object



89
90
91
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 89

def extract_relationship_from_linked(klass, name, linked)
  extract_many(linked)
end

#extract_single(klass, id, response) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 26

def extract_single(klass, id, response)
  name = klass.to_s.underscore
  plural = ActiveSupport::Inflector.pluralize(name)
  data = transform(response)

  assert data[plural],
    "No key #{plural} in json response."

  assert data[plural].is_a?(Array),
    "Key #{plural} should be an array"

  assert data[plural][0]['id'],
    "No id included in #{plural}[0] json data"

  assert data[plural][0]['id'].to_s == id.to_s,
    "Tried to find #{name} with id #{id}, but got #{name} with id #{data[plural][0]['id']}."

  _json_to_model(klass, data[plural][0])
end

#transform(response) ⇒ Object



8
9
10
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 8

def transform(response)
  JSON.parse(response)
end