github_api 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,3 +2,9 @@ Feature: Githu API pagination
2
2
  In order to paginate number of received records
3
3
  As a user
4
4
  I want to be able to view them in butches
5
+
6
+ Scenario: Re
7
+ Given I have "Github::Repos" instance
8
+ And I am looking for "repos" with the following params:
9
+ | page | per_page |
10
+ And I make request within a cassette named "repos"
@@ -7,6 +7,7 @@ require 'github_api/mime_type'
7
7
  require 'github_api/core_ext/hash'
8
8
  require 'github_api/core_ext/array'
9
9
  require 'github_api/compatibility'
10
+ require 'github_api/api/actions'
10
11
 
11
12
  module Github
12
13
 
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class API
5
+
6
+ # Returns all API public methods for a given class.
7
+ def self.inherited(klass)
8
+ klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
9
+ def self.actions
10
+ self.new.api_methods_in(#{klass})
11
+ end
12
+ def actions
13
+ api_methods_in(#{klass})
14
+ end
15
+ RUBY_EVAL
16
+ super
17
+ end
18
+
19
+ def api_methods_in(klass)
20
+ puts "---"
21
+ (klass.send(:instance_methods, false) - ['actions']).sort.each do |rest_met|
22
+ puts "|--> #{rest_met}"
23
+ end
24
+ klass.included_modules.each do |mod|
25
+ if mod.to_s =~ /#{klass}/
26
+ puts "| \\ #{mod.to_s}"
27
+ mod.instance_methods(false).each do |met|
28
+ puts "| |--> #{met}"
29
+ end
30
+ puts "| /"
31
+ end
32
+ end
33
+ puts "---"
34
+ nil
35
+ end
36
+
37
+ end # API
38
+ end # Github
@@ -4,9 +4,11 @@ module Github
4
4
 
5
5
  extend self
6
6
 
7
- FIRST_PAGE = 1
7
+ FIRST_PAGE = 1 # Default request page if none provided
8
8
 
9
- PER_PAGE = 25
9
+ PER_PAGE = 30 # Default number of items as specified by API
10
+
11
+ NOT_FOUND = -1 # Either page or per_page parameter not present
10
12
 
11
13
  class << self
12
14
  attr_accessor :page, :per_page
@@ -21,8 +23,12 @@ module Github
21
23
  end
22
24
 
23
25
  def page_request(path, params={})
24
- params[PARAM_PER_PAGE] = default_page_size unless params[PARAM_PER_PAGE]
25
- params[PARAM_PAGE] = default_page unless params[PARAM_PAGE]
26
+ if params[PARAM_PER_PAGE] == NOT_FOUND
27
+ params[PARAM_PER_PAGE] = default_page_size
28
+ end
29
+ if !params[PARAM_PAGE] || params[PARAM_PAGE] == NOT_FOUND
30
+ params[PARAM_PAGE] = default_page
31
+ end
26
32
 
27
33
  Github::PagedRequest.page = params[PARAM_PAGE]
28
34
  Github::PagedRequest.per_page = params[PARAM_PER_PAGE]
@@ -4,7 +4,7 @@ module Github
4
4
  module VERSION
5
5
  MAJOR = 0
6
6
  MINOR = 4
7
- PATCH = 0
7
+ PATCH = 1
8
8
  BUILD = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.');
@@ -3,23 +3,52 @@ require 'spec_helper'
3
3
  describe Github::API do
4
4
 
5
5
  let(:api) { Github::API.new }
6
+ let(:repos) { Github::Repos }
6
7
 
7
- before(:each) do
8
- @params = { 'a' => { :b => { 'c' => 1 }, 'd' => [ 'a', { :e => 2 }] } }
8
+ it { described_class.included_modules.should include Github::Authorization }
9
+ it { described_class.included_modules.should include Github::MimeType }
10
+ it { described_class.included_modules.should include Github::Connection }
11
+ it { described_class.included_modules.should include Github::Request }
12
+
13
+ context 'actions' do
14
+ it { described_class.new.should respond_to :api_methods_in }
15
+
16
+ it 'dynamically adds actions inspection to classes inheriting from api' do
17
+ repos.should respond_to :actions
18
+ repos.new.should respond_to :actions
19
+ end
20
+
21
+ it 'ensures output contains api methods' do
22
+ methods = [ 'method_a', 'method_b']
23
+ repos.stub(:instance_methods).and_return methods
24
+ output = capture(:stdout) {
25
+ api.api_methods_in(repos)
26
+ }
27
+ output.should =~ /.*method_a.*/
28
+ output.should =~ /.*method_b.*/
29
+ end
9
30
  end
10
31
 
11
- it "should stringify all the keys inside nested hash" do
12
- actual = api.send(:_normalize_params_keys, @params)
13
- expected = { 'a' => { 'b'=> { 'c' => 1 }, 'd' => [ 'a', { 'e'=> 2 }] } }
14
- actual.should == expected
32
+ context '_normalize_params_keys' do
33
+ before do
34
+ @params = { 'a' => { :b => { 'c' => 1 }, 'd' => [ 'a', { :e => 2 }] } }
35
+ end
36
+
37
+ it "should stringify all the keys inside nested hash" do
38
+ actual = api.send(:_normalize_params_keys, @params)
39
+ expected = { 'a' => { 'b'=> { 'c' => 1 }, 'd' => [ 'a', { 'e'=> 2 }] } }
40
+ actual.should be_eql expected
41
+ end
15
42
  end
16
43
 
17
- it "should filter param keys" do
18
- valid = ['a', 'b', 'e']
19
- hash = {'a' => 1, 'b' => 3, 'c' => 2, 'd'=> 4, 'e' => 5 }
20
- actual = api.send(:_filter_params_keys, valid, hash)
21
- expected = {'a' => 1, 'b' => 3, 'e' => 5 }
22
- actual.should == expected
44
+ context '_filter_params_key' do
45
+ it "should remove non valid param keys" do
46
+ valid = ['a', 'b', 'e']
47
+ hash = {'a' => 1, 'b' => 3, 'c' => 2, 'd'=> 4, 'e' => 5 }
48
+ actual = api.send(:_filter_params_keys, valid, hash)
49
+ expected = {'a' => 1, 'b' => 3, 'e' => 5 }
50
+ actual.should be_eql expected
51
+ end
23
52
  end
24
53
 
25
- end
54
+ end # Github::API
@@ -15,15 +15,15 @@ describe Github::PagedRequest do
15
15
  it 'sets default per_page when only custom page passed' do
16
16
  Github.stub_chain(:api_client, :per_page).and_return nil
17
17
  Github.stub_chain(:api_client, :get).and_return nil
18
- Github::PagedRequest.page_request path, {'page' => 3}
18
+ Github::PagedRequest.page_request path, {'page' => 3, 'per_page' => -1}
19
19
  Github::PagedRequest.page.should eq 3
20
- Github::PagedRequest.per_page.should eq 25
20
+ Github::PagedRequest.per_page.should eq 30
21
21
  end
22
22
 
23
23
  it 'sets default page when only custom per_page passed' do
24
24
  Github.stub_chain(:api_client, :page).and_return nil
25
25
  Github.stub_chain(:api_client, :get).and_return nil
26
- Github::PagedRequest.page_request path, {'per_page' => 33}
26
+ Github::PagedRequest.page_request path, {'per_page' => 33, 'page' => -1}
27
27
  Github::PagedRequest.page.should eq 1
28
28
  Github::PagedRequest.per_page.should eq 33
29
29
  end
@@ -0,0 +1,14 @@
1
+ require 'stringio'
2
+
3
+ # Allow to capture stdout, stderr inside string for comparisons
4
+ def capture(*streams)
5
+ streams.map! { |stream| stream.to_s }
6
+ begin
7
+ result = StringIO.new
8
+ streams.each { |stream| eval "$#{stream} = result" }
9
+ yield
10
+ ensure
11
+ streams.each { |stream| eval("$#{stream} = #{stream.upcase}")}
12
+ end
13
+ result.string
14
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: github_api
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.0
5
+ version: 0.4.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Piotr Murach
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-01-14 00:00:00 +00:00
13
+ date: 2012-01-18 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -189,7 +189,7 @@ files:
189
189
  - features/step_definitions/github_api_steps.rb
190
190
  - features/support/env.rb
191
191
  - features/support/vcr.rb
192
- - lib/github_api/api/utils.rb
192
+ - lib/github_api/api/actions.rb
193
193
  - lib/github_api/api.rb
194
194
  - lib/github_api/authorization.rb
195
195
  - lib/github_api/authorizations.rb
@@ -346,6 +346,7 @@ files:
346
346
  - spec/README.rdoc
347
347
  - spec/spec_helper.rb
348
348
  - spec/support/base.rb
349
+ - spec/support/file_ops.rb
349
350
  - spec/support/github_api_shared_examples.rb
350
351
  - README.md
351
352
  - LICENSE.txt
@@ -1,9 +0,0 @@
1
- module Github
2
- class API
3
- module Utils
4
-
5
- private
6
-
7
- end
8
- end
9
- end