OctoFetcher#iterate_pages: extract method

This commit is contained in:
Olle Jonsson 2016-11-10 19:32:24 +01:00
parent 3c39c0c178
commit d122e00a1a

View File

@ -249,33 +249,39 @@ Make sure, that you push tags to remote repo via 'git push --tags'"
# #
# @param [Octokit::Client] client # @param [Octokit::Client] client
# @param [String] method (eg. 'tags') # @param [String] method (eg. 'tags')
#
# @yield [Sawyer::Resource] An OctoKit-provided response (which can be empty)
#
# @return [Integer] total number of pages # @return [Integer] total number of pages
def iterate_pages(client, method, *args) def iterate_pages(client, method, *args)
if args.size == 1 && args.first.is_a?(Hash) request_opts = extract_request_args(args)
request_options = args.delete_at(0) args.push(@request_options.merge(request_opts))
elsif args.size > 1 && args.last.is_a?(Hash)
request_options = args.delete_at(args.length - 1)
end
args.push(@request_options.merge(request_options)) number_of_pages = 1
pages = 1 check_github_response { client.send(method, user_project, *args) }
check_github_response do
client.send(method, user_project, *args)
end
last_response = client.last_response last_response = client.last_response
yield last_response.data yield(last_response.data)
until (next_one = last_response.rels[:next]).nil? until (next_one = last_response.rels[:next]).nil?
pages += 1 number_of_pages += 1
last_response = check_github_response { next_one.get } last_response = check_github_response { next_one.get }
yield last_response.data yield(last_response.data)
end end
pages number_of_pages
end
def extract_request_args(args)
if args.size == 1 && args.first.is_a?(Hash)
args.delete_at(0)
elsif args.size > 1 && args.last.is_a?(Hash)
args.delete_at(args.length - 1)
else
{}
end
end end
# This is wrapper with rescue block # This is wrapper with rescue block