Class: Teamsupport::REST::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/teamsupport/rest/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request_method, path, options = {}) ⇒ Teamsupport::REST::Request

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new REST Request object

Parameters:

  • client (Teamsupport::Client)
  • request_method (String, Symbol)
  • path (String)
  • options (Hash) (defaults to: {})


25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



12
13
14
# File 'lib/teamsupport/rest/request.rb', line 12

def client
  @client
end

#headersObject

Returns the value of attribute headers.



12
13
14
# File 'lib/teamsupport/rest/request.rb', line 12

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/teamsupport/rest/request.rb', line 12

def options
  @options
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/teamsupport/rest/request.rb', line 12

def path
  @path
end

#request_methodObject Also known as: verb

Returns the value of attribute request_method.



12
13
14
# File 'lib/teamsupport/rest/request.rb', line 12

def request_method
  @request_method
end

#uriObject

Returns the value of attribute uri.



12
13
14
# File 'lib/teamsupport/rest/request.rb', line 12

def uri
  @uri
end

Instance Method Details

#performArray, Hash

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

Examples:

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

Returns:

  • (Array, Hash)


42
43
44
45
46
47
48
49
# 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