class Foederati::Provider::Response

Contains a response from a request to a provider's API

Returned by `Foederati::Provider::Request#execute`.

Attributes

faraday_response[R]
provider[R]

Public Class Methods

new(provider, faraday_response) click to toggle source

@param provider [Foederati::Provider] provider the API response is for @param faraday_response [Faraday::Response] Faraday response object

# File lib/foederati/provider/response.rb, line 16
def initialize(provider, faraday_response)
  @provider = provider
  @faraday_response = faraday_response
end

Public Instance Methods

normalise() click to toggle source

Normalises response from provider's API

@return [Hash]

# File lib/foederati/provider/response.rb, line 25
def normalise
  {
    id => {
      total: fetch_from_response(results.total, body) || 0,
      results: items_from_response.map do |item|
        thumbnail = fetch_from_response(fields.thumbnail, item)
        thumbnail = thumbnail.is_a?(Array) ? thumbnail.first : thumbnail
        {
          title: fetch_from_response(fields.title, item),
          thumbnail: thumbnail,
          url: fetch_from_response(fields.url, item)
        }
      end
    }
  }
end
Also aliased as: to_h
to_h()
Alias for: normalise

Protected Instance Methods

fetch_deep(keys, hash) click to toggle source

Digs down into a nested hash to get the value beneath multiple keys

@example

fetch_deep(%i(a b), { a: { b: 'c' } }) #=> 'c'

@param keys one or more keys to fetch from the hash @param hash [Hash] the hash to fetch deep from

# File lib/foederati/provider/response.rb, line 76
def fetch_deep(keys, hash)
  return hash unless hash.is_a?(Hash)

  local_keys = [keys.dup].flatten
  return hash if local_keys.blank?

  key = local_keys.shift
  fetch_deep(local_keys, hash[key])
end
fetch_from_response(field, hash) click to toggle source

Fetch a field from part of the provider's JSON response

@param field `Proc` to call with `hash`, else keys to pass to `#fetch_deep` @param hash [Hash] (part of) the JSON response hash

# File lib/foederati/provider/response.rb, line 58
def fetch_from_response(field, hash)
  if field.blank?
    nil
  elsif field.respond_to?(:call)
    field.call(hash)
  else
    fetch_deep(field, hash)
  end
end
items_from_response() click to toggle source

Gets the set of items from the response body

@return [Array]

# File lib/foederati/provider/response.rb, line 49
def items_from_response
  fetch_from_response(results.items, body) || []
end