forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.rb
More file actions
78 lines (72 loc) · 1.99 KB
/
Copy pathconnection.rb
File metadata and controls
78 lines (72 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# encoding: utf-8
module Github
# Specifies Http connection options
module Connection
extend self
include Github::Constants
ALLOWED_OPTIONS = [
:headers,
:url,
:params,
:request,
:ssl
].freeze
# Default requets header information
#
# @return [Hash[String]]
#
# @api private
def default_headers
{
ACCEPT => 'application/vnd.github.v3+json,' \
'application/vnd.github.beta+json;q=0.5,' \
'application/json;q=0.1',
ACCEPT_CHARSET => 'utf-8'
}
end
# Create default connection options
#
# @return [Hash[Symbol]]
# the default options
#
# @api private
def default_options(options = {})
headers = default_headers.merge(options[:headers] || {})
headers.merge!({USER_AGENT => options[:user_agent]})
{
headers: headers,
ssl: options[:ssl],
url: options[:endpoint]
}
end
# Exposes middleware builder to facilitate custom stacks and easy
# addition of new extensions such as cache adapter.
#
# @api public
def stack(options = {})
@stack ||= begin
builder_class = if defined?(Faraday::RackBuilder)
Faraday::RackBuilder
else
Faraday::Builder
end
builder_class.new(&Github.default_middleware(options))
end
end
# Creates http connection
#
# Returns a Fraday::Connection object
def connection(api, options = {})
connection_options = default_options(options)
connection_options.merge!(builder: stack(options.merge!(api: api)))
if options[:connection_options]
connection_options.deep_merge!(options[:connection_options])
end
if ENV['DEBUG']
p "Connection options : \n"
pp connection_options
end
Faraday.new(connection_options)
end
end # Connection
end # Github