Class: Tremendous::Request
- Inherits:
-
Object
- Object
- Tremendous::Request
- Defined in:
- lib/tremendous/request.rb
Class Method Summary collapse
- ._execute(method, url, data = {}, *opts) ⇒ Object
- .delete(path, data = {}, *opts) ⇒ Object
- .get(path, data = {}, *opts) ⇒ Object
- .handle_response(response) ⇒ Object
- .post(path, data = {}, *opts) ⇒ Object
- .put(path, data = {}, *opts) ⇒ Object
- .url(path, params = {}) ⇒ Object
Class Method Details
._execute(method, url, data = {}, *opts) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/tremendous/request.rb', line 19 def self._execute(method, url, data={}, *opts) data[:headers] = { 'authorization' => "Bearer #{Tremendous.[:access_token]}" }.merge(data.class == Hash ? data[:headers] || {} : {}) HTTParty.send(method, url, data, *opts) end |
.delete(path, data = {}, *opts) ⇒ Object
15 16 17 |
# File 'lib/tremendous/request.rb', line 15 def self.delete(path, data={}, *opts) handle_response(_execute(:delete, url(path), data, *opts)) end |
.get(path, data = {}, *opts) ⇒ Object
3 4 5 |
# File 'lib/tremendous/request.rb', line 3 def self.get(path, data={}, *opts) handle_response(_execute(:get, url(path), data, *opts)) end |
.handle_response(response) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tremendous/request.rb', line 31 def self.handle_response(response) if response.success? response_json = JSON.parse(response.body).with_indifferent_access else case response.code when 400 raise Tremendous::BadDataError.new(response) when 401 raise Tremendous::AccessError.new(response) when 402 raise Tremendous::PaymentError.new(response) else raise Tremendous::Error.new(response) end end end |
.post(path, data = {}, *opts) ⇒ Object
7 8 9 |
# File 'lib/tremendous/request.rb', line 7 def self.post(path, data={}, *opts) handle_response(_execute(:post, url(path), data, *opts)) end |
.put(path, data = {}, *opts) ⇒ Object
11 12 13 |
# File 'lib/tremendous/request.rb', line 11 def self.put(path, data={}, *opts) handle_response(_execute(:put, url(path), data, *opts)) end |
.url(path, params = {}) ⇒ Object
27 28 29 |
# File 'lib/tremendous/request.rb', line 27 def self.url(path, params={}) url = URI.join(Tremendous.config[:base_api_uri], path) end |