github_api 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ Grab the gem by issuing
12
12
 
13
13
  or in your Gemfile
14
14
 
15
- gem "github_api", "~> 0.2.1"
15
+ gem "github_api", "~> 0.3.0"
16
16
 
17
17
  == Usage
18
18
 
@@ -44,7 +44,23 @@ module Github
44
44
 
45
45
  # Check whether authentication credentials are present
46
46
  def authenticated?
47
- basic_auth? || oauth_token? || (login? && password?)
47
+ basic_authed? || oauth_token?
48
+ end
49
+
50
+ # Check whether basic authentication credentials are present
51
+ def basic_authed?
52
+ basic_auth? || (login? && password?)
53
+ end
54
+
55
+ # Select authentication parameters
56
+ def authentication
57
+ if login? && password?
58
+ { :login => login, :password => password }
59
+ elsif basic_auth?
60
+ { :basic_auth => basic_auth }
61
+ else
62
+ { }
63
+ end
48
64
  end
49
65
 
50
66
  private
@@ -36,7 +36,6 @@ module Github
36
36
  def connection(options = {}) # :nodoc:
37
37
 
38
38
  # parse(options['resource'], options['mime_type'] || mime_type) if options['mime_type']
39
- debugger
40
39
  # merged_options = if connection_options.empty?
41
40
  # header_options.merge(options)
42
41
  # else
@@ -57,7 +56,7 @@ module Github
57
56
  builder.use Faraday::Response::Logger
58
57
 
59
58
  builder.use Github::Request::OAuth2, oauth_token if oauth_token?
60
- builder.use Github::Request::BasicAuth, login, password if login? && password?
59
+ builder.use Github::Request::BasicAuth, authentication if basic_authed?
61
60
 
62
61
  unless options[:raw]
63
62
  builder.use Github::Response::Mashify
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'base64'
4
4
  require 'addressable/uri'
5
- require 'set'
5
+ require 'multi_json'
6
6
 
7
7
  module Github
8
8
  # Defines HTTP verbs
@@ -49,7 +49,7 @@ module Github
49
49
  request.url(path, params)
50
50
  when *METHODS_WITH_BODIES
51
51
  request.path = path
52
- request.body = _process_params(params) unless params.empty?
52
+ request.body = MultiJson.encode(_process_params(params)) unless params.empty?
53
53
  end
54
54
  end
55
55
  response.body
@@ -9,7 +9,6 @@ module Github
9
9
  dependency 'base64'
10
10
 
11
11
  def call(env)
12
- # puts "BASIC: #{@auth}"
13
12
  env[:request_headers].merge!('Authorization' => "Basic #{@auth}\"")
14
13
 
15
14
  @app.call env
@@ -17,8 +16,14 @@ module Github
17
16
 
18
17
  def initialize(app, *args)
19
18
  @app = app
20
- login, password = args.shift, args.shift
21
- @auth = Base64.encode64("#{login}:#{password}")
19
+ credentials = ""
20
+ options = args.last.is_a?(Hash) ? args.pop : {}
21
+ if options.has_key? :login
22
+ credentials = "#{options[:login]}:#{options[:password]}"
23
+ elsif options.has_key? :basic_auth
24
+ credentials = "#{options[:basic_auth]}"
25
+ end
26
+ @auth = Base64.encode64(credentials)
22
27
  @auth.gsub!("\n", "")
23
28
  end
24
29
  end # BasicAuth
@@ -4,7 +4,7 @@ module Github
4
4
  module VERSION
5
5
  MAJOR = 0
6
6
  MINOR = 3
7
- PATCH = 0
7
+ PATCH = 1
8
8
  BUILD = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.');
@@ -68,4 +68,32 @@ describe Github::Authorization do
68
68
  end
69
69
  end
70
70
 
71
+ context "authentication" do
72
+ it "should respond to 'authentication'" do
73
+ github.should respond_to :authentication
74
+ end
75
+
76
+ context 'basic_auth' do
77
+ before do
78
+ github = Github.new :basic_auth => 'github:pass'
79
+ end
80
+
81
+ it "should return hash with basic auth params" do
82
+ github.authentication.should be_a Hash
83
+ github.authentication.should have_key :basic_auth
84
+ end
85
+ end
86
+
87
+ context 'login & password' do
88
+ before do
89
+ github = Github.new :login => 'github', :password => 'pass'
90
+ end
91
+
92
+ it "should return hash with login & password params" do
93
+ github.authentication.should be_a Hash
94
+ github.authentication.should have_key :login
95
+ end
96
+ end
97
+ end # authentication
98
+
71
99
  end # Github::Authorization
@@ -3,9 +3,10 @@ require 'spec_helper'
3
3
  describe Github::Authorizations do
4
4
 
5
5
  let(:github) { Github.new }
6
+ let(:basic_auth) { 'login:password' }
6
7
 
7
8
  before do
8
- github.basic_auth = 'login:password'
9
+ github.basic_auth = basic_auth
9
10
  end
10
11
 
11
12
  after do
@@ -15,7 +16,7 @@ describe Github::Authorizations do
15
16
  describe "authorizations" do
