class BridgeAPI::ApiArray

Attributes

body[R]
errors[R]
headers[R]
linked[R]
members[R]
meta[R]
status[R]

Public Class Methods

new(response, api_client, result_mapping) click to toggle source
# File lib/bridge_api/api_array.rb, line 16
def initialize(response, api_client, result_mapping)
  @meta_fields = %w[meta linked]
  @api_client = api_client
  @linked = {}
  @meta = {}
  @extra_meta_fields = []
  pattern = %r{.*(/api/.*)}
  path = response.env.url
  matches = pattern.match(path.to_s)
  mapping = nil
  if matches
    mapping_exists = result_mapping.key?(matches[1])
    mapping = result_mapping[matches[1]] if mapping_exists
  end
  @extra_meta_fields.concat(mapping[:meta]) unless mapping.nil?
  case response.status
  when *((200..206).to_a + [302])
    apply_response_metadata(response)
    @members = get_response_content(response)
  else
    set_response_vars(response)
    @members = []
  end
end
process_response(response, api_client, result_mapping) click to toggle source
# File lib/bridge_api/api_array.rb, line 12
def self.process_response(response, api_client, result_mapping)
  ApiArray.new(response, api_client, result_mapping)
end

Public Instance Methods

[](i) click to toggle source
# File lib/bridge_api/api_array.rb, line 56
def [](i)
  @members[i]
end
all_pages!() click to toggle source
# File lib/bridge_api/api_array.rb, line 87
def all_pages!
  if pages?
    response = get_page(@next_page)
    apply_response_metadata(response)
    @members.concat(get_response_content(response))
    while @next_page
      response = get_page(@next_page)
      apply_response_metadata(response)
      @members.concat(get_response_content(response))
    end
  end
  self
end
each() { |member| ... } click to toggle source
# File lib/bridge_api/api_array.rb, line 64
def each
  @members.each { |member| yield(member) }
end
each_page() { |members, linked, meta| ... } click to toggle source
# File lib/bridge_api/api_array.rb, line 76
def each_page
  yield(@members, @linked, @meta)
  while @next_page
    response = get_page(@next_page)
    apply_response_metadata(response, false)
    @members = get_response_content(response)
    yield(@members, @linked, @meta)
  end
  @link_hash = {}
end
last() click to toggle source
# File lib/bridge_api/api_array.rb, line 60
def last
  @members.last
end
length() click to toggle source
# File lib/bridge_api/api_array.rb, line 52
def length
  @members.length
end
next_page() click to toggle source
# File lib/bridge_api/api_array.rb, line 72
def next_page
  load_page(@next_page)
end
pages?() click to toggle source
# File lib/bridge_api/api_array.rb, line 68
def pages?
  !@next_page.nil?
end
set_response_vars(response) click to toggle source
# File lib/bridge_api/api_array.rb, line 41
def set_response_vars(response)
  @headers = response.headers
  @status = response.status
  @body = response.body
  @errors = begin
              response.body['errors']
            rescue StandardError
              []
            end
end

Private Instance Methods

apply_response_metadata(response, concat = true) click to toggle source
# File lib/bridge_api/api_array.rb, line 130
def apply_response_metadata(response, concat = true)
  unless concat
    @linked = {}
    @meta = {}
  end
  @method = response.env[:method]
  set_response_vars(response)
  init_pages(response)
  init_linked(response)
  init_meta(response)
end
get_page(url, params = {}) click to toggle source
# File lib/bridge_api/api_array.rb, line 103
def get_page(url, params = {})
  query = URI.parse(url).query
  p = CGI.parse(query).merge(params)
  u = url.gsub("?#{query}", '')
  p.each { |k, v| p[k] = v.first if v.is_a?(Array) }
  @api_client.enforce_rate_limits
  response = @api_client.connection.send(:get) do |r|
    r.url(u, p)
  end
  @api_client.apply_rate_limits(response)
  response
end
get_response_content(response) click to toggle source
# File lib/bridge_api/api_array.rb, line 121
def get_response_content(response)
  return [] unless response.body.is_a?(Hash)

  content = response.body.reject { |k, _v| @meta_fields.include?(k) || @extra_meta_fields.include?(k) }
  return Array(content.values[0]) unless content.empty?

  []
end
init_linked(response) click to toggle source
# File lib/bridge_api/api_array.rb, line 142
def init_linked(response)
  if response.body.is_a?(Hash) && response.body.key?('linked')
    @linked = @linked.merge(response.body['linked']) { |_key, oldval, newval| [*oldval].to_a + [*newval].to_a }
  end
end
init_meta(response) click to toggle source
# File lib/bridge_api/api_array.rb, line 148
def init_meta(response)
  if response.body.is_a?(Hash) && response.body.key?('meta')
    @meta = @meta.merge(response.body['meta']) { |_key, oldval, newval| [*oldval].to_a + [*newval].to_a }
    @extra_meta_fields.each do |field|
      @meta[field] = response.body[field]
    end
  end
end
init_pages(response) click to toggle source
# File lib/bridge_api/api_array.rb, line 157
def init_pages(response)
  @next_page = nil
  @prev_page = nil
  if response.body.is_a?(Hash) && response.body.key?('meta')
    if response.body['meta'].key?('next')
      @next_page = response.body['meta']['next']
    end
    if response.body['meta'].key?('prev')
      @prev_page = response.body['meta']['prev']
    end
  end
end
load_page(url) click to toggle source
# File lib/bridge_api/api_array.rb, line 116
def load_page(url)
  response = get_page(url)
  ApiArray.process_response(response, @api_client)
end