class Glassfrog::REST::Request

Encapsulates an HTTP Request.

Constants

REQUEST_ASSOCIATIONS
ROOT_URL

Attributes

client[RW]

@return [Glassfrog::Client]

headers[RW]

@return [Hash]

options[RW]

@return [Hash]

request_method[RW]

@return [Symbol]

uri[RW]

@return [Addressable::URI]

Public Class Methods

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

Initializes a new Request object. @param client [Glassfrog::Client] The client that will send the request. @param request_method [Symbol] The type of request that will be sent. @param path [String] The path (minus the root) that the request will be sent to. @param options [Hash] The options that will be included in the request.

@return [Glassfrog::Request] The initialized Request object.

# File lib/glassfrog/rest/request.rb, line 38
def initialize(client, request_method, path, options)
  @client = client
  @headers = client.headers
  @request_method = request_method
  @uri = Addressable::URI.parse(path.start_with?('http') ? path : ROOT_URL + path)
  @options = options
end

Public Instance Methods

perform() click to toggle source

Sends the Request.

@return [Array<Hash>, Boolean] The fetched or created parameters, or boolean reflecting whether the request was successful.

# File lib/glassfrog/rest/request.rb, line 50
def perform
  options_key = REQUEST_ASSOCIATIONS[@request_method]
  response = @client.http.headers(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
  fail_or_return_response_body(response.code, response, response.headers)
end

Private Instance Methods

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

Generates an error if the code corresponds to one. @param code [Integer] The HTTP response code. @param body [String] The body of the HTTP response. @param headers [Hash] The HTTP response headers.

@return [Glassfrog::Error, nil] The corresponding error, or nil.

# File lib/glassfrog/rest/request.rb, line 82
def error(code, body, headers)
  klass = Glassfrog::Error::ERRORS[code]
  klass.from_response(code, body, headers) if !klass.nil?
end
fail_or_return_response_body(code, body, headers) click to toggle source

Returns an error if there was one, or parses the fetched object. @param code [Integer] The HTTP response code. @param body [String] The body of the HTTP response. @param headers [Hash] The HTTP response headers.

@return [Array<Hash>, Boolean] The fetched or created parameters or boolean reflecting whether the request was successful.

# File lib/glassfrog/rest/request.rb, line 65
def fail_or_return_response_body(code, body, headers)
  error = error(code, body, headers)
  fail(error) if error
  if @request_method == :patch || @request_method == :delete
    true
  else
    symbolize_keys(body.parse)
  end
end