class Opendistro::Request

@private

Attributes

ca_cert[RW]
endpoint[RW]
password[RW]
username[RW]
verify_ssl[RW]

Public Class Methods

decode(response) click to toggle source

Decodes a JSON response into Ruby object.

# 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) click to toggle source

Converts the response body to an ObjectifiedHash.

# 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

Public Instance Methods

request_defaults() click to toggle source

Sets a base_uri and default_params for requests. @raise [Error::MissingCredentials] if endpoint not set.

# File lib/opendistro/request.rb, line 73
def request_defaults
  raise Error::MissingCredentials, 'Please set an endpoint to API' unless @endpoint
end
validate(response) click to toggle source

Checks the response code for common errors. Returns parsed response for successful requests.

# 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

Private Instance Methods

authorization_header() click to toggle source

Returns an Authorization header hash

@raise [Error::MissingCredentials] if private_token and auth_token are not set.

# File lib/opendistro/request.rb, line 82
def authorization_header
  raise Error::MissingCredentials, 'Please provide a private_token or auth_token for user' if @username.nil? || @password.nil?

  auth = Base64.encode64("#{@username}:#{@password}")
  { 'Authorization' => "Basic #{auth}" }
end
httparty_config(options) click to toggle source

Set HTTParty configuration @see github.com/jnunemaker/httparty

# File lib/opendistro/request.rb, line 91
def httparty_config(options)
  options.merge!(httparty) if httparty
end