Skip to content

Commit a7bedd4

Browse files
committed
Add options integration.
1 parent f0decae commit a7bedd4

3 files changed

Lines changed: 117 additions & 2 deletions

File tree

lib/github_api/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def stack(options={}, &block)
8787

8888
# Returns a Fraday::Connection object
8989
#
90-
def connection(options = {})
90+
def connection(options={})
9191
conn_options = default_options(options)
9292
clear_cache unless options.empty?
9393
puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG']

lib/github_api/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def request(method, path, params, options) # :nodoc:
3636

3737
puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']
3838

39-
conn = connection(options)
39+
conn = connection(options.merge(current_options))
4040
if conn.path_prefix != '/' && path.index(conn.path_prefix) != 0
4141
path = (conn.path_prefix + path).gsub(/\/(\/)*/, '/')
4242
end

spec/integration/options_spec.rb

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github, 'options' do
6+
let(:default_adapter) { :net_http }
7+
let(:adapter) { :patron }
8+
let(:token) { '123' }
9+
let(:options) { { :adapter => adapter } }
10+
11+
context 'when different instances of the same class' do
12+
it 'instantiates the same api classes with different options' do
13+
repos = Github::Repos.new
14+
repos2 = Github::Repos.new options
15+
16+
expect(repos.object_id).to_not eql(repos2.object_id)
17+
18+
expect(repos.adapter).to eql default_adapter
19+
expect(repos2.adapter).to eql adapter
20+
21+
repos.adapter = adapter
22+
repos2.adapter = default_adapter
23+
24+
expect(repos.adapter).to eql adapter
25+
expect(repos2.adapter).to eql default_adapter
26+
end
27+
28+
it 'instantiates the same clients with different options' do
29+
client = Github.new :oauth_token => 'client'
30+
client2 = Github.new :oauth_token => 'client2'
31+
32+
expect(client.object_id).to_not eql(client2.object_id)
33+
34+
expect(client.oauth_token).to eql 'client'
35+
expect(client2.oauth_token).to eql 'client2'
36+
end
37+
end
38+
39+
context 'when different instances of the same chain' do
40+
it "inherits properties from client and doesn't pass them up the tree" do
41+
client = Github.new options
42+
repos = client.repos
43+
44+
expect(client.adapter).to eql(adapter)
45+
expect(repos.adapter).to eql(adapter)
46+
47+
repos.adapter = default_adapter
48+
expect(client.adapter).to eql(adapter)
49+
expect(repos.adapter).to eql(default_adapter)
50+
51+
client.oauth_token = token
52+
expect(client.oauth_token).to eql(token)
53+
expect(repos.oauth_token).to be_nil
54+
end
55+
56+
it "inherits properties from api and doesn't pass them up the tree" do
57+
repos = Github::Repos.new options
58+
comments = repos.comments
59+
60+
expect(repos.adapter).to eql(adapter)
61+
expect(comments.adapter).to eql(adapter)
62+
63+
comments.adapter = default_adapter
64+
expect(repos.adapter).to eql(adapter)
65+
expect(comments.adapter).to eql(default_adapter)
66+
67+
repos.oauth_token = token
68+
expect(repos.oauth_token).to eql(token)
69+
expect(comments.oauth_token).to be_nil
70+
end
71+
end
72+
73+
context 'when setting attributes through accessors' do
74+
let(:default_endpoint) { 'https://api.github.com'}
75+
let(:endpoint) { 'https://my-company/api/v3' }
76+
let(:login) { 'Piotr' }
77+
78+
it 'sets login on correct instance' do
79+
client = Github.new :login => login
80+
expect(Github.login).to be_nil
81+
expect(client.login).to eql login
82+
end
83+
84+
it 'sets attribute' do
85+
client = Github.new options
86+
client.endpoint = endpoint
87+
expect(Github.endpoint).to eql default_endpoint
88+
expect(client.endpoint).to eql endpoint
89+
end
90+
91+
it 'sets attribute through dsl' do
92+
client = Github.new do |config|
93+
config.endpoint = endpoint
94+
end
95+
expect(client.endpoint).to eql endpoint
96+
repos = client.repos do |config|
97+
config.adapter = adapter
98+
end
99+
expect(repos.endpoint).to eql endpoint
100+
expect(repos.adapter).to eql adapter
101+
end
102+
103+
it 'updates current_options through api setters' do
104+
client = Github.new :endpoint => endpoint
105+
expect(client.repos.current_options[:endpoint]).to eql endpoint
106+
expect(client.repos.endpoint).to eql endpoint
107+
108+
client.repos.endpoint = default_endpoint
109+
110+
expect(client.repos.endpoint).to eql default_endpoint
111+
expect(client.repos.current_options[:endpoint]).to eql default_endpoint
112+
end
113+
end
114+
115+
end # options

0 commit comments

Comments
 (0)