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

Constructor Details

#initialize(options = {}) ⇒ JsonApiSerializer

Returns a new instance of JsonApiSerializer.



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

def initialize(options = {})
end

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



19
20
21
22
23
24
25
26
27
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 19

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



15
16
17
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 15

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

#extract_many(klass, response) ⇒ Object



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

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



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

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


84
85
86
87
88
89
90
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 84

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



92
93
94
# File 'lib/ruby_json_api_client/serializers/json_api_serializer.rb', line 92

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

#extract_single(klass, id, response) ⇒ Object



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

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



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

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