Class: GitHubBub::Request
- Inherits:
-
Object
- Object
- GitHubBub::Request
- 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
-
#options ⇒ Object
Returns the value of attribute options.
-
#token ⇒ Object
(also: #token?)
Returns the value of attribute token.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .before_callbacks ⇒ Object
- .clear_callbacks ⇒ Object
- .delete(url, query = {}, options = {}) ⇒ Object
- .get(url, query = {}, options = {}) ⇒ Object
- .head(url, query = {}, options = {}) ⇒ Object
- .patch(url, query = {}, options = {}) ⇒ Object
- .post(url, query = {}, options = {}) ⇒ Object
- .put(url, query = {}, options = {}) ⇒ Object
- .set_before_callback(&block) ⇒ Object
Instance Method Summary collapse
- #before_callbacks! ⇒ Object
- #delete ⇒ Object
- #get ⇒ Object
- #head ⇒ Object
-
#initialize(url, query = {}, options = {}) ⇒ Request
constructor
A new instance of Request.
- #patch ⇒ Object
- #post ⇒ Object
- #put ⇒ Object
-
#query_to_json_body! ⇒ Object
do they take query params? do they take :body? who cares, send them both!.
- #run_callback {|_self| ... } ⇒ Object
- #set_auth_from_token! ⇒ Object
- #wrap_request(&block) ⇒ Object
Constructor Details
#initialize(url, query = {}, options = {}) ⇒ Request
Returns a new instance of Request.
14 15 16 17 18 19 |
# File 'lib/git_hub_bub/request.rb', line 14 def initialize(url, query = {}, = {}) self.url = url.include?("http") ? url : File.join(BASE_URI, url) self. = BASE_OPTIONS.merge( || {}) self.[:query] = query if query && !query.empty? self.[:headers] = BASE_HEADERS.merge([:headers]|| {}) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/git_hub_bub/request.rb', line 5 def @options end |
#token ⇒ Object 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 |
#url ⇒ Object
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_callbacks ⇒ Object
120 121 122 |
# File 'lib/git_hub_bub/request.rb', line 120 def self.before_callbacks @before_callbacks ||=[] end |
.clear_callbacks ⇒ Object
124 125 126 |
# File 'lib/git_hub_bub/request.rb', line 124 def self.clear_callbacks @before_callbacks = [] end |
.delete(url, query = {}, options = {}) ⇒ Object
73 74 75 |
# File 'lib/git_hub_bub/request.rb', line 73 def self.delete(url, query = {}, = {}) self.new(url, query, ).delete end |
.get(url, query = {}, options = {}) ⇒ Object
31 32 33 |
# File 'lib/git_hub_bub/request.rb', line 31 def self.get(url, query = {}, = {}) self.new(url, query, ).get end |
.head(url, query = {}, options = {}) ⇒ Object
21 22 23 |
# File 'lib/git_hub_bub/request.rb', line 21 def self.head(url, query = {}, = {}) self.new(url, query, ).head end |
.patch(url, query = {}, options = {}) ⇒ Object
53 54 55 |
# File 'lib/git_hub_bub/request.rb', line 53 def self.patch(url, query = {}, = {}) self.new(url, query, ).patch end |
.post(url, query = {}, options = {}) ⇒ Object
43 44 45 |
# File 'lib/git_hub_bub/request.rb', line 43 def self.post(url, query = {}, = {}) self.new(url, query, ).post end |
.put(url, query = {}, options = {}) ⇒ Object
63 64 65 |
# File 'lib/git_hub_bub/request.rb', line 63 def self.put(url, query = {}, = {}) self.new(url, query, ).put end |
.set_before_callback(&block) ⇒ Object
116 117 118 |
# File 'lib/git_hub_bub/request.rb', line 116 def self.set_before_callback(&block) before_callbacks << block end |
Instance Method Details
#before_callbacks! ⇒ Object
128 129 130 131 132 |
# File 'lib/git_hub_bub/request.rb', line 128 def before_callbacks! self.class.before_callbacks.each do |callback| run_callback &callback end end |
#delete ⇒ Object
77 78 79 80 81 |
# File 'lib/git_hub_bub/request.rb', line 77 def delete wrap_request do Excon.delete(url, ) end end |
#get ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/git_hub_bub/request.rb', line 35 def get wrap_request do ex = Excon.get(url, ) ex = Excon.get(@location, ) if @location = ex.headers["Location"] ex end end |
#head ⇒ Object
25 26 27 28 29 |
# File 'lib/git_hub_bub/request.rb', line 25 def head wrap_request do Excon.head(url, ) end end |
#patch ⇒ Object
57 58 59 60 61 |
# File 'lib/git_hub_bub/request.rb', line 57 def patch wrap_request do Excon.patch(url, ) end end |
#post ⇒ Object
47 48 49 50 51 |
# File 'lib/git_hub_bub/request.rb', line 47 def post wrap_request do Excon.post(url, ) end end |
#put ⇒ Object
67 68 69 70 71 |
# File 'lib/git_hub_bub/request.rb', line 67 def put wrap_request do Excon.put(url, ) end end |
#query_to_json_body! ⇒ Object
do they take query params? do they take :body? who cares, send them both!
96 97 98 |
# File 'lib/git_hub_bub/request.rb', line 96 def query_to_json_body! [:body] = [:query].to_json if [:query] end |
#run_callback {|_self| ... } ⇒ Object
134 135 136 |
# File 'lib/git_hub_bub/request.rb', line 134 def run_callback(&block) yield self end |
#set_auth_from_token! ⇒ Object
100 101 102 103 |
# File 'lib/git_hub_bub/request.rb', line 100 def set_auth_from_token! return unless token [:headers]["Authorization"] ||= "token #{token}" end |
#wrap_request(&block) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/git_hub_bub/request.rb', line 83 def wrap_request(&block) before_callbacks! set_auth_from_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 |