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
99 lines (85 loc) · 2.7 KB
/
connection.rb
File metadata and controls
99 lines (85 loc) · 2.7 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# encoding: utf-8
require 'faraday'
require 'github_api/response'
require 'github_api/response/mashify'
require 'github_api/response/jsonize'
require 'github_api/response/raise_error'
require 'github_api/response/header'
require 'github_api/request/oauth2'
require 'github_api/request/basic_auth'
require 'github_api/request/jsonize'
module Github
module Connection
extend self
include Github::Constants
ALLOWED_OPTIONS = [
:headers,
:url,
:params,
:request,
:ssl
].freeze
def default_options(options={})
{
:headers => {
ACCEPT => "application/vnd.github.v3.full+json," \
"application/vnd.github.beta.full+json;q=0.7," \
"application/vnd.github+json;q=0.5," \
"application/json;q=0.1",
ACCEPT_CHARSET => "utf-8",
USER_AGENT => user_agent,
CONTENT_TYPE => 'application/json'
},
:ssl => options.fetch(:ssl) { ssl },
:url => options.fetch(:endpoint) { Github.endpoint }
}.merge(options)
end
# Default middleware stack that uses default adapter as specified at
# configuration stage.
#
def default_middleware(options={})
Proc.new do |builder|
builder.use Github::Request::Jsonize
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::UrlEncoded
builder.use Github::Request::OAuth2, oauth_token if oauth_token?
builder.use Github::Request::BasicAuth, authentication if basic_authed?
builder.use Faraday::Response::Logger if ENV['DEBUG']
unless options[:raw]
builder.use Github::Response::Mashify
builder.use Github::Response::Jsonize
end
builder.use Github::Response::RaiseError
builder.adapter adapter
end
end
@connection = nil
@stack = nil
def clear_cache
@connection = nil
end
def caching?
end
# Exposes middleware builder to facilitate custom stacks and easy
# addition of new extensions such as cache adapter.
#
def stack(options={}, &block)
@stack ||= begin
if block_given?
Faraday::Builder.new(&block)
else
Faraday::Builder.new(&default_middleware(options))
end
end
end
# Returns a Fraday::Connection object
#
def connection(options={})
conn_options = default_options(options)
clear_cache unless options.empty?
puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG']
@connection ||= Faraday.new(conn_options.merge(:builder => stack(options)))
end
end # Connection
end # Github