github_api 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,7 @@ You can authenticate either using OAuth authentication convenience methods(see s
28
28
 
29
29
  @github = Github.new :login => 'peter-murach', :password => '...'
30
30
 
31
- or use convenience method:
31
+ or use convenience method:
32
32
 
33
33
  @github = Github.new :basic_auth => 'login:password'
34
34
 
@@ -67,6 +67,7 @@ Main API methods are grouped into the following classes that can be instantiated
67
67
  Github::PullRequests
68
68
  Github::Repos
69
69
  Github::Users
70
+ Github::Authorizations
70
71
 
71
72
  Some parts of GitHub API v3 require you to be autheticated, for instance the following are examples of APIs only for the authenticated user
72
73
 
@@ -117,6 +118,13 @@ In order to authenticate the user through OAuth2 on GitHub you need to
117
118
 
118
119
  Once you have your access token, configure your github instance following instructions under Configuration.
119
120
 
121
+ Alternatively you can use OAuth Authorizations API. For instance, to create access token through GitHub API do following
122
+
123
+ @github = Github.new :basic_auth => 'login:password'
124
+ @github.oauth.create_authorization 'scopes' => ['repo']
125
+
126
+ 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).
127
+
120
128
  == MIME Types
121
129
 
122
130
  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.
@@ -53,6 +53,7 @@ module Github
53
53
  :Users => 'users',
54
54
  :CoreExt => 'core_ext',
55
55
  :MimeType => 'mime_type',
56
- :Authorization => 'authorization'
56
+ :Authorization => 'authorization',
57
+ :Authorizations => 'authorizations'
57
58
 
58
59
  end # Github
@@ -42,6 +42,11 @@ module Github
42
42
  @client.auth_code.get_token(authorization_code, params)
43
43
  end
44
44
 
45
+ # Check whether authentication credentials are present
46
+ def authenticated?
47
+ basic_auth? || oauth_token? || (login? && password?)
48
+ end
49
+
45
50
  private
46
51
 
47
52
  def _verify_client # :nodoc:
@@ -0,0 +1,119 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Authorizations < API
5
+
6
+ VALID_AUTH_PARAM_NAMES = %w[
7
+ scopes
8
+ add_scopes
9
+ remove_scopes
10
+ ].freeze
11
+
12
+ # Creates new OAuth Authorizations API
13
+ def initialize(options = {})
14
+ super(options)
15
+ end
16
+
17
+ # List authorizations
18
+ #
19
+ # = Examples
20
+ # @github = Github.new :basic_auth => 'login:password'
21
+ # @github.oauth.authorizations
22
+ # @github.oauth.authorizations { |auth| ... }
23
+ #
24
+ def authorizations(params={})
25
+ _check_if_authenticated
26
+ _normalize_params_keys(params)
27
+
28
+ response = get("/authorizations", params)
29
+ return response unless block_given?
30
+ response.each { |el| yield el }
31
+ end
32
+ alias :auths :authorizations
33
+ alias :list_auths :authorizations
34
+ alias :list_authorizations :authorizations
35
+
36
+ # Get a single authorization
37
+ #
38
+ # = Examples
39
+ # @github = Github.new :basic_auth => 'login:password'
40
+ # @github.oauth.authorization 'authorization-id'
41
+ #
42
+ def authorization(authorization_id, params={})
43
+ _validate_presence_of(authorization_id)
44
+ _check_if_authenticated
45
+ _normalize_params_keys params
46
+
47
+ get "/authorizations/#{authorization_id}", params
48
+ end
49
+ alias :auth :authorization
50
+ alias :get_auth :authorization
51
+ alias :get_authorization :authorization
52
+
53
+ # Create a new authorization
54
+ #
55
+ # = Inputs
56
+ # * <tt>:scopes</tt> - Optional array - A list of scopes that this authorization is in.
57
+ # = Examples
58
+ # @github = Github.new :basic_auth => 'login:password'
59
+ # @github.oauth.create_authorization
60
+ # "scopes" => ["public_repo"]
61
+ #
62
+ def create_authorization(params={})
63
+ _check_if_authenticated
64
+ _normalize_params_keys(params)
65
+ _filter_params_keys(VALID_AUTH_PARAM_NAMES, params)
66
+
67
+ post("/authorizations", params)
68
+ end
69
+ alias :create_auth :create_authorization
70
+
71
+ # Update an existing authorization
72
+ #
73
+ # = Inputs
74
+ # * <tt>:scopes</tt> - Optional array - A list of scopes that this authorization is in.
75
+ # * <tt>:add_scopes</tt> - Optional array - A list of scopes to add to this authorization.
76
+ # * <tt>:remove_scopes</tt> - Optional array - A list of scopes to remove from this authorization.
77
+ #
78
+ # = Examples
79
+ # @github = Github.new :basic_auth => 'login:password'
80
+ # @github.oauth.update_authorization
81
+ # "add_scopes" => ["repo"],
82
+ #
83
+ def update_authorization(authorization_id, params={})
84
+ _check_if_authenticated
85
+ _validate_presence_of(authorization_id)
86
+
87
+ _normalize_params_keys(params)
88
+ _filter_params_keys(VALID_AUTH_PARAM_NAMES, params)
89
+
90
+ patch("/authorizations/#{authorization_id}", params)
91
+ end
92
+ alias :update_auth :update_authorization
93
+
94
+ # Delete an authorization
95
+ #
96
+ # = Examples
97
+ # @github.oauth.delete_authorization 'authorization-id'
98
+ #
99
+ def delete_authorization(authorization_id, params={})
100
+ _check_if_authenticated
101
+ _validate_presence_of(authorization_id)
102
+
103
+ _normalize_params_keys(params)
104
+ _filter_params_keys(VALID_AUTH_PARAM_NAMES, params)
105
+
106
+ delete("/authorizations/#{authorization_id}", params)
107
+ end
108
+ alias :delete_auth :delete_authorization
109
+ alias :remove_auth :delete_authorization
110
+ alias :remove_authorization :delete_authorization
111
+
112
+ private
113
+
114
+ def _check_if_authenticated
115
+ raise ArgumentError, 'You can only access authentication tokens through Basic Authentication' unless authenticated?
116
+ end
117
+
118
+ end # Authorizations
119
+ end # Github
@@ -39,5 +39,11 @@ module Github
39
39
  @users ||= _create_instance Github::Users, options
