Module: Flexirest::JsonAPIProxy::Response
Overview
Parsing JSON API responses
Instance Method Summary collapse
Methods included from Helpers
Instance Method Details
#parse(body, object) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/flexirest/json_api_proxy.rb', line 194 def parse(body, object) # Save resource class for building lazy association loaders save_resource_class(object) # Retrieve the resource(s) object or array from the data object records = body['data'] return records unless records.present? # Convert the resource object to an array, # because it is easier to work with an array than a single object # Also keep track if record is singular or plural for the result later is_singular_record = records.is_a?(Hash) records = [records] if is_singular_record # Retrieve all names of linked relationships if records.first['relationships'] relationships = records.first['relationships'].keys end resource_type = records.first['type'] included = body['included'] # Parse the records, and retrieve all resources in a # (nested) array of resources that is easy to work with in Flexirest resources = records.map do |record| fetch_attributes_and_relationships( resource_type, record, included, relationships ) end # Depending on whether we got a resource object (hash) or array # in the beginning, return to the caller with the same type is_singular_record ? resources.first : resources end |
#save_resource_class(object) ⇒ Object
190 191 192 |
# File 'lib/flexirest/json_api_proxy.rb', line 190 def save_resource_class(object) @resource_class = object.is_a?(Class) ? object : object.class end |