Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added an auto-pagination option that will fetch all results upon requ…

…est.
  • Loading branch information...
commit 5162cd75f0982563309a6787a6713459b1e51633 1 parent 4af419b
Brian Guthrie bguthrie authored
25 lib/octokit/configuration.rb
View
@@ -10,11 +10,13 @@ module Configuration
:password,
:proxy,
:oauth_token,
- :user_agent].freeze
+ :user_agent,
+ :auto_paginate].freeze
- DEFAULT_ADAPTER = Faraday.default_adapter
- DEFAULT_API_VERSION = 2
- DEFAULT_USER_AGENT = "Octokit Ruby Gem #{Octokit::VERSION}".freeze
+ DEFAULT_ADAPTER = Faraday.default_adapter
+ DEFAULT_API_VERSION = 2
+ DEFAULT_USER_AGENT = "Octokit Ruby Gem #{Octokit::VERSION}".freeze
+ DEFAULT_AUTO_PAGINATE = false
attr_accessor(*VALID_OPTIONS_KEYS)
@@ -31,13 +33,14 @@ def options
end
def reset
- self.adapter = DEFAULT_ADAPTER
- self.api_version = DEFAULT_API_VERSION
- self.login = nil
- self.password = nil
- self.proxy = nil
- self.oauth_token = nil
- self.user_agent = DEFAULT_USER_AGENT
+ self.adapter = DEFAULT_ADAPTER
+ self.api_version = DEFAULT_API_VERSION
+ self.login = nil
+ self.password = nil
+ self.proxy = nil
+ self.oauth_token = nil
+ self.user_agent = DEFAULT_USER_AGENT
+ self.auto_paginate = DEFAULT_AUTO_PAGINATE
end
end
end
18 lib/octokit/request.rb
View
@@ -38,7 +38,23 @@ def request(method, path, options, version, authenticate, raw, force_urlencoded)
end
end
end
- raw ? response : response.body
+
+ if raw
+ response
+ elsif auto_paginate && ( next_url = links(response)["next"] )
+ response.body + request(method, next_url, options, version, authenticate, raw, force_urlencoded)
+ else
+ response.body
+ end
+ end
+
+ def links(response)
+ links = ( response.headers["Link"] || "" ).split(', ').map do |link|
+ url, type = link.match(/<(.*?)>; rel="(\w+)"/).captures
+ [ type, url ]
+ end
+
+ Hash[ *links.flatten ]
end
end
end
16 spec/octokit/client_spec.rb
View
@@ -9,4 +9,20 @@
Octokit::Client.new(:login => 'foo', :password => 'bar').commits('baz/quux')
}.should_not raise_exception
end
+
+ it "should paginate the response if auto_paginate is on" do
+ stub_get("https://api.github.com/foo/bar").
+ to_return(:status => 200, :body => %q{["stuff"]}, :headers =>
+ { 'Link' => %q{<https://api.github.com/foo/bar?page=2>; rel="next", <https://api.github.com/foo/bar?page=3>; rel="last"} })
+
+ stub_get("https://api.github.com/foo/bar?page=2").
+ to_return(:status => 200, :body => %q{["even more stuff"]}, :headers =>
+ { 'Link' => %q{<https://api.github.com/foo/bar?page=3>; rel="next", <https://api.github.com/foo/bar?page=3>; rel="last", <https://api.github.com/foo/bar?page=1>; rel="prev", <https://api.github.com/foo/bar?page=1>; rel="first"} })
+
+ stub_get("https://api.github.com/foo/bar?page=3").
+ to_return(:status => 200, :body => %q{["stuffapalooza"]}, :headers =>
+ { 'Link' => %q{<https://api.github.com/foo/bar?page=2>; rel="prev", <https://api.github.com/foo/bar?page=1>; rel="first"} })
+
+ Octokit::Client.new(:auto_paginate => true).get("https://api.github.com/foo/bar", {}, 3).should == ['stuff', 'even more stuff', 'stuffapalooza']
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.