40
40
  end
41
41
 
42
+ # An API for users to manage their own tokens. You can only access your own
43
+ # tokens, and only through Basic Authentication.
44
+ def oauth(options = {})
45
+ @oauth ||= _create_instance Github::Authorizations, options
46
+ end
47
+
42
48
  end # Client
43
49
  end # Github
@@ -3,8 +3,8 @@
3
3
  module Github
4
4
  module VERSION
5
5
  MAJOR = 0
6
- MINOR = 2
7
- PATCH = 2
6
+ MINOR = 3
7
+ PATCH = 0
8
8
  BUILD = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.');
@@ -0,0 +1,14 @@
1
+ {
2
+ "id": 1,
3
+ "url": "https://api.github.com/authorizations/1",
4
+ "scopes": [
5
+ "public_repo"
6
+ ],
7
+ "token": "abc123",
8
+ "app": {
9
+ "url": "http://my-github-app.com",
10
+ "name": "my github app"
11
+ },
12
+ "updated_at": "2011-09-06T20:39:23Z",
13
+ "created_at": "2011-09-06T17:26:27Z"
14
+ }
@@ -0,0 +1,16 @@
1
+ [
2
+ {
3
+ "id": 1,
4
+ "url": "https://api.github.com/authorizations/1",
5
+ "scopes": [
6
+ "public_repo"
7
+ ],
8
+ "token": "abc123",
9
+ "app": {
10
+ "url": "http://my-github-app.com",
11
+ "name": "my github app"
12
+ },
13
+ "updated_at": "2011-09-06T20:39:23Z",
14
+ "created_at": "2011-09-06T17:26:27Z"
15
+ }
16
+ ]
@@ -0,0 +1,241 @@
1
+ require 'spec_helper'
2
+
3
+ describe Github::Authorizations do
4
+
5
+ let(:github) { Github.new }
6
+
7
+ before do
8
+ github.basic_auth = 'login:password'
9
+ end
10
+
11
+ after do
12
+ reset_authentication_for github
13
+ end
14
+
15
+ describe "authorizations" do
16
+ context "resource found" do
17
+ before do
18
+ stub_get("/authorizations").
19
+ to_return(:body => fixture('auths/authorizations.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
20
+ end
21
+
22
+
23
+ it "should fail to get resource without basic authentication" do
24
+ reset_authentication_for github
25
+ expect { github.oauth.authorizations }.to raise_error(ArgumentError)
26
+ end
27
+
28
+ it "should get the resources" do
29
+ github.oauth.authorizations
30
+ a_get("/authorizations").should have_been_made
31
+ end
32
+
33
+ it "should return array of resources" do
34
+ authorizations = github.oauth.authorizations
35
+ authorizations.should be_an Array
36
+ authorizations.should have(1).items
37
+ end
38
+
39
+ it "should be a mash type" do
40
+ authorizations = github.oauth.authorizations
41
+ authorizations.first.should be_a Hashie::Mash
42
+ end
43
+
44
+ it "should get authorization information" do
45
+ authorizations = github.oauth.authorizations
46
+ authorizations.first.token.should == 'abc123'
47
+ end
48
+
49
+ it "should yield to a block" do
50
+ github.oauth.should_receive(:authorizations).and_yield('web')
51
+ github.oauth.authorizations { |param| 'web' }
52
+ end
53
+ end
54
+
55
+ context "resource not found" do
56
+ before do
57
+ stub_get("/authorizations").
58
+ to_return(:body => "", :status => [404, "Not Found"])
59
+ end
60
+
61
+ it "should return 404 with a message 'Not Found'" do
62
+ expect { github.oauth.authorizations }.to raise_error(Github::ResourceNotFound)
63
+ end
64
+ end
65
+ end # authorizations
66
+
67
+ describe "authorization" do
68
+ let(:authorization_id) { 1 }
69
+
70
+ context "resource found" do
71
+ before do
72
+ stub_get("/authorizations/#{authorization_id}").
73
+ to_return(:body => fixture('auths/authorization.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
74
+ end
75
+
76
+ it "should fail to get resource without authorization id" do
77
+ expect { github.oauth.authorization nil }.to raise_error(ArgumentError)
78
+ end
79
+
80
+ it "should get the resource" do
81
+ github.oauth.authorization authorization_id
82
+ a_get("/authorizations/#{authorization_id}").should have_been_made
83
+ end
84
+
85
+ it "should get authorization information" do
86
+ authorization = github.oauth.authorization authorization_id
87
+ authorization.id.should == authorization_id
88
+ authorization.token.should == 'abc123'
89
+ end
90
+
91
+ it "should return mash" do
92
+ authorization = github.oauth.authorization authorization_id
93
+ authorization.should be_a Hashie::Mash
94
+ end
95
+ end
96
+
97
+ context "resource not found" do
98
+ before do
99
+ stub_get("/authorizations/#{authorization_id}").
100
+ to_return(:body => fixture('auths/authorization.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
101
+ end
102
+
103
+ it "should fail to retrive resource" do
104
+ expect {
105
+ github.oauth.authorization authorization_id
106
+ }.to raise_error(Github::ResourceNotFound)
107
+ end
108
+ end
109
+ end # authorization
110
+
111
+ describe "create_authorization" do
112
+ let(:inputs) { { :scopes => ['repo'] } }
113
+
114
+ context "resouce created" do
115
+
116
+ it "should fail to get resource without basic authentication" do
117
+ reset_authentication_for github
118
+ expect { github.oauth.create_authorization }.to raise_error(ArgumentError)
119
+ end
120
+
121
+ before do
122
+ stub_post("/authorizations").with(inputs).
123
+ to_return(:body => fixture('auths/authorization.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
124
+ end
125
+
126
+ it "should create resource successfully" do
127
+ github.oauth.create_authorization inputs
128
+ a_post("/authorizations").with(inputs).should have_been_made
129
+ end
130
+
131
+ it "should return the resource" do
132
+ authorization = github.oauth.create_authorization inputs
133
+ authorization.should be_a Hashie::Mash
134
+ end
135
+
136
+ it "should get the authorization information" do
137
+ authorization = github.oauth.create_authorization inputs
138
+ authorization.token.should == 'abc123'
139
+ end
140
+ end
141
+
142
+ context "failed to create resource" do
143
+ before do
144
+ stub_post("/authorizations").with(inputs).
145
+ to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
146
+
147
+ end
148
+
149
+ it "should fail to retrieve resource" do
150
+ expect {
151
+ github.oauth.create_authorization inputs
152
+ }.to raise_error(Github::ResourceNotFound)
153
+ end
154
+ end
155
+ end # create_authorization
156
+
157
+ describe "update_authorization" do
158
+ let(:authorization_id) { 1 }
159
+ let(:inputs) { { :add_scopes => ['repo'] } }
160
+
161
+ context "resouce updated" do
162
+
163
+ it "should fail to get resource without basic authentication" do
164
+ reset_authentication_for github
165
+ expect { github.oauth.update_authorization }.to raise_error(ArgumentError)
166
+ end
167
+
168
+ before do
169
+ stub_patch("/authorizations/#{authorization_id}").with(inputs).
170
+ to_return(:body => fixture('auths/authorization.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
171
+ end
172
+
173
+ it "should update resource successfully" do
174
+ github.oauth.update_authorization authorization_id, inputs
175
+ a_patch("/authorizations/#{authorization_id}").with(inputs).should have_been_made
176
+ end
177
+
178
+ it "should return the resource" do
179
+ authorization = github.oauth.update_authorization authorization_id, inputs
180
+ authorization.should be_a Hashie::Mash
181
+ end
182
+
183
+ it "should get the authorization information" do
184
+ authorization = github.oauth.update_authorization authorization_id, inputs
185
+ authorization.token.should == 'abc123'
186
+ end
187
+ end
188
+
189
+ context "failed to update resource" do
190
+ before do
191
+ stub_patch("/authorizations/#{authorization_id}").with(inputs).
192
+ to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
193
+
194
+ end
195
+
196
+ it "should fail to retrieve resource" do
197
+ expect {
198
+ github.oauth.update_authorization authorization_id, inputs
199
+ }.to raise_error(Github::ResourceNotFound)
200
+ end
201
+ end
202
+ end # update_authorization
203
+
204
+ describe "delete_authorization" do
205
+ let(:authorization_id) { 1 }
206
+ let(:inputs) { { :add_scopes => ['repo'] } }
207
+
208
+ context "resouce deleted" do
209
+
210
+ it "should fail to get resource without basic authentication" do
211
+ reset_authentication_for github
212
+ expect { github.oauth.delete_authorization nil }.to raise_error(ArgumentError)
213
+ end
214
+
215
+ before do
216
+ stub_delete("/authorizations/#{authorization_id}").
217
+ to_return(:body => '', :status => 204, :headers => {:content_type => "application/json; charset=utf-8"})
218
+ end
219
+
220
+ it "should delete resource successfully" do
221
+ github.oauth.delete_authorization authorization_id
222
+ a_delete("/authorizations/#{authorization_id}").should have_been_made
223
+ end
224
+ end
225
+
226
+ context "failed to create resource" do
227
+ before do
228
+ stub_delete("/authorizations/#{authorization_id}").
229
+ to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
230
+
231
+ end
232
+
233
+ it "should fail to retrieve resource" do
234
+ expect {
235
+ github.oauth.delete_authorization authorization_id
236
+ }.to raise_error(Github::ResourceNotFound)
237
+ end
238
+ end
239
+ end # delete_authorization
240
+
241
+ end # Github::Authorizations
@@ -66,6 +66,12 @@ end
66
66
 
67
67
  OAUTH_TOKEN = 'bafec72922f31fe86aacc8aca4261117f3bd62cf'
68
68
 
69
+ def reset_authentication_for(object)
70
+ ['basic_auth', 'oauth_token', 'login', 'password' ].each do |item|
71
+ object.send("#{item}=", nil)
72
+ end
73
+ end
74
+
69
75
  class Hash
70
76
  def except(*keys)
71
77
  cpy = self.dup
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 2
9
- version: 0.2.2
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Piotr Murach
@@ -26,9 +26,9 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  segments:
28
28
  - 1
29
- - 1
29
+ - 2
30
30
  - 0
31
- version: 1.1.0
31
+ version: 1.2.0
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
@@ -196,6 +196,7 @@ files:
196
196
  - lib/github_api/api/utils.rb
197
197
  - lib/github_api/api.rb
198
198
  - lib/github_api/authorization.rb
199
+ - lib/github_api/authorizations.rb
199
200
  - lib/github_api/cache_control.rb
200
201
  - lib/github_api/client.rb
201
202
  - lib/github_api/compatibility.rb
@@ -246,6 +247,8 @@ files:
246
247
  - lib/github_api/users.rb
247
248
  - lib/github_api/version.rb
248
249
  - lib/github_api.rb
250
+ - spec/fixtures/auths/authorization.json
251
+ - spec/fixtures/auths/authorizations.json
249
252
  - spec/fixtures/orgs/members.json
250
253
  - spec/fixtures/orgs/org.json
251
254
  - spec/fixtures/orgs/orgs.json
@@ -278,6 +281,7 @@ files:
278
281
  - spec/fixtures/repos/watchers.json
279
282
  - spec/github/api_spec.rb
280
283
  - spec/github/authorization_spec.rb
284
+ - spec/github/authorizations_spec.rb
281
285
  - spec/github/client_spec.rb
282
286
  - spec/github/core_ext/hash_spec.rb
283
287
  - spec/github/gists/comments_spec.rb