-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to remove code duplication. Still need to move beer over to use the new finders
- Loading branch information
1 parent
9239c4f
commit d569a07
Showing
2 changed files
with
46 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |