Module: Hyperion::Requestor

Defined in:
lib/hyperion/requestor.rb

Instance Method Summary collapse

Instance Method Details

#request(route, opts = {}) {|rendered| ... } ⇒ Object

Parameters:

  • route (RestRoute)

    The route to request

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :body (Object)

    The payload to POST/PUT. Usually a Hash or Array.

  • :also_handle (Hash<predicate, transformer>)

    Additional handlers to use besides the default handlers. A predicate is an integer HTTP code, an integer Range of HTTP codes, a HyperionStatus enumeration value, or a predicate proc. A transformer is a procedure which accepts a HyperionResult and returns the final value to return from request

  • :render (Proc)

    A transformer, usually a proc returned by as or as_many. Only called on HTTP 200.

  • :timeout (Integer)

    The limit of the entire request in seconds. The default is 0 which means there will be no timeout during transfer; there still may be a timeout during connection.

Yields:

  • (rendered)

    Yields to allow an additional transformation. Only called on HTTP 200.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hyperion/requestor.rb', line 20

def request(route, opts={}, &project)
  Hyperion::Util.guard_param(route, 'a RestRoute', RestRoute)
  Hyperion::Util.guard_param(opts, 'an options hash', Hash)

  body = opts[:body]
  headers = opts[:headers] || {}
  additional_handler_hash = opts[:also_handle] || {}
  timeout = opts[:timeout] || 0
  render = opts[:render] || Proc.identity
  project = project || Proc.identity

  Hyperion.request(route, body: body, additional_headers: headers, timeout: timeout) do |result|
    all_handlers = [hash_handler(additional_handler_hash),
                    handler_from_including_class,
                    built_in_handler(project, render)]

    all_handlers.each { |handlers| handlers.call(result) }
    fallthrough(result)
  end
end