class Teamsupport::REST::Request

Attributes

client[RW]
headers[RW]
options[RW]
path[RW]
request_method[RW]
uri[RW]
verb[RW]

Public Class Methods

new(client, request_method, path, options = {}) click to toggle source

Initialize a new REST Request object

@param client [Teamsupport::Client] @param request_method [String, Symbol] @param path [String] @param options [Hash]

@return [Teamsupport::REST::Request]

@api private

# File lib/teamsupport/rest/request.rb, line 25
def initialize(client, request_method, path, options = {})
  @client = client
  @uri = Addressable::URI.parse(path.start_with?('http') ? path : @client.api_url + path)
  @request_method = request_method
  @headers = Teamsupport::Headers.new(@client, @request_method, @uri, options).request_headers
  @path = uri.path
  @options = options
end

Public Instance Methods

perform() click to toggle source

Perform an HTTP REST request and return response body or raise the error

@example

Teamsupport::REST::Request.new(self, request_method, path, options).perform

@return [Array, Hash]

@api public

# File lib/teamsupport/rest/request.rb, line 42
def perform
  options_key = @request_method == :get ? :params : :body
  # For non-GET requests need to send the options as JSON in the request body
  response = http_client.headers(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
  response_body = response.body.empty? ? '' : symbolize_keys!(response.parse)
  response_headers = response.headers
  fail_or_return_response_body(response.code, response_body, response_headers)
end

Private Instance Methods

error(code, body, headers) click to toggle source

Check response code/body/headers for errors and return Teamsupport::Error

@param code [Strin] @param body [String] @param headers [String]

@return [Teamsupport::Error, nil]

@api private

# File lib/teamsupport/rest/request.rb, line 79
def error(code, body, headers)
  klass = Teamsupport::Error::ERRORS[code]

  klass.from_response(body, headers) unless klass.nil?
end
fail_or_return_response_body(code, body, headers) click to toggle source

Check response and raise an error or return the response body if successful

@param code [Strin] @param body [String] @param headers [String]

@raise [Teamsupport::Error] Error raised based on response code/body/headers

@return [Array, Hash]

@api private

# File lib/teamsupport/rest/request.rb, line 64
def fail_or_return_response_body(code, body, headers)
  error = error(code, body, headers)
  raise(error) if error
  body
end
http_client() click to toggle source

Returns the HTTP Client object

@return [HTTP::Client, HTTP]

@api private

# File lib/teamsupport/rest/request.rb, line 110
def http_client
  HTTP
end
symbolize_keys!(object) click to toggle source

Convert array/hash keys to symbols

@param object [Array, Hash]

@return [Hash, Array]

@api private

# File lib/teamsupport/rest/request.rb, line 92
def symbolize_keys!(object)
  if object.is_a?(Array)
    object.each_with_index do |val, index|
      object[index] = symbolize_keys!(val)
    end
  elsif object.is_a?(Hash)
    object.keys.each do |key|
      object[key.to_sym] = symbolize_keys!(object.delete(key))
    end
  end
  object
end