Class: Commute::Stack
- Inherits:
-
Object
- Object
- Commute::Stack
- Extended by:
- Forwardable
- Defined in:
- lib/commute/stack.rb
Overview
A Stack is a sequence of layers that define a transfornation of a request/response body.
Each Layer has a optional name. Options of the Layer can be passed in the Context‘s options under the the equally named key.
A Stack can both process a request and a response body (that can be either object, as long as it conforms with the first Layer).
Defined Under Namespace
Classes: Sequence
Instance Attribute Summary collapse
-
#request ⇒ Object
The request Sequence of the stack.
-
#response ⇒ Object
The response Sequence of the stack.
Instance Method Summary collapse
- #<<(layer) ⇒ Object
-
#clone ⇒ Object
Override the clone method to clone the layers.
-
#initialize(*layers) ⇒ Stack
constructor
Create a new Stack with a few layers.
-
#run_request(body, options = {}) ⇒ Object
Run a request body through the stack.
-
#run_response(body, options = {}) ⇒ Object
Run a response body through the stack.
Constructor Details
#initialize(*layers) ⇒ Stack
Create a new Commute::Stack with a few layers.
55 56 57 58 |
# File 'lib/commute/stack.rb', line 55 def initialize *layers @request = Sequence.new layers @response = Sequence.new layers.reverse end |
Instance Attribute Details
#request ⇒ Object
The request Sequence of the stack.
48 49 50 |
# File 'lib/commute/stack.rb', line 48 def request @request end |
#response ⇒ Object
The response Sequence of the stack.
52 53 54 |
# File 'lib/commute/stack.rb', line 52 def response @response end |
Instance Method Details
#<<(layer) ⇒ Object
61 62 63 64 |
# File 'lib/commute/stack.rb', line 61 def << layer @request << layer @response.insert 0, layer end |
#clone ⇒ Object
Override the clone method to clone the layers.
97 98 99 100 101 102 |
# File 'lib/commute/stack.rb', line 97 def clone stack = super stack.request = stack.request.clone stack.response = stack.response.clone stack end |
#run_request(body, options = {}) ⇒ Object
Run a request body through the stack.
72 73 74 75 76 77 |
# File 'lib/commute/stack.rb', line 72 def run_request body, = {} @request.inject(body) do |body, layer| # Run through layer to modfiy the request layer.request body, [layer.name] || {} end end |
#run_response(body, options = {}) ⇒ Object
Run a response body through the stack. We want the response to have the inverse manipulations
so we invert the stack.
87 88 89 90 91 92 |
# File 'lib/commute/stack.rb', line 87 def run_response body, = {} @response.inject(body) do |body, layer| # Run through layer to modfiy the request layer.response body, [layer.name] || {} end end |