Class: RubyShift::Request
- Inherits:
-
Object
- Object
- RubyShift::Request
- Includes:
- HTTParty
- Defined in:
- lib/rubyshift/request.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
Class Method Summary collapse
Instance Method Summary collapse
- #delete(path, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
- #post(path, options = {}) ⇒ Object
- #put(path, options = {}) ⇒ Object
-
#set_request_defaults(httparty = nil) ⇒ Object
Sets default_params for requests.
-
#validate(response) ⇒ Object
Checks the response code for common errors.
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
12 13 14 |
# File 'lib/rubyshift/request.rb', line 12 def access_token @access_token end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
12 13 14 |
# File 'lib/rubyshift/request.rb', line 12 def endpoint @endpoint end |
Class Method Details
.decode(response) ⇒ Object
40 41 42 43 44 |
# File 'lib/rubyshift/request.rb', line 40 def self.decode(response) JSON.load response rescue JSON::ParserError raise Error::Parsing.new "The response is not a valid JSON" end |
.parse(body) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rubyshift/request.rb', line 14 def self.parse(body) data = decode(body)['data'] if data.is_a? Hash data.delete('links') data.delete('members') OpenStruct.new data elsif data.is_a? Array data.collect! { |d| d.delete('links') d.delete('members') OpenStruct.new(d) } elsif data true elsif !data false elsif data.nil? false else raise Error::Parsing.new "Couldn't parse the response body: #{body.inspect}" end rescue raise Error::Parsing.new "Couldn't parse the response body: #{body.inspect}" end |
Instance Method Details
#delete(path, options = {}) ⇒ Object
64 65 66 67 68 |
# File 'lib/rubyshift/request.rb', line 64 def delete(path, ={}) set_httparty_config() () validate self.class.delete(@endpoint + path, ) end |
#get(path, options = {}) ⇒ Object
46 47 48 49 50 |
# File 'lib/rubyshift/request.rb', line 46 def get(path, ={}) set_httparty_config() () validate self.class.get(@endpoint + path, ) end |
#post(path, options = {}) ⇒ Object
52 53 54 55 56 |
# File 'lib/rubyshift/request.rb', line 52 def post(path, ={}) set_httparty_config() (, path) validate self.class.post(@endpoint + path, ) end |
#put(path, options = {}) ⇒ Object
58 59 60 61 62 |
# File 'lib/rubyshift/request.rb', line 58 def put(path, ={}) set_httparty_config() () validate self.class.put(@endpoint + path, ) end |
#set_request_defaults(httparty = nil) ⇒ Object
Sets default_params for requests.
94 95 96 97 |
# File 'lib/rubyshift/request.rb', line 94 def set_request_defaults(httparty=nil) raise Error::MissingCredentials.new("Please set an API endpoint") unless @endpoint #self.class.default_params[:httparty] = {} if httparty.nil? end |
#validate(response) ⇒ Object
Checks the response code for common errors. Returns parsed response for successful requests.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rubyshift/request.rb', line 72 def validate(response) case response.code when 400 then fail Error::BadRequest.new (response) when 401 then fail Error::.new (response) when 403 then fail Error::Forbidden.new (response) when 404 then fail Error::NotFound.new (response) when 405 then fail Error::MethodNotAllowed.new (response) when 409 then fail Error::Conflict.new (response) when 422 then fail Error::Unprocessable.new (response) when 500 then fail Error::InternalServerError.new (response) when 502 then fail Error::BadGateway.new (response) when 503 then fail Error::ServiceUnavailable.new (response) end parsed = response.parsed_response parsed.client = self if parsed.respond_to?(:client=) parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!) parsed end |