Skip to content

Commit 3e3b9fc

Browse files
committed
Fix issue with setting connection options based on custom request headers.
1 parent e7bf7e8 commit 3e3b9fc

1 file changed

Lines changed: 33 additions & 26 deletions

File tree

lib/github_api/connection.rb

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,34 @@ module Connection
1414
:ssl
1515
].freeze
1616

17+
# Default requets header information
18+
#
19+
# @return [Hash[String]]
20+
#
21+
# @api private
22+
def default_headers
23+
{
24+
ACCEPT => 'application/vnd.github.v3+json,' \
25+
'application/vnd.github.beta+json;q=0.5,' \
26+
'application/json;q=0.1',
27+
ACCEPT_CHARSET => 'utf-8'
28+
}
29+
end
30+
31+
# Create default connection options
32+
#
33+
# @return [Hash[Symbol]]
34+
# the default options
35+
#
36+
# @api private
1737
def default_options(options = {})
18-
accept = options[:headers] && options[:headers][:accept]
38+
headers = default_headers.merge(options[:headers] || {})
39+
headers.merge!({USER_AGENT => options[:user_agent]})
1940
{
20-
headers: {
21-
ACCEPT => accept || 'application/vnd.github.v3+json,' \
22-
'application/vnd.github.beta+json;q=0.5,' \
23-
'application/json;q=0.1',
24-
ACCEPT_CHARSET => 'utf-8',
25-
USER_AGENT => options[:user_agent]
26-
},
41+
headers: headers,
2742
ssl: options[:ssl],
2843
url: options[:endpoint]
29-
}.tap do |h|
30-
if type = options[:headers] && options[:headers][CONTENT_TYPE]
31-
h[:headers][CONTENT_TYPE] = type
32-
end
33-
h
34-
end
35-
end
36-
37-
def clear_cache
38-
@connection = nil
39-
end
40-
41-
def caching?
42-
!@connection.nil?
44+
}
4345
end
4446

4547
# Exposes middleware builder to facilitate custom stacks and easy
@@ -48,7 +50,11 @@ def caching?
4850
# @api public
4951
def stack(options = {})
5052
@stack ||= begin
51-
builder_class = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
53+
builder_class = if defined?(Faraday::RackBuilder)
54+
Faraday::RackBuilder
55+
else
56+
Faraday::Builder
57+
end
5258
builder_class.new(&Github.default_middleware(options))
5359
end
5460
end
@@ -58,14 +64,15 @@ def stack(options = {})
5864
# Returns a Fraday::Connection object
5965
def connection(api, options = {})
6066
connection_options = default_options(options)
61-
clear_cache unless options.empty?
6267
connection_options.merge!(builder: stack(options.merge!(api: api)))
63-
connection_options.deep_merge!(options[:connection_options]) if options[:connection_options]
68+
if options[:connection_options]
69+
connection_options.deep_merge!(options[:connection_options])
70+
end
6471
if ENV['DEBUG']
6572
p "Connection options : \n"
6673
pp connection_options
6774
end
68-
@connection ||= Faraday.new(connection_options)
75+
Faraday.new(connection_options)
6976
end
7077
end # Connection
7178
end # Github

0 commit comments

Comments
 (0)