class PaychexAPI::ApiArray

Constants

MAX_PAGES

Attributes

errors[R]
headers[R]
members[W]
metadata[R]
next_page[W]
status[R]

Public Class Methods

new(response, api_client) click to toggle source
# File lib/paychex_api/api_array.rb, line 18
def initialize(response, api_client)
  @meta_fields = %w[metadata links]
  @api_client = api_client
  @links = []
  @metadata = {}
  @errors = response.body['errors'] if response.body.is_a?(Hash) && response.body.key?('errors')
  case response.status
  when *((200..206).to_a + [302])
    apply_response_metadata(response)
    @members = get_response_content(response)
  end
end
process_response(response, api_client) click to toggle source
# File lib/paychex_api/api_array.rb, line 14
def self.process_response(response, api_client)
  ApiArray.new(response, api_client)
end

Public Instance Methods

[](index) click to toggle source
# File lib/paychex_api/api_array.rb, line 43
def [](index)
  members[index]
end
all_pages!() click to toggle source
# File lib/paychex_api/api_array.rb, line 74
def all_pages!
  return self unless pages?

  combine_page
  combine_pages
  self
end
each() { |member| ... } click to toggle source
# File lib/paychex_api/api_array.rb, line 51
def each
  members.each { |member| yield(member) }
end
each_page() { |members, linked, meta| ... } click to toggle source
# File lib/paychex_api/api_array.rb, line 63
def each_page
  yield(members, @linked, @meta)
  while @next_page
    response = 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/paychex_api/api_array.rb, line 47
def last
  members.last
end
length() click to toggle source
# File lib/paychex_api/api_array.rb, line 39
def length
  members.length
end
members() click to toggle source
# File lib/paychex_api/api_array.rb, line 31
def members
  @members || []
end
next_page() click to toggle source
# File lib/paychex_api/api_array.rb, line 59
def next_page
  load_page(@next_page)
end
pages?() click to toggle source
# File lib/paychex_api/api_array.rb, line 55
def pages?
  !@next_page.nil?
end
size() click to toggle source
# File lib/paychex_api/api_array.rb, line 35
def size
  length
end

Private Instance Methods

apply_response_metadata(response, concat = true) click to toggle source
# File lib/paychex_api/api_array.rb, line 136
def apply_response_metadata(response, concat = true)
  unless concat
    @links = []
    @metadata = {}
  end

  @status = response.status
  @headers = response.headers
  @method = response.env[:method]

  init_pages(response)
  init_linked(response)
  init_meta(response)
end
combine_page() click to toggle source
# File lib/paychex_api/api_array.rb, line 92
def combine_page
  response = page(@next_page)
  apply_response_metadata(response)
  @members.concat(get_response_content(response))
end
combine_pages() click to toggle source
# File lib/paychex_api/api_array.rb, line 84
def combine_pages
  counter = 0
  while @next_page && counter <= MAX_PAGES
    combine_page
    counter += 1
  end
end
get_response_content(response) click to toggle source
# File lib/paychex_api/api_array.rb, line 124
def get_response_content(response)
  body = response.body

  return [] unless body.is_a?(Hash)

  if body.key?('content')
    body['content'] || []
  else
    body.present? ? body : []
  end
end
init_linked(response) click to toggle source
# File lib/paychex_api/api_array.rb, line 151
def init_linked(response)
  return nil unless response.body.is_a?(Hash) && response.body.key?('links')

  @links = @links.concat(response.body['links'])
end
init_meta(response) click to toggle source
# File lib/paychex_api/api_array.rb, line 157
def init_meta(response)
  return nil unless response.body.is_a?(Hash) && response.body.key?('metadata')

  @metadata = response.body['metadata']
end
init_pages(response) click to toggle source
# File lib/paychex_api/api_array.rb, line 163
def init_pages(response)
  return nil unless response.body.is_a?(Hash) && response.body.key?('links')

  @next_page, @prev_page = nil
  response.body['links'].each do |link|
    case link['rel']
    when 'next'
      @next_page = link['href']
    when 'prev'
      @prev_page = link['href']
    end
  end
end
load_page(url) click to toggle source
# File lib/paychex_api/api_array.rb, line 119
def load_page(url)
  response = page(url)
  ApiArray.process_response(response, @api_client)
end
page(url, params = {}, tries = 0) click to toggle source
# File lib/paychex_api/api_array.rb, line 98
def page(url, params = {}, tries = 0)
  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) }

  resp = @api_client.connection.send(:get) do |r|
    r.url(u, p)
  end

  tries = 0 if status == 200
  resp
rescue Footrest::HttpError::Unauthorized => e
  raise e unless tries.zero?

  # token may have expired for long running pagination requests, refresh and try again
  @api_client.refresh_token!
  page(url, params, (tries + 1))
end