16
17
  context "resource found" do
17
18
  before do
18
- stub_get("/authorizations").
19
+ stub_get("/authorizations", "https://#{basic_auth}@api.github.com").
19
20
  to_return(:body => fixture('auths/authorizations.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
20
21
  end
21
22
 
@@ -27,7 +28,7 @@ describe Github::Authorizations do
27
28
 
28
29
  it "should get the resources" do
29
30
  github.oauth.authorizations
30
- a_get("/authorizations").should have_been_made
31
+ a_get("/authorizations", "https://#{basic_auth}@api.github.com").should have_been_made
31
32
  end
32
33
 
33
34
  it "should return array of resources" do
@@ -54,7 +55,7 @@ describe Github::Authorizations do
54
55
 
55
56
  context "resource not found" do
56
57
  before do
57
- stub_get("/authorizations").
58
+ stub_get("/authorizations", "https://#{basic_auth}@api.github.com").
58
59
  to_return(:body => "", :status => [404, "Not Found"])
59
60
  end
60
61
 
@@ -69,7 +70,7 @@ describe Github::Authorizations do
69
70
 
70
71
  context "resource found" do
71
72
  before do
72
- stub_get("/authorizations/#{authorization_id}").
73
+ stub_get("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").
73
74
  to_return(:body => fixture('auths/authorization.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
74
75
  end
75
76
 
@@ -79,7 +80,7 @@ describe Github::Authorizations do
79
80
 
80
81
  it "should get the resource" do
81
82
  github.oauth.authorization authorization_id
82
- a_get("/authorizations/#{authorization_id}").should have_been_made
83
+ a_get("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").should have_been_made
83
84
  end
84
85
 
85
86
  it "should get authorization information" do
@@ -96,7 +97,7 @@ describe Github::Authorizations do
96
97
 
97
98
  context "resource not found" do
98
99
  before do
99
- stub_get("/authorizations/#{authorization_id}").
100
+ stub_get("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").
100
101
  to_return(:body => fixture('auths/authorization.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
101
102
  end
102
103
 
@@ -119,13 +120,13 @@ describe Github::Authorizations do
119
120
  end
120
121
 
121
122
  before do
122
- stub_post("/authorizations").with(inputs).
123
+ stub_post("/authorizations", "https://#{basic_auth}@api.github.com").with(inputs).
123
124
  to_return(:body => fixture('auths/authorization.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
124
125
  end
125
126
 
126
127
  it "should create resource successfully" do
127
128
  github.oauth.create_authorization inputs
128
- a_post("/authorizations").with(inputs).should have_been_made
129
+ a_post("/authorizations", "https://#{basic_auth}@api.github.com").with(inputs).should have_been_made
129
130
  end
130
131
 
131
132
  it "should return the resource" do
@@ -141,7 +142,7 @@ describe Github::Authorizations do
141
142
 
142
143
  context "failed to create resource" do
143
144
  before do
144
- stub_post("/authorizations").with(inputs).
145
+ stub_post("/authorizations", "https://#{basic_auth}@api.github.com").with(inputs).
145
146
  to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
146
147
 
147
148
  end
@@ -166,13 +167,13 @@ describe Github::Authorizations do
166
167
  end
167
168
 
168
169
  before do
169
- stub_patch("/authorizations/#{authorization_id}").with(inputs).
170
+ stub_patch("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").with(inputs).
170
171
  to_return(:body => fixture('auths/authorization.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
171
172
  end
172
173
 
173
174
  it "should update resource successfully" do
174
175
  github.oauth.update_authorization authorization_id, inputs
175
- a_patch("/authorizations/#{authorization_id}").with(inputs).should have_been_made
176
+ a_patch("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").with(inputs).should have_been_made
176
177
  end
177
178
 
178
179
  it "should return the resource" do
@@ -188,7 +189,7 @@ describe Github::Authorizations do
188
189
 
189
190
  context "failed to update resource" do
190
191
  before do
191
- stub_patch("/authorizations/#{authorization_id}").with(inputs).
192
+ stub_patch("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").with(inputs).
192
193
  to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
193
194
 
194
195
  end
@@ -213,19 +214,19 @@ describe Github::Authorizations do
213
214
  end
214
215
 
215
216
  before do
216
- stub_delete("/authorizations/#{authorization_id}").
217
+ stub_delete("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").
217
218
  to_return(:body => '', :status => 204, :headers => {:content_type => "application/json; charset=utf-8"})
218
219
  end
219
220
 
220
221
  it "should delete resource successfully" do
221
222
  github.oauth.delete_authorization authorization_id
222
- a_delete("/authorizations/#{authorization_id}").should have_been_made
223
+ a_delete("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").should have_been_made
223
224
  end
224
225
  end
225
226
 
226
227
  context "failed to create resource" do
227
228
  before do
228
- stub_delete("/authorizations/#{authorization_id}").
229
+ stub_delete("/authorizations/#{authorization_id}", "https://#{basic_auth}@api.github.com").
229
230
  to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
230
231
 
231
232
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Piotr Murach
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-11-26 00:00:00 +00:00
17
+ date: 2011-11-27 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency