Class: Opendistro::Request
- Inherits:
-
Object
- Object
- Opendistro::Request
- Includes:
- HTTParty
- Defined in:
- lib/opendistro/request.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#ca_cert ⇒ Object
Returns the value of attribute ca_cert.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
-
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
Class Method Summary collapse
-
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
-
.parse(body) ⇒ Object
Converts the response body to an ObjectifiedHash.
Instance Method Summary collapse
-
#request_defaults ⇒ Object
Sets a base_uri and default_params for requests.
-
#validate(response) ⇒ Object
Checks the response code for common errors.
Instance Attribute Details
#ca_cert ⇒ Object
Returns the value of attribute ca_cert.
14 15 16 |
# File 'lib/opendistro/request.rb', line 14 def ca_cert @ca_cert end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
14 15 16 |
# File 'lib/opendistro/request.rb', line 14 def endpoint @endpoint end |
#password ⇒ Object
Returns the value of attribute password.
14 15 16 |
# File 'lib/opendistro/request.rb', line 14 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
14 15 16 |
# File 'lib/opendistro/request.rb', line 14 def username @username end |
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
14 15 16 |
# File 'lib/opendistro/request.rb', line 14 def verify_ssl @verify_ssl end |
Class Method Details
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
36 37 38 39 40 |
# File 'lib/opendistro/request.rb', line 36 def self.decode(response) response ? JSON.load(response) : {} rescue JSON::ParserError raise Error::Parsing, 'The response is not a valid JSON' end |
.parse(body) ⇒ Object
Converts the response body to an ObjectifiedHash.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/opendistro/request.rb', line 17 def self.parse(body) body = decode(body) if body.is_a? Hash ObjectifiedHash.new body elsif body.is_a? Array PaginatedResponse.new(body.collect! { |e| ObjectifiedHash.new(e) }) elsif body true elsif !body false elsif body.nil? false else raise Error::Parsing, "Couldn't parse a response body" end end |
Instance Method Details
#request_defaults ⇒ Object
Sets a base_uri and default_params for requests.
73 74 75 |
# File 'lib/opendistro/request.rb', line 73 def request_defaults raise Error::MissingCredentials, 'Please set an endpoint to API' unless @endpoint end |
#validate(response) ⇒ Object
Checks the response code for common errors. Returns parsed response for successful requests.
61 62 63 64 65 66 67 68 69 |
# File 'lib/opendistro/request.rb', line 61 def validate(response) error_klass = Error::STATUS_MAPPINGS[response.code] raise error_klass, response if error_klass 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 |