class Adapter::JsonApi

Public Class Methods

collection(options, response, _binding) click to toggle source
# File lib/rspec/api_helpers/adapter/jsonapi/adapter.rb, line 14
def collection(options, response, _binding)
  jsonapi_collection = HashWithIndifferentAccess.new(JSON.parse(response.body))

  jsonapi_collection = transform_collection(jsonapi_collection.dig('data') || {})

  return Adapter::ActiveModel::Collection.new(options, jsonapi_collection, _binding)
end
resource(options, response, _binding) click to toggle source
# File lib/rspec/api_helpers/adapter/jsonapi/adapter.rb, line 6
def resource(options, response, _binding)
  jsonapi_resource = HashWithIndifferentAccess.new(JSON.parse(response.body))

  jsonapi_resource = transform_resource(jsonapi_resource.dig('data') || {})

  return Adapter::ActiveModel::Resource.new(options, jsonapi_resource, _binding)
end

Private Class Methods

transform_collection(jsonapi_hash, root: true) click to toggle source
# File lib/rspec/api_helpers/adapter/jsonapi/adapter.rb, line 37
def transform_collection(jsonapi_hash, root: true)
  type = jsonapi_hash.first.dig('type').pluralize

  json_hash = jsonapi_hash.map{|data|
    transform_resource(data, root: false)
  }

  if root
    return HashWithIndifferentAccess.new({type => json_hash})
  else
    return json_hash
  end
end
transform_resource(jsonapi_hash, root: true) click to toggle source
# File lib/rspec/api_helpers/adapter/jsonapi/adapter.rb, line 24
def transform_resource(jsonapi_hash, root: true)
  type = jsonapi_hash.dig('type').singularize
  json_hash = jsonapi_hash.dig('attributes').transform_keys{|key|
    key.underscore
  }.merge(id: jsonapi_hash.dig('id').to_s)

  if root
    return HashWithIndifferentAccess.new({type => json_hash})
  else
    return HashWithIndifferentAccess.new(json_hash)
  end
end