Class: RubyJsonApiClient::AmsSerializer
- Inherits:
-
Object
- Object
- RubyJsonApiClient::AmsSerializer
- Defined in:
- lib/ruby_json_api_client/serializers/ams_serializer.rb
Instance Attribute Summary collapse
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
- #_create_model(klass, data) ⇒ Object
- #assert(test, failure_message) ⇒ Object
- #extract_many(klass, response, key = nil) ⇒ Object
- #extract_many_relationship(parent, name, options, response) ⇒ Object
- #extract_many_relationship_from_links(parent, name, options, url) ⇒ Object
- #extract_many_relationship_from_sideload(parent, name, options, response) ⇒ Object
- #extract_single(klass, id, response) ⇒ Object
- #extract_single_relationship(parent, name, options, response) ⇒ Object
- #extract_single_relationship_from_links(parent, name, options, url) ⇒ Object
- #extract_single_relationship_from_sideload(parent, name, options, response) ⇒ Object
- #to_json(model) ⇒ Object
- #transform(response) ⇒ Object
Instance Attribute Details
#store ⇒ Object
Returns the value of attribute store.
6 7 8 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 6 def store @store end |
Instance Method Details
#_create_model(klass, data) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 43 def _create_model(klass, data) model = klass.new(meta: {}) model.[:data] = data if data['links'] model.[:links] = data['links'] end data.reduce(model) do |record, (field, value)| if klass.has_field?(field.to_sym) record.send("#{field}=", value) end record end end |
#assert(test, failure_message) ⇒ Object
39 40 41 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 39 def assert(test, ) raise if !test end |
#extract_many(klass, response, key = nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 81 def extract_many(klass, response, key = nil) key = klass.to_s.underscore if key.nil? plural = ActiveSupport::Inflector.pluralize(key) 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 << _create_model(klass, json) end end |
#extract_many_relationship(parent, name, options, response) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 98 def extract_many_relationship(parent, name, , response) # given response this will find the relationship # for ams based apis the relationship will either be # 1) in links # 2) in sideloaded data data = transform(response) singular = ActiveSupport::Inflector.singularize(name) = parent. || {} = [:links] = [:data] if && [name.to_s] extract_many_relationship_from_links(parent, name, , [name.to_s]) elsif data[name.to_s] && && ["#{singular}_ids"] extract_many_relationship_from_sideload(parent, name, , response) else [] end end |
#extract_many_relationship_from_links(parent, name, options, url) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 121 def extract_many_relationship_from_links(parent, name, , url) # 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 klass_name = [:class_name] || ActiveSupport::Inflector.classify(name) klass = ActiveSupport::Inflector.constantize(klass_name) store.load_collection(klass, url) end |
#extract_many_relationship_from_sideload(parent, name, options, response) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 131 def extract_many_relationship_from_sideload(parent, name, , response) singular = ActiveSupport::Inflector.singularize(name) plural = ActiveSupport::Inflector.pluralize(name) klass_name = [:class_name] || ActiveSupport::Inflector.classify(name) klass = ActiveSupport::Inflector.constantize(klass_name) = parent.[:data] ids = ["#{singular}_ids"] idMap = ids.reduce({}) do |map, id| map[id] = true map end extract_many(klass, response, plural) .select { |record| idMap[record.id] } end |
#extract_single(klass, id, response) ⇒ Object
61 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/ams_serializer.rb', line 61 def extract_single(klass, id, response) name = klass.to_s.underscore data = transform(response) assert data[name], "No key #{name} in json response." assert data[name]['id'], "No id included in #{name} json data" if id # we will allow idless loading, but if an id is given we # will try to verify it. assert data[name]['id'].to_s == id.to_s, "Tried to find #{name} with id #{id}, but got #{name} with id #{data[name]['id']}." end _create_model(klass, data[name]) end |
#extract_single_relationship(parent, name, options, response) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 148 def extract_single_relationship(parent, name, , response) plural = ActiveSupport::Inflector.pluralize(name) data = transform(response) = parent. || {} = [:links] = [:data] if && [name.to_s] extract_single_relationship_from_links(parent, name, , [name.to_s]) elsif data[plural.to_s] && && ["#{name}_id"] extract_single_relationship_from_sideload(parent, name, , response) else nil # nothing found, return nil object end end |
#extract_single_relationship_from_links(parent, name, options, url) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 167 def extract_single_relationship_from_links(parent, name, , url) klass_name = [:class_name] || ActiveSupport::Inflector.classify(name) klass = ActiveSupport::Inflector.constantize(klass_name) = parent. || {} = [:data] || {} store.load_single(klass, ["#{name}_id"], url) end |
#extract_single_relationship_from_sideload(parent, name, options, response) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 176 def extract_single_relationship_from_sideload(parent, name, , response) plural = ActiveSupport::Inflector.pluralize(name) klass_name = [:class_name] || ActiveSupport::Inflector.classify(name) klass = ActiveSupport::Inflector.constantize(klass_name) = parent.[:data] id = ["#{name}_id"] extract_many(klass, response, plural) .detect { |record| record.id == id } end |
#to_json(model) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 12 def to_json(model) key = model.class.to_s.underscore.downcase data = {} data[key] = {} if model.persisted? data[key][:id] = model.id end # conert fields to json model.class.fields.reduce(data[key]) do |result, field| result[field] = model.send(field) result end # convert has one relationships to json relationships = model.loaded_has_ones || {} relationships.reduce(data[key]) do |result, (name, relationship)| if relationship.id result["#{name}_id"] = relationship.id end result end JSON::generate(data) end |
#transform(response) ⇒ Object
8 9 10 |
# File 'lib/ruby_json_api_client/serializers/ams_serializer.rb', line 8 def transform(response) JSON.parse(response) end |