module Tankard::Api::Utils::PageFinders

Seamless querying of multiple pages of results Allows using any ruby enumerable (EX. map)

@author Matthew Shafer

Public Instance Methods

each(&block) click to toggle source

Loads data from brewerydb and calls supplied block with resulting data

@yieldparam [Hash] hash containing individual beer information

# File lib/tankard/api/utils/page_finders.rb, line 17
def each(&block)
  find_on_single_or_all_pages(http_request_parameters, block)
end

Private Instance Methods

call_block_with_data(data, block) click to toggle source
# File lib/tankard/api/utils/page_finders.rb, line 48
def call_block_with_data(data, block)
  fail Tankard::Error::InvalidResponse unless data

  if data.is_a?(Hash)
    block.call(data)
  else
    data.each { |item| block.call(item) }
  end
end
find_on_all_pages(options, block) click to toggle source
# File lib/tankard/api/utils/page_finders.rb, line 31
def find_on_all_pages(options, block)
  page = 0

  loop do
    page += 1
    options[:p] = page if page > 1
    total_pages = find_on_single_page(options, block)
    break unless page < total_pages
  end
end
find_on_single_or_all_pages(options, block) click to toggle source
# File lib/tankard/api/utils/page_finders.rb, line 23
def find_on_single_or_all_pages(options, block)
  if options[:p]
    find_on_single_page(options, block)
  else
    find_on_all_pages(options, block)
  end
end
find_on_single_page(options, block) click to toggle source
# File lib/tankard/api/utils/page_finders.rb, line 42
def find_on_single_page(options, block)
  response = get_request(http_client, http_request_uri, options)
  call_block_with_data(response['data'], block)
  response['numberOfPages'].to_i
end
http_client() click to toggle source
# File lib/tankard/api/utils/page_finders.rb, line 62
def http_client
  fail NoMethodError, 'Need to implement method'
end
http_request_parameters() click to toggle source
# File lib/tankard/api/utils/page_finders.rb, line 66
def http_request_parameters
  fail NoMethodError, 'Need to implement method'
end
http_request_uri() click to toggle source
# File lib/tankard/api/utils/page_finders.rb, line 58
def http_request_uri
  fail NoMethodError, 'Need to implement method'
end