github_api 0.3.9 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +331 -0
- data/features/README.rdoc +26 -0
- data/features/cassettes/repos/branches.yml +79 -0
- data/features/cassettes/repos/tags.yml +167 -0
- data/features/cassettes/repos/teams.yml +38 -0
- data/features/repos.feature +28 -0
- data/features/step_definitions/common_steps.rb +11 -0
- data/features/step_definitions/github_api_steps.rb +23 -0
- data/features/support/vcr.rb +6 -0
- data/lib/github_api.rb +12 -2
- data/lib/github_api/api.rb +9 -5
- data/lib/github_api/connection.rb +4 -4
- data/lib/github_api/constants.rb +38 -0
- data/lib/github_api/error.rb +5 -2
- data/lib/github_api/page_iterator.rb +118 -0
- data/lib/github_api/page_links.rb +45 -0
- data/lib/github_api/paged_request.rb +34 -0
- data/lib/github_api/repos.rb +2 -2
- data/lib/github_api/response/helpers.rb +8 -1
- data/lib/github_api/response/raise_error.rb +3 -0
- data/lib/github_api/result.rb +86 -8
- data/lib/github_api/utils/url.rb +46 -0
- data/lib/github_api/version.rb +2 -2
- data/spec/README.rdoc +4 -0
- data/spec/github/page_iterator_spec.rb +189 -0
- data/spec/github/page_links_spec.rb +34 -0
- data/spec/github/paged_request_spec.rb +38 -0
- data/spec/github/response/helpers_spec.rb +18 -0
- data/spec/github/result_spec.rb +67 -4
- data/spec/github/utils/url_spec.rb +36 -0
- metadata +37 -10
- data/README.rdoc +0 -235
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Github::PageLinks do
|
4
|
+
|
5
|
+
let(:link) {
|
6
|
+
"<https://api.github.com/users/wycats/repos?page=4&per_page=20>; rel=\"next\", <https://api.github.com/users/wycats/repos?page=6&per_page=20>; rel=\"last\", <https://api.github.com/users/wycats/repos?page=1&per_page=20>; rel=\"first\", <https://api.github.com/users/wycats/repos?page=2&per_page=20>; rel=\"prev\""
|
7
|
+
}
|
8
|
+
let(:response_headers) { { 'Link' => link } }
|
9
|
+
|
10
|
+
let(:first) { "https://api.github.com/users/wycats/repos?page=1&per_page=20" }
|
11
|
+
let(:last) { "https://api.github.com/users/wycats/repos?page=6&per_page=20" }
|
12
|
+
let(:prev) { "https://api.github.com/users/wycats/repos?page=2&per_page=20" }
|
13
|
+
let(:nexxt) { "https://api.github.com/users/wycats/repos?page=4&per_page=20" }
|
14
|
+
|
15
|
+
context 'build page links instance' do
|
16
|
+
|
17
|
+
it 'parses first link successfully' do
|
18
|
+
Github::PageLinks.new(response_headers).first.should eql first
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'parses last link successfully' do
|
22
|
+
Github::PageLinks.new(response_headers).last.should eql last
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'parses next link successfully' do
|
26
|
+
Github::PageLinks.new(response_headers).next.should eql nexxt
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'parses prev link successfully' do
|
30
|
+
Github::PageLinks.new(response_headers).prev.should eql prev
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end # Github::PageLinks
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Github::PagedRequest do
|
4
|
+
|
5
|
+
it { described_class.constants.should include :FIRST_PAGE }
|
6
|
+
it { described_class.constants.should include :PER_PAGE }
|
7
|
+
|
8
|
+
context 'page_request' do
|
9
|
+
let(:path) { '/repos' }
|
10
|
+
|
11
|
+
before do
|
12
|
+
Github.new
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'sets default per_page when only custom page passed' do
|
16
|
+
Github.stub_chain(:api_client, :per_page).and_return nil
|
17
|
+
Github.stub_chain(:api_client, :get).and_return nil
|
18
|
+
Github::PagedRequest.page_request path, {'page' => 3}
|
19
|
+
Github::PagedRequest.page.should eq 3
|
20
|
+
Github::PagedRequest.per_page.should eq 25
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'sets default page when only custom per_page passed' do
|
24
|
+
Github.stub_chain(:api_client, :page).and_return nil
|
25
|
+
Github.stub_chain(:api_client, :get).and_return nil
|
26
|
+
Github::PagedRequest.page_request path, {'per_page' => 33}
|
27
|
+
Github::PagedRequest.page.should eq 1
|
28
|
+
Github::PagedRequest.per_page.should eq 33
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'sends get request with passed parameters' do
|
32
|
+
Github.stub(:api_client).and_return Github::Client
|
33
|
+
Github::Client.should_receive(:get).with(path, 'page' => 2, 'per_page' => 33)
|
34
|
+
Github::PagedRequest.page_request path, {'page' => 2, 'per_page' => 33}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end # Github::PagedRequest
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Github::Response::Helpers do
|
4
|
+
|
5
|
+
let(:env) { { :body => [1,2,3] } }
|
6
|
+
let(:instance) { described_class.new }
|
7
|
+
|
8
|
+
it 'includes result helper methods' do
|
9
|
+
res = instance.on_complete(env)
|
10
|
+
res[:body].class.included_modules.should include Github::Result
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should extend result with environemnt getter' do
|
14
|
+
res = instance.on_complete(env)
|
15
|
+
res[:body].should respond_to :env
|
16
|
+
end
|
17
|
+
|
18
|
+
end # Github::Response::Helpers
|
data/spec/github/result_spec.rb
CHANGED
@@ -3,7 +3,12 @@ require 'spec_helper'
|
|
3
3
|
describe Github::Result do
|
4
4
|
|
5
5
|
let(:github) { Github.new }
|
6
|
+
let(:user) { 'wycats' }
|
6
7
|
let(:res) { github.events.public }
|
8
|
+
let(:pages) { ['1', '5', '6'] }
|
9
|
+
let(:link) {
|
10
|
+
"<https://api.github.com/users/wycats/repos?page=6&per_page=20>; rel=\"last\", <https://api.github.com/users/wycats/repos?page=1&per_page=20>; rel=\"first\""
|
11
|
+
}
|
7
12
|
|
8
13
|
before do
|
9
14
|
stub_get("/events").
|
@@ -12,8 +17,24 @@ describe Github::Result do
|
|
12
17
|
:headers => {
|
13
18
|
:content_type => "application/json; charset=utf-8",
|
14
19
|
'X-RateLimit-Remaining' => '4999',
|
15
|
-
'
|
20
|
+
'X-RateLimit-Limit' => '5000',
|
21
|
+
'content-length' => '344',
|
22
|
+
'Link' => link
|
16
23
|
})
|
24
|
+
|
25
|
+
['1', '5', '6'].each do |page|
|
26
|
+
stub_get("/users/#{user}/repos").
|
27
|
+
with(:query => {'per_page' => '20', 'page' => page}).
|
28
|
+
to_return(:body => fixture('events/events.json'),
|
29
|
+
:status => 200,
|
30
|
+
:headers => {
|
31
|
+
:content_type => "application/json; charset=utf-8",
|
32
|
+
'X-RateLimit-Remaining' => '4999',
|
33
|
+
'X-RateLimit-Limit' => '5000',
|
34
|
+
'content-length' => '344',
|
35
|
+
'Link' => link
|
36
|
+
})
|
37
|
+
end
|
17
38
|
end
|
18
39
|
|
19
40
|
it "should read response content_type " do
|
@@ -24,11 +45,15 @@ describe Github::Result do
|
|
24
45
|
res.content_length.should match '344'
|
25
46
|
end
|
26
47
|
|
27
|
-
it "should read response ratelimit" do
|
28
|
-
res.
|
48
|
+
it "should read response ratelimit limit" do
|
49
|
+
res.ratelimit_limit.should == '5000'
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should read response ratelimit remaining" do
|
53
|
+
res.ratelimit_remaining.should == '4999'
|
29
54
|
end
|
30
55
|
|
31
|
-
it "should read response
|
56
|
+
it "should read response status" do
|
32
57
|
res.status.should be 200
|
33
58
|
end
|
34
59
|
|
@@ -40,4 +65,42 @@ describe Github::Result do
|
|
40
65
|
res.body.should_not be_empty
|
41
66
|
end
|
42
67
|
|
68
|
+
context "pagination methods" do
|
69
|
+
let(:env) { {:response_headers => {}}}
|
70
|
+
let(:iterator) { Github::PageIterator.new(env) }
|
71
|
+
let(:items) { [] }
|
72
|
+
|
73
|
+
before do
|
74
|
+
described_class.stub(:page_iterator).and_return iterator
|
75
|
+
@items.stub(:env).and_return env
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should respond to links" do
|
79
|
+
res.links.should be_a Github::PageLinks
|
80
|
+
end
|
81
|
+
|
82
|
+
%w[ next prev ].each do |link|
|
83
|
+
it "should return #{link} page if exists" do
|
84
|
+
res.send(:"#{link}_page").should eq @items
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
%w[ first last].each do |link|
|
89
|
+
it "should return #{link} page if exists" do
|
90
|
+
res.send(:"#{link}_page").should_not be_empty
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'finds single page successfully' do
|
95
|
+
iterator.stub(:get_page).and_return res
|
96
|
+
res.page(5).should eq res
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'checks if there are more pages' do
|
100
|
+
res.should_receive(:has_next_page?).and_return true
|
101
|
+
res.has_next_page?.should be_true
|
102
|
+
end
|
103
|
+
|
104
|
+
end # pagination
|
105
|
+
|
43
106
|
end # Github::Result
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Github::Utils::Url do
|
4
|
+
|
5
|
+
it 'escapes correctly' do
|
6
|
+
described_class.escape('<html>').should eql '%3Chtml%3E'
|
7
|
+
described_class.escape('a space').should eql 'a+space'
|
8
|
+
described_class.escape('\\/?}!@#$%^&*()').should eql '%5C%2F%3F%7D%21%40%23%24%25%5E%26%2A%28%29'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'unescapes correctly' do
|
12
|
+
described_class.unescape('%3Chtml%3E').should eql '<html>'
|
13
|
+
described_class.unescape('a+space').should eql 'a space'
|
14
|
+
described_class.unescape('%5C%2F%3F%7D%21%40%23%24%25%5E%26%2A%28%29').should eql '\\/?}!@#$%^&*()'
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'parses query strings correctly' do
|
18
|
+
it { described_class.parse_query("a=b").should eq 'a' => 'b' }
|
19
|
+
it { described_class.parse_query("a=b&a=c").should eq 'a' => ['b','c'] }
|
20
|
+
it { described_class.parse_query("a=b&c=d").should eq 'a' => 'b', 'c' => 'd' }
|
21
|
+
it { described_class.parse_query("a+b=%28c%29").should eq 'a b' => '(c)' }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'parse_query_for_param' do
|
25
|
+
it 'returns nil if cannot find parameter' do
|
26
|
+
described_class.parse_query_for_param("param1=a;param2=b", 'param3').
|
27
|
+
should be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns value for given parameter name' do
|
31
|
+
described_class.parse_query_for_param("param1=a;param2=b", 'param1').
|
32
|
+
should eq 'a'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end # Github::Utils::Url
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: github_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.4.0
|
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-
|
13
|
+
date: 2012-01-14 00:00:00 +00:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
requirements:
|
66
66
|
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 2.
|
68
|
+
version: 2.8.0
|
69
69
|
type: :development
|
70
70
|
version_requirements: *id005
|
71
71
|
- !ruby/object:Gem::Dependency
|
@@ -124,29 +124,29 @@ dependencies:
|
|
124
124
|
type: :development
|
125
125
|
version_requirements: *id010
|
126
126
|
- !ruby/object:Gem::Dependency
|
127
|
-
name:
|
127
|
+
name: vcr
|
128
128
|
prerelease: false
|
129
129
|
requirement: &id011 !ruby/object:Gem::Requirement
|
130
130
|
none: false
|
131
131
|
requirements:
|
132
132
|
- - ~>
|
133
133
|
- !ruby/object:Gem::Version
|
134
|
-
version:
|
134
|
+
version: 1.11.3
|
135
135
|
type: :development
|
136
136
|
version_requirements: *id011
|
137
137
|
- !ruby/object:Gem::Dependency
|
138
|
-
name:
|
138
|
+
name: simplecov
|
139
139
|
prerelease: false
|
140
140
|
requirement: &id012 !ruby/object:Gem::Requirement
|
141
141
|
none: false
|
142
142
|
requirements:
|
143
|
-
- -
|
143
|
+
- - ~>
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: "0"
|
145
|
+
version: "0.4"
|
146
146
|
type: :development
|
147
147
|
version_requirements: *id012
|
148
148
|
- !ruby/object:Gem::Dependency
|
149
|
-
name: guard-
|
149
|
+
name: guard-rspec
|
150
150
|
prerelease: false
|
151
151
|
requirement: &id013 !ruby/object:Gem::Requirement
|
152
152
|
none: false
|
@@ -156,6 +156,17 @@ dependencies:
|
|
156
156
|
version: "0"
|
157
157
|
type: :development
|
158
158
|
version_requirements: *id013
|
159
|
+
- !ruby/object:Gem::Dependency
|
160
|
+
name: guard-cucumber
|
161
|
+
prerelease: false
|
162
|
+
requirement: &id014 !ruby/object:Gem::Requirement
|
163
|
+
none: false
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: "0"
|
168
|
+
type: :development
|
169
|
+
version_requirements: *id014
|
159
170
|
description: " Ruby wrapper that supports all of the GitHub API v3 methods(nearly 200). It's build in a modular way, that is, you can either instantiate the whole api wrapper Github.new or use parts of it e.i. Github::Repos.new if working solely with repositories is your main concern. "
|
160
171
|
email: ""
|
161
172
|
executables: []
|
@@ -166,9 +177,15 @@ extra_rdoc_files: []
|
|
166
177
|
|
167
178
|
files:
|
168
179
|
- Rakefile
|
180
|
+
- features/cassettes/repos/branches.yml
|
181
|
+
- features/cassettes/repos/tags.yml
|
182
|
+
- features/cassettes/repos/teams.yml
|
169
183
|
- features/github_api.feature
|
170
184
|
- features/options.feature
|
171
185
|
- features/pagination.feature
|
186
|
+
- features/README.rdoc
|
187
|
+
- features/repos.feature
|
188
|
+
- features/step_definitions/common_steps.rb
|
172
189
|
- features/step_definitions/github_api_steps.rb
|
173
190
|
- features/support/env.rb
|
174
191
|
- features/support/vcr.rb
|
@@ -181,6 +198,7 @@ files:
|
|
181
198
|
- lib/github_api/compatibility.rb
|
182
199
|
- lib/github_api/configuration.rb
|
183
200
|
- lib/github_api/connection.rb
|
201
|
+
- lib/github_api/constants.rb
|
184
202
|
- lib/github_api/core_ext/array.rb
|
185
203
|
- lib/github_api/core_ext/hash.rb
|
186
204
|
- lib/github_api/error.rb
|
@@ -202,6 +220,9 @@ files:
|
|
202
220
|
- lib/github_api/orgs/members.rb
|
203
221
|
- lib/github_api/orgs/teams.rb
|
204
222
|
- lib/github_api/orgs.rb
|
223
|
+
- lib/github_api/page_iterator.rb
|
224
|
+
- lib/github_api/page_links.rb
|
225
|
+
- lib/github_api/paged_request.rb
|
205
226
|
- lib/github_api/pull_requests/comments.rb
|
206
227
|
- lib/github_api/pull_requests.rb
|
207
228
|
- lib/github_api/repos/collaborators.rb
|
@@ -227,6 +248,7 @@ files:
|
|
227
248
|
- lib/github_api/users/followers.rb
|
228
249
|
- lib/github_api/users/keys.rb
|
229
250
|
- lib/github_api/users.rb
|
251
|
+
- lib/github_api/utils/url.rb
|
230
252
|
- lib/github_api/version.rb
|
231
253
|
- lib/github_api.rb
|
232
254
|
- spec/coverage_adapter.rb
|
@@ -304,6 +326,9 @@ files:
|
|
304
326
|
- spec/github/orgs/members_spec.rb
|
305
327
|
- spec/github/orgs/teams_spec.rb
|
306
328
|
- spec/github/orgs_spec.rb
|
329
|
+
- spec/github/page_iterator_spec.rb
|
330
|
+
- spec/github/page_links_spec.rb
|
331
|
+
- spec/github/paged_request_spec.rb
|
307
332
|
- spec/github/repos/collaborators_spec.rb
|
308
333
|
- spec/github/repos/commits_spec.rb
|
309
334
|
- spec/github/repos/downloads_spec.rb
|
@@ -313,14 +338,16 @@ files:
|
|
313
338
|
- spec/github/repos/pub_sub_hubbub_spec.rb
|
314
339
|
- spec/github/repos/watching_spec.rb
|
315
340
|
- spec/github/repos_spec.rb
|
341
|
+
- spec/github/response/helpers_spec.rb
|
316
342
|
- spec/github/result_spec.rb
|
317
343
|
- spec/github/users_spec.rb
|
344
|
+
- spec/github/utils/url_spec.rb
|
318
345
|
- spec/github_spec.rb
|
319
346
|
- spec/README.rdoc
|
320
347
|
- spec/spec_helper.rb
|
321
348
|
- spec/support/base.rb
|
322
349
|
- spec/support/github_api_shared_examples.rb
|
323
|
-
- README.
|
350
|
+
- README.md
|
324
351
|
- LICENSE.txt
|
325
352
|
has_rdoc: true
|
326
353
|
homepage: https://github.com/peter-murach/github
|
data/README.rdoc
DELETED
@@ -1,235 +0,0 @@
|
|
1
|
-
= GithubAPI {<img src="http://travis-ci.org/peter-murach/github.png?branch=master" />}[http://travis-ci.org/peter-murach/github] {<img src="https://gemnasium.com/peter-murach/github.png?travis" />}[https://gemnasium.com/peter-murach/github]
|
2
|
-
|
3
|
-
Wiki[https://github.com/peter-murach/github/wiki] | RDocs[http://rubydoc.info/github/peter-murach/github/master/frames]
|
4
|
-
|
5
|
-
A Ruby wrapper for the GitHub REST API v3.
|
6
|
-
|
7
|
-
Supports all the API methods(nearly 200). It's build in a modular way, that is, you can either instantiate the whole api wrapper Github.new or use parts of it e.i. Github::Repos.new if working solely with repositories is your main concern.
|
8
|
-
|
9
|
-
== Installation
|
10
|
-
|
11
|
-
Grab the gem by issuing
|
12
|
-
|
13
|
-
gem install github_api
|
14
|
-
|
15
|
-
or in your Gemfile
|
16
|
-
|
17
|
-
gem "github_api"
|
18
|
-
|
19
|
-
== Usage
|
20
|
-
|
21
|
-
Create a new client instance
|
22
|
-
|
23
|
-
@github = Github.new
|
24
|
-
|
25
|
-
At this stage you can also supply various configuration parameters, such as :user, :repo, :org, :oauth_token, :login, :password or :basic_auth which are used throughout the API
|
26
|
-
|
27
|
-
@github = Github.new :user => 'peter-murach', :repo => 'github-api'
|
28
|
-
|
29
|
-
or
|
30
|
-
|
31
|
-
@github = Github.new do |opts|
|
32
|
-
opts.user = 'peter-murach'
|
33
|
-
opts.repo = 'github-api'
|
34
|
-
end
|
35
|
-
|
36
|
-
You can authenticate either using OAuth authentication convenience methods(see section OAuth) or through basic authentication by passing your login and password credentials
|
37
|
-
|
38
|
-
@github = Github.new :login => 'peter-murach', :password => '...'
|
39
|
-
|
40
|
-
or use convenience method:
|
41
|
-
|
42
|
-
@github = Github.new :basic_auth => 'login:password'
|
43
|
-
|
44
|
-
You can interact with GitHub interface, for example repositories, by issueing following calls
|
45
|
-
|
46
|
-
@github.repos.commits
|
47
|
-
@github.repos.branches
|
48
|
-
@github.repos.contributors
|
49
|
-
|
50
|
-
The code base is modular and allows for you to work specifically with a given part of GitHub API e.g. repositories
|
51
|
-
|
52
|
-
@repos = Github::Repos.new
|
53
|
-
@repos.branches 'peter-murach', 'github'
|
54
|
-
|
55
|
-
or
|
56
|
-
|
57
|
-
@repos = Github::Repos.new :user => 'peter-murach', :repo => 'github'
|
58
|
-
@repos.branches
|
59
|
-
|
60
|
-
The response is of type [Hashie::Mash] and allows to traverse all the json response attributes like method calls e.i.
|
61
|
-
|
62
|
-
@repos = Github::Repos.new :user => 'peter-murach', :repo => 'github'
|
63
|
-
@repos.branches do |branch|
|
64
|
-
puts branch.name
|
65
|
-
end
|
66
|
-
|
67
|
-
== API
|
68
|
-
|
69
|
-
Main API methods are grouped into the following classes that can be instantiated on their own
|
70
|
-
|
71
|
-
Github - full API access
|
72
|
-
Github::Gists
|
73
|
-
Github::GitData
|
74
|
-
Github::Issues
|
75
|
-
Github::Orgs
|
76
|
-
Github::PullRequests
|
77
|
-
Github::Repos
|
78
|
-
Github::Users
|
79
|
-
Github::Events
|
80
|
-
Github::Authorizations
|
81
|
-
|
82
|
-
Some parts of GitHub API v3 require you to be autheticated, for instance the following are examples of APIs only for the authenticated user
|
83
|
-
|
84
|
-
Github::Users::Emails
|
85
|
-
Github::Users::Keys
|
86
|
-
|
87
|
-
All method calls form ruby like sentences and allow for intuitive api navigation, for instance
|
88
|
-
|
89
|
-
@github = Github.new :oauth_token => '...'
|
90
|
-
@github.users.following 'wycats' # => returns users that 'wycats' is following
|
91
|
-
@github.users.following 'wycats' # => returns true if following, otherwise false
|
92
|
-
|
93
|
-
For specification on all available methods go to http://developer.github.com/v3/ or
|
94
|
-
read the rdoc, all methods are documented there with examples of usage.
|
95
|
-
|
96
|
-
== Inputs
|
97
|
-
|
98
|
-
Some API methods apart from required parameters such as username, repository name
|
99
|
-
or organisation name, allow you to switch the way the data is returned to you, for instance
|
100
|
-
|
101
|
-
@github = Github.new
|
102
|
-
@github.git_data.tree 'peter-murach', 'github', 'c18647b75d72f19c1e0cc8af031e5d833b7f12ea' # => gets a tree
|
103
|
-
|
104
|
-
@github.git_data.tree 'peter-murach', 'github', 'c18647b75d72f19c1e0cc8af031e5d833b7f12ea', :recursive => true # => gets a whole tree recursively
|
105
|
-
|
106
|
-
by passing a block you can iterate over the file tree
|
107
|
-
|
108
|
-
@github.git_data.tree 'peter-murach', 'github', 'c18647b75d72f19c1e0cc8af031e5d833b7f12ea', :recursive => true do |file|
|
109
|
-
puts file.path
|
110
|
-
end
|
111
|
-
|
112
|
-
== OAuth
|
113
|
-
|
114
|
-
In order to authenticate the user through OAuth2 on GitHub you need to
|
115
|
-
|
116
|
-
* visit https://github.com/account/applications/ and register your app
|
117
|
-
|
118
|
-
* authorize your credentials https://github.com/login/oauth/authorize
|
119
|
-
You can use convenience methods to help you achieve this that come with this gem:
|
120
|
-
|
121
|
-
@github = Github.new :client_id => '...', :client_secret => '...'
|
122
|
-
@github.authorize_url :redirect_uri => 'http://localhost', :scope => 'repo'
|
123
|
-
# => "https://github.com/login/oauth/authorize?scope=repo&response_type=code&client_id='...'&redirect_uri=http%3A%2F%2Flocalhost"
|
124
|
-
|
125
|
-
After you get your authorization code, call to receive your access_token
|
126
|
-
|
127
|
-
token = github.get_token( authorization_code )
|
128
|
-
|
129
|
-
Once you have your access token, configure your github instance following instructions under Configuration.
|
130
|
-
|
131
|
-
Alternatively you can use OAuth Authorizations API. For instance, to create access token through GitHub API do following
|
132
|
-
|
133
|
-
@github = Github.new :basic_auth => 'login:password'
|
134
|
-
@github.oauth.create_authorization 'scopes' => ['repo']
|
135
|
-
|
136
|
-
You can add more than one scope from the <tt>user</tt>, <tt>public_repo</tt>, <tt>repo</tt>, <tt>gist</tt> or leave the scopes parameter out, in which case, the default read-only access will be assumed(includes public user profile info, public repo info, and gists).
|
137
|
-
|
138
|
-
== MIME Types
|
139
|
-
|
140
|
-
Issues, PullRequests and few other API leverage custom mime types which are <tt>:json</tt>, <tt>:blob</tt>, <tt>:raw</tt>, <tt>:text</tt>, <tt>:html</tt>, <tt>:full</tt>. By default <tt>:raw</tt> is used.
|
141
|
-
|
142
|
-
In order to pass a mime type with your request do
|
143
|
-
|
144
|
-
@github = Github.new
|
145
|
-
@github.pull_requests.pull_requests 'peter-murach', 'github', :mime_type => :full
|
146
|
-
|
147
|
-
Your header will contain 'Accept: "application/vnd.github-pull.full+json"' which in turn returns raw, text and html representations in response body.
|
148
|
-
|
149
|
-
== Configuration
|
150
|
-
|
151
|
-
Certain methods require authentication. To get your GitHub OAuth v2 credentials,
|
152
|
-
register an app at https://github.com/account/applications/
|
153
|
-
|
154
|
-
Github.configure do |config|
|
155
|
-
config.oauth_token = YOUR_OAUTH_ACCESS_TOKEN
|
156
|
-
config.basic_auth = 'login:password'
|
157
|
-
end
|
158
|
-
|
159
|
-
or
|
160
|
-
|
161
|
-
Github.new(:oauth_token => YOUR_OAUTH_TOKEN)
|
162
|
-
Github.new(:basic_auth => 'login:password)
|
163
|
-
|
164
|
-
All parameters can be overwirtten as per method call. By passing parameters hash...
|
165
|
-
|
166
|
-
== Caching
|
167
|
-
|
168
|
-
Each <tt>get</tt> request by default is not going to be cached. In order to set the cache do... If no cache type is provided a default memoization is done.
|
169
|
-
|
170
|
-
Github.cache do...
|
171
|
-
|
172
|
-
== Examples
|
173
|
-
|
174
|
-
Some api methods require input parameters, these are added simply as a hash properties, for instance
|
175
|
-
|
176
|
-
@issues = Github::Issues.new :user => 'peter-murach', :repo => 'github-api'
|
177
|
-
@issues.milestones :state => 'open', :sort => 'due_date', :direction => 'asc'
|
178
|
-
|
179
|
-
Other methods may require inputs as an array of strings
|
180
|
-
|
181
|
-
@users = Github::Users.new :oauth_token => '...'
|
182
|
-
@users.add_email 'email1', 'email2', ..., 'emailn' # => Adds emails to the authenticated user
|
183
|
-
|
184
|
-
If a method returns a collection, you can iterator over it by supplying a block parameter,
|
185
|
-
|
186
|
-
@issues = Github::Issues.new :user => 'peter-murach', :repo => 'github-api'
|
187
|
-
@issues.events do |event|
|
188
|
-
puts event.actor.login
|
189
|
-
end
|
190
|
-
|
191
|
-
Query requests instead of http responses return boolean values
|
192
|
-
|
193
|
-
@github = Github.new
|
194
|
-
@github.orgs.public_member? 'github', 'technoweenie' # => true
|
195
|
-
|
196
|
-
== Rails Example
|
197
|
-
|
198
|
-
A Rails controller that allows a user to authorize their GitHub account and then perform request.
|
199
|
-
|
200
|
-
class GithubController < ApplicationController
|
201
|
-
|
202
|
-
def authorize
|
203
|
-
github = Github.new :client_id => '...', :client_secret => '...'
|
204
|
-
address = github.authorize_url :redirect_uri => 'http://...', :scope => 'repo'
|
205
|
-
redirect_to address
|
206
|
-
end
|
207
|
-
|
208
|
-
def callback
|
209
|
-
authorization_code = params[:code]
|
210
|
-
token = github.get_token authorization_code
|
211
|
-
access_token = token.token
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
== TODO
|
216
|
-
|
217
|
-
* Add request caching - local filestore?, http caching?.
|
218
|
-
* Add response processing methods
|
219
|
-
* Add response set helper methods e.i. pagination.
|
220
|
-
* Add DSL falvoured api access
|
221
|
-
|
222
|
-
== Contributing to github
|
223
|
-
|
224
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
225
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
226
|
-
* Fork the project
|
227
|
-
* Start a feature/bugfix branch
|
228
|
-
* Commit and push until you are happy with your contribution
|
229
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
230
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
231
|
-
|
232
|
-
== Copyright
|
233
|
-
|
234
|
-
Copyright (c) 2011 Piotr Murach. See LICENSE.txt for
|
235
|
-
further details.
|