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
- #skip_token? ⇒ Boolean
- #wrap_request(&block) ⇒ Object
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 = {}, = {}) 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.
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
125 126 127 |
# File 'lib/git_hub_bub/request.rb', line 125 def self.before_callbacks @before_callbacks ||=[] end |
.clear_callbacks ⇒ Object
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 = {}, = {}) self.new(url, query, ).delete end |
.get(url, query = {}, options = {}) ⇒ Object
36 37 38 |
# File 'lib/git_hub_bub/request.rb', line 36 def self.get(url, query = {}, = {}) self.new(url, query, ).get end |
.head(url, query = {}, options = {}) ⇒ Object
26 27 28 |
# File 'lib/git_hub_bub/request.rb', line 26 def self.head(url, query = {}, = {}) self.new(url, query, ).head end |
.patch(url, query = {}, options = {}) ⇒ Object
58 59 60 |
# File 'lib/git_hub_bub/request.rb', line 58 def self.patch(url, query = {}, = {}) self.new(url, query, ).patch end |
.post(url, query = {}, options = {}) ⇒ Object
48 49 50 |
# File 'lib/git_hub_bub/request.rb', line 48 def self.post(url, query = {}, = {}) self.new(url, query, ).post end |
.put(url, query = {}, options = {}) ⇒ Object
68 69 70 |
# File 'lib/git_hub_bub/request.rb', line 68 def self.put(url, query = {}, = {}) self.new(url, query, ).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 |
#delete ⇒ Object
82 83 84 85 86 |
# File 'lib/git_hub_bub/request.rb', line 82 def delete wrap_request do Excon.delete(url, ) end end |
#get ⇒ Object
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, ) ex = Excon.get(@location, ) if @location = ex.headers["Location"] ex end end |
#head ⇒ Object
30 31 32 33 34 |
# File 'lib/git_hub_bub/request.rb', line 30 def head wrap_request do Excon.head(url, ) end end |
#patch ⇒ Object
62 63 64 65 66 |
# File 'lib/git_hub_bub/request.rb', line 62 def patch wrap_request do Excon.patch(url, ) end end |
#post ⇒ Object
52 53 54 55 56 |
# File 'lib/git_hub_bub/request.rb', line 52 def post wrap_request do Excon.post(url, ) end end |
#put ⇒ Object
72 73 74 75 76 |
# File 'lib/git_hub_bub/request.rb', line 72 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!
101 102 103 |
# File 'lib/git_hub_bub/request.rb', line 101 def query_to_json_body! [:body] = [:query].to_json if [:query] end |
#run_callback {|_self| ... } ⇒ Object
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 [:headers]["Authorization"] ||= "token #{token}" end |
#skip_token? ⇒ Boolean
22 23 24 |
# File 'lib/git_hub_bub/request.rb', line 22 def skip_token? @skip_token end |
#wrap_request(&block) ⇒ Object
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 |