Class: Glassfrog::REST::Request

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/glassfrog/rest/request.rb

Overview

Encapsulates an HTTP Request.

Constant Summary collapse

ROOT_URL =
'https://glassfrog.holacracy.org/api/v3'
REQUEST_ASSOCIATIONS =
{
     get: :params,
    post: :json,
   patch: :json,
  delete: :form
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#extract_id, #parameterize, #symbolize_keys

Constructor Details

#initialize(client, request_method, path, options) ⇒ Glassfrog::Request

Initializes a new Request object.

Parameters:

  • client (Glassfrog::Client)

    The client that will send the request.

  • request_method (Symbol)

    The type of request that will be sent.

  • path (String)

    The path (minus the root) that the request will be sent to.

  • options (Hash)

    The options that will be included in the request.



38
39
40
41
42
43
44
# 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

Instance Attribute Details

#clientGlassfrog::Client

Returns:



14
15
16
# File 'lib/glassfrog/rest/request.rb', line 14

def client
  @client
end

#headersHash

Returns:

  • (Hash)


16
17
18
# File 'lib/glassfrog/rest/request.rb', line 16

def headers
  @headers
end

#optionsHash

Returns:

  • (Hash)


16
17
18
# File 'lib/glassfrog/rest/request.rb', line 16

def options
  @options
end

#request_methodSymbol

Returns:

  • (Symbol)


18
19
20
# File 'lib/glassfrog/rest/request.rb', line 18

def request_method
  @request_method
end

#uriAddressable::URI

Returns:

  • (Addressable::URI)


20
21
22
# File 'lib/glassfrog/rest/request.rb', line 20

def uri
  @uri
end

Instance Method Details

#performArray<Hash>, Boolean

Sends the Request.

Returns:

  • (Array<Hash>, Boolean)

    The fetched or created parameters, or boolean reflecting whether the request was successful.



50
51
52
53
54
# 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