Class: Commute::Context
- Inherits:
-
Object
- Object
- Commute::Context
- 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
-
#options ⇒ Object
The options of the context.
-
#stack ⇒ Object
The stack of the context.
Instance Method Summary collapse
-
#[](key) ⇒ Object
The value associated with that key.
-
#build {|response, result| ... } ⇒ Typhoeus::Request
Builds a Typhoeus::Request from the context.
-
#build_without_after(&callback) ⇒ Object
Builds the context without calling the after hook.
-
#commute(request) ⇒ Object
Queues a request for later parallel execution.
-
#initialize(api, options, stack) ⇒ Context
constructor
Create a new Context.
-
#method_missing(name, *args, &block) ⇒ Object
Any method missing should be a shortcut to an api call with the current context.
-
#rush ⇒ Object
Executes all the requests in the queue of the api.
-
#with(options = {}) {|stack| ... } ⇒ Object
Create a new context from this context by providing new options and stack modifications.
Constructor Details
#initialize(api, options, stack) ⇒ Context
Create a new Context.
37 38 39 40 41 |
# File 'lib/commute/context.rb', line 37 def initialize api, , stack @api = api @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. = args.first || {} # Create the new context. context = with , &block # Call the Api. This returns a new context. @api.send name, context end |
Instance Attribute Details
#options ⇒ Object
Returns The options of the context.
26 27 28 |
# File 'lib/commute/context.rb', line 26 def @options end |
#stack ⇒ Object
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.
46 |
# File 'lib/commute/context.rb', line 46 def_delegator :@options, :[], :[] |
#build {|response, result| ... } ⇒ Typhoeus::Request
This should work with some kind of Typhoeus adapter.
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.
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, else body end # Build the Typhoeus Request. [:body] = raw_body request = Typhoeus::ContextualRequest.new self, self[:url], # 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, 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.
51 |
# File 'lib/commute/context.rb', line 51 def_delegator :@api, :commute, :commute |
#rush ⇒ Object
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.
63 64 65 66 67 68 69 |
# File 'lib/commute/context.rb', line 63 def with = {} # Alter the stack if necessary stack = @stack.clone yield(stack) if block_given? # Create a new context. Context.new @api, mix(@options, ), stack end |