Class: Commute::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/commute/context.rb

Overview

A context represents all what can better define a HTTP request/response trip. This includes:

* An url.
* A partial request/response stack that defines request/response body transformations.
* Partial options for the request, the stack or behavior of certain {Api} calls.

Contexts work incrementally to provide a full context (all required options and behaviour) for a HTTP request/response trip.

Every Context has notion of an underlying api to easily make requests.

Contexts can be extended through #with. This way you can add options/override options, alter the stack or specify an url.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, options, stack) ⇒ Context

Create a new Context.

Parameters:

  • api (Api)

    The underlying api.

  • options (Hash)

    all kinds of options (for the request, api call and stack layers).

  • stack (Stack)

    The request/response stack.



37
38
39
40
41
# File 'lib/commute/context.rb', line 37

def initialize api, options, stack
  @api = api
  @options = options
  @stack = stack
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Any method missing should be a shortcut to an api call with the current context. The options and the block that are passed are used to create a new context. This context is then passed to the API call to return another new context.



127
128
129
130
131
132
133
134
# File 'lib/commute/context.rb', line 127

def method_missing name, *args, &block
  # Extract the options from the method arguments.
  options = args.first || {}
  # Create the new context.
  context = with options, &block
  # Call the Api. This returns a new context.
  @api.send name, context
end

Instance Attribute Details

#optionsObject

Returns The options of the context.

Returns:

  • The options of the context.



26
27
28
# File 'lib/commute/context.rb', line 26

def options
  @options
end

#stackObject

Returns The stack of the context.

Returns:

  • The stack of the context.



30
31
32
# File 'lib/commute/context.rb', line 30

def stack
  @stack
end

Instance Method Details

#[](key) ⇒ Object

Returns The value associated with that key.

Parameters:

  • key (Symbol)

    The key to get from the options.

Returns:

  • (Object)

    The value associated with that key.



46
# File 'lib/commute/context.rb', line 46

def_delegator :@options, :[], :[]

#build {|response, result| ... } ⇒ Typhoeus::Request

TODO:

This should work with some kind of Typhoeus adapter.

TODO:

Actually the prepare method should only be called right before the request is sent. We do not take queueing into account here. Timestamps for authorization could expire. Can be solved by extending Typhoeus::Request with an UnpreparedRequest and preparing it right before it is sent.

Builds a Typhoeus::Request from the context.

Yield Parameters:

  • response (Typhoeus::Response)

    The response object as we got it from Typhoeus.

  • result (Object)

    The result of the body after going through the Stack.

Returns:

  • (Typhoeus::Request)

    The built Typhoeus Request.



83
84
85
86
87
88
89
90
91
92
# File 'lib/commute/context.rb', line 83

def build &callback
  # Call after hook if necessary.
  context = if @api.respond_to? :after
    @api.after self
  else
    self
  end
  # Build the real context (with or without after hook).
  context.build_without_after &callback
end

#build_without_after(&callback) ⇒ Object

Builds the context without calling the after hook.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/commute/context.rb', line 97

def build_without_after &callback
  # Run the request body through the stack.
  body = self[:body]
  raw_body = if body && body != ''
    @stack.run_request body, options
  else
    body
  end
  # Build the Typhoeus Request.
  options[:body] = raw_body
  request = Typhoeus::ContextualRequest.new self, self[:url], options
  # Attach an on_complete handler that wraps the callback.
  request.on_complete do |response|
    # Run the response body through the stack.
    body = response.body
    result = if body && body != '' && response.success?
      @stack.run_response response.body, options
    else
      body
    end
    # Call the real commute callback.
    callback.call response, result if callback
  end
  # Return the request.
  request
end

#commute(request) ⇒ Object

Queues a request for later parallel execution.

Parameters:

  • request (Typhoeus::Request)

    The request to queue for later execution.



51
# File 'lib/commute/context.rb', line 51

def_delegator :@api, :commute, :commute

#rushObject

Executes all the requests in the queue of the api.



55
# File 'lib/commute/context.rb', line 55

def_delegator :@api, :rush, :rush

#with(options = {}) {|stack| ... } ⇒ Object

Create a new context from this context by providing new options and stack modifications.

Parameters:

Yield Parameters:

Yield Returns:



63
64
65
66
67
68
69
# File 'lib/commute/context.rb', line 63

def with options = {}
  # Alter the stack if necessary
  stack = @stack.clone
  yield(stack) if block_given?
  # Create a new context.
  Context.new @api, mix(@options, options), stack
end