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
- RAISE_ON_FAIL =
ENV["GIT_HUB_BUB_RAISE_ON_FAIL"]
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
- #skip_token? ⇒ Boolean
- #wrap_request(&block) ⇒ Object
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 = {}, = {}) self.url = url =~ /^http(\w?)\:\/\// ? url : File.join(BASE_URI, url) @skip_token = .delete(:skip_token) 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.
7 8 9 |
# File 'lib/git_hub_bub/request.rb', line 7 def @options end |
#token ⇒ Object 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 |
#url ⇒ Object
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_callbacks ⇒ Object
131 132 133 |
# File 'lib/git_hub_bub/request.rb', line 131 def self.before_callbacks @before_callbacks ||=[] end |
.clear_callbacks ⇒ Object
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 = {}, = {}) self.new(url, query, ).delete end |
.get(url, query = {}, options = {}) ⇒ Object
39 40 41 |
# File 'lib/git_hub_bub/request.rb', line 39 def self.get(url, query = {}, = {}) self.new(url, query, ).get end |
.head(url, query = {}, options = {}) ⇒ Object
29 30 31 |
# File 'lib/git_hub_bub/request.rb', line 29 def self.head(url, query = {}, = {}) self.new(url, query, ).head end |
.patch(url, query = {}, options = {}) ⇒ Object
61 62 63 |
# File 'lib/git_hub_bub/request.rb', line 61 def self.patch(url, query = {}, = {}) self.new(url, query, ).patch end |
.post(url, query = {}, options = {}) ⇒ Object
51 52 53 |
# File 'lib/git_hub_bub/request.rb', line 51 def self.post(url, query = {}, = {}) self.new(url, query, ).post end |
.put(url, query = {}, options = {}) ⇒ Object
71 72 73 |
# File 'lib/git_hub_bub/request.rb', line 71 def self.put(url, query = {}, = {}) self.new(url, query, ).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 |
#delete ⇒ Object
85 86 87 88 89 |
# File 'lib/git_hub_bub/request.rb', line 85 def delete wrap_request do Excon.delete(url, ) end end |
#get ⇒ Object
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, ) ex = Excon.get(@location, ) if @location = ex.headers["Location"] ex end end |
#head ⇒ Object
33 34 35 36 37 |
# File 'lib/git_hub_bub/request.rb', line 33 def head wrap_request do Excon.head(url, ) end end |
#patch ⇒ Object
65 66 67 68 69 |
# File 'lib/git_hub_bub/request.rb', line 65 def patch wrap_request do Excon.patch(url, ) end end |
#post ⇒ Object
55 56 57 58 59 |
# File 'lib/git_hub_bub/request.rb', line 55 def post wrap_request do Excon.post(url, ) end end |
#put ⇒ Object
75 76 77 78 79 |
# File 'lib/git_hub_bub/request.rb', line 75 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!
107 108 109 |
# File 'lib/git_hub_bub/request.rb', line 107 def query_to_json_body! [:body] = [:query].to_json if [:query] end |
#run_callback {|_self| ... } ⇒ Object
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 [:headers]["Authorization"] ||= "token #{token}" end |
#skip_token? ⇒ 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 |