Class: EphemeralCalc::GoogleAPI::Request
- Inherits:
-
Object
- Object
- EphemeralCalc::GoogleAPI::Request
- Extended by:
- Forwardable
- Defined in:
- lib/ephemeral_calc/google_api/request.rb
Instance Attribute Summary collapse
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#method ⇒ Object
Returns the value of attribute method.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(method, uri, credentials = nil) {|_self| ... } ⇒ Request
constructor
A new instance of Request.
- #perform ⇒ Object
Constructor Details
#initialize(method, uri, credentials = nil) {|_self| ... } ⇒ Request
Returns a new instance of Request.
24 25 26 27 28 29 |
# File 'lib/ephemeral_calc/google_api/request.rb', line 24 def initialize(method, uri, credentials = nil) self.method = method self.uri = uri self.credentials = credentials yield self if block_given? end |
Instance Attribute Details
#credentials ⇒ Object
Returns the value of attribute credentials.
11 12 13 |
# File 'lib/ephemeral_calc/google_api/request.rb', line 11 def credentials @credentials end |
#method ⇒ Object
Returns the value of attribute method.
11 12 13 |
# File 'lib/ephemeral_calc/google_api/request.rb', line 11 def method @method end |
#uri ⇒ Object
Returns the value of attribute uri.
11 12 13 |
# File 'lib/ephemeral_calc/google_api/request.rb', line 11 def uri @uri end |
Class Method Details
.get(uri, credentials = nil) ⇒ Object
14 15 16 17 |
# File 'lib/ephemeral_calc/google_api/request.rb', line 14 def self.get(uri, credentials = nil) result = self.new(:get, uri, credentials) result.perform {|r| yield r if block_given? } end |
.post(uri, credentials = nil) ⇒ Object
19 20 21 22 |
# File 'lib/ephemeral_calc/google_api/request.rb', line 19 def self.post(uri, credentials = nil) result = self.new(:post, uri, credentials) result.perform {|r| yield r if block_given? } end |
Instance Method Details
#perform ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ephemeral_calc/google_api/request.rb', line 31 def perform http_opts = {use_ssl: true} response = Net::HTTP.start(uri.host, uri.port, http_opts) do |http| add_field "Authorization", "Bearer #{credentials.access_token}" if credentials add_field "Accept", "application/json" yield self if block_given? http.request request end if (200..299).include?(response.code.to_i) return response else raise RequestError.new(response.code.to_i), "Error #{response.code} (#{response.msg}) - #{uri}\n#{response.body}" end end |