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.

Yields:

  • (rendered)

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hyperion/requestor.rb', line 17

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] || {}
  render = opts[:render] || Proc.identity
  project = project || Proc.identity

  Hyperion.request(route, body, headers) 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