Class: GitHubBub::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/git_hub_bub/request.rb

Constant Summary collapse

BASE_URI =
'https://api.github.com'
GITHUB_VERSION =
"vnd.github.3.raw+json"
BASE_HEADERS =
EXTRA_HEADERS.merge({'Accept' => "application/#{GITHUB_VERSION}", "User-Agent" => USER_AGENT})
BASE_OPTIONS =
{ omit_default_port:  true }
RETRIES =
1
RAISE_ON_FAIL =
ENV["GIT_HUB_BUB_RAISE_ON_FAIL"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, query = {}, options = {}) ⇒ Request

Returns a new instance of Request.



17
18
19
20
21
22
23
# File 'lib/git_hub_bub/request.rb', line 17

def initialize(url, query = {}, options = {})
  self.url               = url =~ /^http(\w?)\:\/\// ? url : File.join(BASE_URI, url)
  @skip_token            = options.delete(:skip_token)
  self.options           = BASE_OPTIONS.merge(options || {})
  self.options[:query]   = query if query && !query.empty?
  self.options[:headers] = BASE_HEADERS.merge(options[:headers]|| {})
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/git_hub_bub/request.rb', line 7

def options
  @options
end

#tokenObject Also known as: token?

Returns the value of attribute token.



7
8
9
# File 'lib/git_hub_bub/request.rb', line 7

def token
  @token
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/git_hub_bub/request.rb', line 7

def url
  @url
end

Class Method Details

.before_callbacksObject



131
132
133
# File 'lib/git_hub_bub/request.rb', line 131

def self.before_callbacks
  @before_callbacks ||=[]
end

.clear_callbacksObject



135
136
137
# File 'lib/git_hub_bub/request.rb', line 135

def self.clear_callbacks
  @before_callbacks = []
end

.delete(url, query = {}, options = {}) ⇒ Object



81
82
83
# File 'lib/git_hub_bub/request.rb', line 81

def self.delete(url, query = {}, options = {})
  self.new(url, query, options).delete
end

.get(url, query = {}, options = {}) ⇒ Object



39
40
41
# File 'lib/git_hub_bub/request.rb', line 39

def self.get(url, query = {}, options = {})
  self.new(url, query, options).get
end

.head(url, query = {}, options = {}) ⇒ Object



29
30
31
# File 'lib/git_hub_bub/request.rb', line 29

def self.head(url, query = {}, options = {})
  self.new(url, query, options).head
end

.patch(url, query = {}, options = {}) ⇒ Object



61
62
63
# File 'lib/git_hub_bub/request.rb', line 61

def self.patch(url, query = {}, options = {})
  self.new(url, query, options).patch
end

.post(url, query = {}, options = {}) ⇒ Object



51
52
53
# File 'lib/git_hub_bub/request.rb', line 51

def self.post(url, query = {}, options = {})
  self.new(url, query, options).post
end

.put(url, query = {}, options = {}) ⇒ Object



71
72
73
# File 'lib/git_hub_bub/request.rb', line 71

def self.put(url, query = {}, options = {})
  self.new(url, query, options).put
end

.set_before_callback(&block) ⇒ Object



127
128
129
# File 'lib/git_hub_bub/request.rb', line 127

def self.set_before_callback(&block)
  before_callbacks << block
end

Instance Method Details

#before_callbacks!Object



139
140
141
142
143
# File 'lib/git_hub_bub/request.rb', line 139

def before_callbacks!
  self.class.before_callbacks.each do |callback|
    run_callback &callback
  end
end

#deleteObject



85
86
87
88
89
# File 'lib/git_hub_bub/request.rb', line 85

def delete
  wrap_request do
    Excon.delete(url, options)
  end
end

#getObject



43
44
45
46
47
48
49
# File 'lib/git_hub_bub/request.rb', line 43

def get
  wrap_request do
    ex = Excon.get(url, options)
    ex = Excon.get(@location, options) if @location = ex.headers["Location"]
    ex
  end
end

#headObject



33
34
35
36
37
# File 'lib/git_hub_bub/request.rb', line 33

def head
  wrap_request do
    Excon.head(url, options)
  end
end

#patchObject



65
66
67
68
69
# File 'lib/git_hub_bub/request.rb', line 65

def patch
  wrap_request do
    Excon.patch(url, options)
  end
end

#postObject



55
56
57
58
59
# File 'lib/git_hub_bub/request.rb', line 55

def post
  wrap_request do
    Excon.post(url, options)
  end
end

#putObject



75
76
77
78
79
# File 'lib/git_hub_bub/request.rb', line 75

def put
  wrap_request do
    Excon.put(url, options)
  end
end

#query_to_json_body!Object

do they take query params? do they take :body? who cares, send them both!



107
108
109
# File 'lib/git_hub_bub/request.rb', line 107

def query_to_json_body!
  options[:body] = options[:query].to_json if options[:query]
end

#run_callback {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



145
146
147
# File 'lib/git_hub_bub/request.rb', line 145

def run_callback(&block)
  yield self
end

#set_auth_from_token!Object



111
112
113
114
# File 'lib/git_hub_bub/request.rb', line 111

def set_auth_from_token!
  return unless token
  options[:headers]["Authorization"] ||= "token #{token}"
end

#skip_token?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/git_hub_bub/request.rb', line 25

def skip_token?
  @skip_token
end

#wrap_request(&block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/git_hub_bub/request.rb', line 91

def wrap_request(&block)
  before_callbacks!
  set_auth_from_token! unless skip_token?
  query_to_json_body!
  response = RETRIES.times.retry do
    GitHubBub::Response.create(yield)
  end

  if RAISE_ON_FAIL
    raise RequestError, "message: '#{response.json_body['message']}', url: '#{url}', response: '#{response.inspect}'" unless response.success?
  end
  return response
end