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

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.



14
15
16
17
18
19
20
# File 'lib/git_hub_bub/request.rb', line 14

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.



5
6
7
# File 'lib/git_hub_bub/request.rb', line 5

def options
  @options
end

#tokenObject Also known as: token?

Returns the value of attribute token.



5
6
7
# File 'lib/git_hub_bub/request.rb', line 5

def token
  @token
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/git_hub_bub/request.rb', line 5

def url
  @url
end

Class Method Details

.before_callbacksObject



125
126
127
# File 'lib/git_hub_bub/request.rb', line 125

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

.clear_callbacksObject



129
130
131
# File 'lib/git_hub_bub/request.rb', line 129

def self.clear_callbacks
  @before_callbacks = []
end

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



78
79
80
# File 'lib/git_hub_bub/request.rb', line 78

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

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



36
37
38
# File 'lib/git_hub_bub/request.rb', line 36

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

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



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

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

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



58
59
60
# File 'lib/git_hub_bub/request.rb', line 58

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

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



48
49
50
# File 'lib/git_hub_bub/request.rb', line 48

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

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



68
69
70
# File 'lib/git_hub_bub/request.rb', line 68

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

.set_before_callback(&block) ⇒ Object



121
122
123
# File 'lib/git_hub_bub/request.rb', line 121

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

Instance Method Details

#before_callbacks!Object



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

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

#deleteObject



82
83
84
85
86
# File 'lib/git_hub_bub/request.rb', line 82

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

#getObject



40
41
42
43
44
45
46
# File 'lib/git_hub_bub/request.rb', line 40

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

#headObject



30
31
32
33
34
# File 'lib/git_hub_bub/request.rb', line 30

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

#patchObject



62
63
64
65
66
# File 'lib/git_hub_bub/request.rb', line 62

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

#postObject



52
53
54
55
56
# File 'lib/git_hub_bub/request.rb', line 52

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

#putObject



72
73
74
75
76
# File 'lib/git_hub_bub/request.rb', line 72

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!



101
102
103
# File 'lib/git_hub_bub/request.rb', line 101

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

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

Yields:

  • (_self)

Yield Parameters:



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

def run_callback(&block)
  yield self
end

#set_auth_from_token!Object



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

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

#skip_token?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/git_hub_bub/request.rb', line 22

def skip_token?
  @skip_token
end

#wrap_request(&block) ⇒ Object

Raises:



88
89
90
91
92
93
94
95
96
97
# File 'lib/git_hub_bub/request.rb', line 88

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
  raise RequestError, "message: '#{response.json_body['message']}', url: '#{url}', response: '#{response.inspect}'" unless response.status.to_s =~ /^2.*/
  return response
end