Skip to content

Commit

Permalink
move find_* methods into a module
Browse files Browse the repository at this point in the history
ability to remove code duplication.  Still need to move beer over to use the new finders
  • Loading branch information
matthewshafer committed May 4, 2013
1 parent 9239c4f commit d569a07
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
31 changes: 4 additions & 27 deletions lib/tankard/api/beers.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'hashie'
require 'tankard/api/request/get'
require 'tankard/api/utils/finders'

module Tankard
module Api
class Beers
include ::Enumerable
include Tankard::Api::Request::Get
include Tankard::Api::Utils::Finders

def initialize(request, options={})
@request = request
Expand All @@ -14,9 +16,9 @@ def initialize(request, options={})

def each(&block)
if options_have_page_set
find_on_single_page(block)
find_on_single_page("beers", @request, @options, block)
else
find_on_all_pages(block)
find_on_all_pages("beers", @request, @options,block)
end
end

Expand All @@ -35,31 +37,6 @@ def page(number)
def options_have_page_set
@options.has_key?(:p)
end

def find_on_all_pages(block)
page = 0
options = @options.clone
begin
page += 1
options[:p] = page
response = get_request(@request, "beers", options)
total_pages = response["numberOfPages"].to_i
data = response["data"]
raise Tankard::Error::InvalidResponse unless data
data.each { |beer| block.call(beer) }
end while page < total_pages
end

def find_on_single_page(block)
data = request_data(@request, "beers", @options)
raise Tankard::Error::InvalidResponse unless data

if data.is_a?(Hash)
block.call(data)
else
data.each { |beer| block.call(beer) }
end
end
end
end
end
42 changes: 42 additions & 0 deletions lib/tankard/api/utils/finders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'tankard/api/request/get'

module Tankard
module Api
module Utils
module Finders
include Tankard::Api::Request::Get

private
def find_on_all_pages(uri, request, options, block)
page = 0
# the clone is here just in case someone decides to create an api class of their own
# and not go through client. If they re-use the api class they have created then
# we would possibably have additional options set that they would reuse with subsequent calls
# honestly though no one should ever create an api class of their own to use, its a bad idea
# might pull out this clone at some point
options = options.clone
begin
page += 1
options[:p] = page
response = get_request(request, uri, options)
total_pages = response["numberOfPages"].to_i
data = response["data"]
raise Tankard::Error::InvalidResponse unless data
data.each { |beer| block.call(beer) }
end while page < total_pages
end

def find_on_single_page(uri, request, options, block)
data = request_data(request, uri, options)
raise Tankard::Error::InvalidResponse unless data

if data.is_a?(Hash)
block.call(data)
else
data.each { |beer| block.call(beer) }
end
end
end
end
end
end

0 comments on commit d569a07

Please sign in to comment.