From f248098401e90ae73d37cd6ed71681cb04c0b203 Mon Sep 17 00:00:00 2001 From: Felipe Gentil Date: Mon, 16 Feb 2015 13:04:49 -0200 Subject: [PATCH 1/6] Sets :per_page base on current_options if necessary --- lib/github_api/api/arguments.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/github_api/api/arguments.rb b/lib/github_api/api/arguments.rb index 14312aa6..afdfe252 100644 --- a/lib/github_api/api/arguments.rb +++ b/lib/github_api/api/arguments.rb @@ -9,6 +9,7 @@ class Arguments include Validations AUTO_PAGINATION = 'auto_pagination'.freeze + PER_PAGE = 'per_page'.freeze # Parameters passed to request attr_reader :params @@ -100,6 +101,7 @@ def parse(*args, &block) @params = options @remaining = args[@required.size..-1] extract_pagination(options) + set_per_page(options) yield_or_eval(&block) self @@ -243,6 +245,16 @@ def yield_or_eval(&block) return unless block block.arity > 0 ? yield(self) : instance_eval(&block) end + + # Handle pagination params when they are not passed directly + # + def set_per_page(options) + per_page_config = api.current_options[:"#{PER_PAGE}"] + if (per_page_config != Github::Configuration.property_set[:"#{PER_PAGE}"]) + options[PER_PAGE] ||= per_page_config unless per_page_config.nil? + end + options + end end # Arguments end # Api end # Github From 7354f5c5c94b12ddf03e9b3166e822d35659cc9f Mon Sep 17 00:00:00 2001 From: Felipe Gentil Date: Wed, 18 Feb 2015 10:21:11 -0200 Subject: [PATCH 2/6] Tests for handling per_page as parameter in different requests --- spec/integration/arguments_spec.rb | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/spec/integration/arguments_spec.rb b/spec/integration/arguments_spec.rb index 102182ae..ac255241 100644 --- a/spec/integration/arguments_spec.rb +++ b/spec/integration/arguments_spec.rb @@ -71,4 +71,54 @@ subject.get user, repo, milestone_id, :auto_pagination => true end end + + context 'handling per_page as a parameter' do + let(:per_page) { 2 } + + before :each do + stub_request(:get, url). + to_return(:status => 200, :body => '', :headers => {}) + end + + context 'when per_page is passed on Github init' do + let(:token) { [*('a'..'z')].sample(30).join } + let(:instance) { Github.new(oauth_token: token, per_page: per_page) } + let(:response) { instance.repos.list } + let(:url) { "https://api.github.com/user/repos?access_token=#{token}&per_page=#{per_page}" } + + it 'passes per_page params to url' do + expect(response.status).to eq 200 + end + end + + context 'when per_page is passed on Repos initialize only' do + let(:repos) { Github::Client::Repos.new(per_page: per_page) } + let(:response) { repos.list(user: 'fpgentil') } + let(:url) { "https://api.github.com/users/fpgentil/repos?per_page=#{per_page}" } + + it 'passes per_page params to url' do + expect(response.status).to eq 200 + end + end + + context 'when per_page is passed on Repos initialize and list method' do + let(:repos) { Github::Client::Repos.new(per_page: per_page + 5) } + let(:response) { repos.list(user: 'fpgentil', per_page: per_page) } + let(:url) { "https://api.github.com/users/fpgentil/repos?per_page=#{per_page}" } + + it 'passes per_page params to url' do + expect(response.status).to eq 200 + end + end + + context 'when per_page is not passed as params' do + let(:repos) { Github::Client::Repos.new } + let(:response) { repos.list(user: 'fpgentil') } + let(:url) { "https://api.github.com/users/fpgentil/repos" } + + it 'does not pass per_page params to url' do + expect(response.status).to eq 200 + end + end + end end From ec020e49d411dc8651f8fa15429ebb89ceacf0f6 Mon Sep 17 00:00:00 2001 From: Thomas Rodriguez Date: Wed, 18 Feb 2015 11:16:53 -0700 Subject: [PATCH 3/6] Update README.md Spelling correction under Manipulating Files. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 13a09230..cc33996c 100644 --- a/README.md +++ b/README.md @@ -621,7 +621,7 @@ In order to be able to create/update/remove files you need to use Contents API l contents = Github::Client::Repos::Contents.new oauth_token: '...' ``` -Having instantiaed the contents, to create a file do: +Having instantiated the contents, to create a file do: ```ruby contents.create 'username', 'repo_name', 'full_path_to/file.ext', From e8cad18e4f4b658dc97ea5fd2ef08960e5d95dc1 Mon Sep 17 00:00:00 2001 From: Felipe Gentil Date: Fri, 17 Apr 2015 17:29:52 -0300 Subject: [PATCH 4/6] Remove from arguments logic to set per_page as parameter --- lib/github_api/api/arguments.rb | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lib/github_api/api/arguments.rb b/lib/github_api/api/arguments.rb index afdfe252..14312aa6 100644 --- a/lib/github_api/api/arguments.rb +++ b/lib/github_api/api/arguments.rb @@ -9,7 +9,6 @@ class Arguments include Validations AUTO_PAGINATION = 'auto_pagination'.freeze - PER_PAGE = 'per_page'.freeze # Parameters passed to request attr_reader :params @@ -101,7 +100,6 @@ def parse(*args, &block) @params = options @remaining = args[@required.size..-1] extract_pagination(options) - set_per_page(options) yield_or_eval(&block) self @@ -245,16 +243,6 @@ def yield_or_eval(&block) return unless block block.arity > 0 ? yield(self) : instance_eval(&block) end - - # Handle pagination params when they are not passed directly - # - def set_per_page(options) - per_page_config = api.current_options[:"#{PER_PAGE}"] - if (per_page_config != Github::Configuration.property_set[:"#{PER_PAGE}"]) - options[PER_PAGE] ||= per_page_config unless per_page_config.nil? - end - options - end end # Arguments end # Api end # Github From a99d26fe8a81926c8c5f75144319b8cf4f9015d6 Mon Sep 17 00:00:00 2001 From: Felipe Gentil Date: Fri, 17 Apr 2015 17:30:38 -0300 Subject: [PATCH 5/6] Sets per_page parameter using Pagination module --- lib/github_api/client/repos.rb | 3 +++ lib/github_api/pagination.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/github_api/client/repos.rb b/lib/github_api/client/repos.rb index b3786623..6af035da 100644 --- a/lib/github_api/client/repos.rb +++ b/lib/github_api/client/repos.rb @@ -132,6 +132,9 @@ def list(*args) permit %w[ user org type sort direction since ] end params = arguments.params + unless params.symbolize_keys[:per_page] + params.merge!(Pagination.per_page_as_param(current_options[:per_page])) + end response = if (user_name = params.delete('user') || user) get_request("/users/#{user_name}/repos", params) diff --git a/lib/github_api/pagination.rb b/lib/github_api/pagination.rb index 945224b9..064dd715 100644 --- a/lib/github_api/pagination.rb +++ b/lib/github_api/pagination.rb @@ -6,6 +6,8 @@ module Github module Pagination include Github::Constants + PER_PAGE = 'per_page'.freeze + # Return page links def links @links = Github::PageLinks.new(env[:response_headers]) @@ -91,6 +93,16 @@ def has_next_page? page_iterator.next? end + # Handle pagination params when they are not passed directly + # + def self.per_page_as_param(per_page_config) + params = {} + if (per_page_config != Github::Configuration.property_set[:per_page]) + params[:per_page] = per_page_config unless per_page_config.nil? + end + params + end + private # Internally used page iterator From e7bce9fa7159a0b996c29df4a9c8b5a2bb8fd52f Mon Sep 17 00:00:00 2001 From: Felipe Gentil Date: Fri, 17 Apr 2015 17:33:48 -0300 Subject: [PATCH 6/6] Remove unecessary PER_PAGE constant --- lib/github_api/pagination.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/github_api/pagination.rb b/lib/github_api/pagination.rb index 064dd715..14d843b1 100644 --- a/lib/github_api/pagination.rb +++ b/lib/github_api/pagination.rb @@ -6,8 +6,6 @@ module Github module Pagination include Github::Constants - PER_PAGE = 'per_page'.freeze - # Return page links def links @links = Github::PageLinks.new(env[:response_headers])