Skip to content

Commit bc40350

Browse files
committed
Change to simplify api setup.
1 parent a7d8952 commit bc40350

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

lib/github_api/api.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module Github
1717
# Core class responsible for api interface operations
1818
class API
1919
extend Github::ClassMethods
20+
2021
include Constants
2122
include Authorization
2223
include MimeType
@@ -43,36 +44,39 @@ class API
4344
#
4445
# @api public
4546
def initialize(options={}, &block)
46-
setup(options)
47+
opts = Github.configuration.fetch.merge(options)
48+
@current_options = opts
49+
50+
Github.configuration.property_names.each do |key|
51+
send("#{key}=", opts[key])
52+
end
53+
54+
if opts.key?(:login)
55+
@login, @password = opts[:login], opts[:password]
56+
elsif opts.key?(:basic_auth)
57+
@login, @password = extract_basic_auth(opts[:basic_auth])
58+
end
59+
4760
yield_or_eval(&block) if block_given?
4861
end
4962

63+
# Call block with argument
64+
#
65+
# @api private
5066
def yield_or_eval(&block)
5167
return unless block
5268
block.arity > 0 ? yield(self) : self.instance_eval(&block)
5369
end
5470

55-
# Configure options and process basic authorization
56-
#
57-
# @api private
58-
def setup(options={})
59-
options = Github.configuration.fetch.merge(options)
60-
self.current_options = options
61-
Github.configuration.property_names.each do |key|
62-
send("#{key}=", options[key])
63-
end
64-
process_basic_auth(options[:basic_auth])
65-
end
66-
6771
# Extract login and password from basic_auth parameter
6872
#
69-
def process_basic_auth(auth)
73+
# @api private
74+
def extract_basic_auth(auth)
7075
case auth
7176
when String
72-
self.login, self.password = auth.split(':', 2)
77+
auth.split(':', 2)
7378
when Hash
74-
self.login = auth[:login]
75-
self.password = auth[:password]
79+
[auth[:login], auth[:password]]
7680
end
7781
end
7882

lib/github_api/authorization.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,5 @@ def authentication
7171
def _verify_client # :nodoc:
7272
raise ArgumentError, 'Need to provide client_id and client_secret' unless client_id? && client_secret?
7373
end
74-
7574
end # Authorization
7675
end # Github

spec/github/authorization_spec.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
let(:site) { 'http://github-ent.example.com/' }
1010
let(:options) { {:site => site} }
1111

12-
subject(:github) { Github.new options }
12+
subject(:github) { Github.new(options) }
1313

1414
after do
15-
reset_authentication_for github
15+
reset_authentication_for(github)
1616
end
1717

1818
context '.client' do
@@ -151,13 +151,16 @@
151151
end
152152

153153
context 'login & password' do
154-
let(:options) { { :login => 'github', :password => 'pass' } }
155-
156154
it "should return hash with login & password params" do
157-
expect(github.authentication).to be_a Hash
158-
expect(github.authentication).to have_key(:login)
155+
options = {login: 'github', password: 'pass'}
156+
github = Github.new(options)
157+
158+
expect(github.authentication).to be_a(Hash)
159+
expect(github.authentication).to include({login: 'github'})
160+
expect(github.authentication).to include({password: 'pass'})
161+
162+
reset_authentication_for(github)
159163
end
160164
end
161165
end # authentication
162-
163166
end # Github::Authorization

0 commit comments

Comments
 (0)