commute 0.1.2 → 0.2.0.rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.todo +15 -0
- data/Gemfile +5 -2
- data/commute.gemspec +2 -1
- data/examples/gist_api.rb +71 -0
- data/examples/highrise_task_api.rb +59 -0
- data/examples/pastie_api.rb +18 -0
- data/lib/commute/aspects/caching.rb +37 -0
- data/lib/commute/aspects/crud.rb +41 -0
- data/lib/commute/aspects/pagination.rb +16 -0
- data/lib/commute/aspects/url.rb +57 -0
- data/lib/commute/common/basic_auth.rb +20 -0
- data/lib/commute/common/cache.rb +43 -0
- data/lib/commute/common/chemicals.rb +39 -0
- data/lib/commute/common/conditional.rb +27 -0
- data/lib/commute/common/em-synchrony_adapter.rb +29 -0
- data/lib/commute/common/em_http_request_adapter.rb +57 -0
- data/lib/commute/common/json.rb +28 -0
- data/lib/commute/common/typhoeus_adapter.rb +40 -0
- data/lib/commute/common/xml.rb +7 -0
- data/lib/commute/configuration.rb +8 -0
- data/lib/commute/core/api.rb +116 -0
- data/lib/commute/core/builder.rb +261 -0
- data/lib/commute/core/commuter.rb +116 -0
- data/lib/commute/core/context.rb +63 -0
- data/lib/commute/core/processors/code_status_processor.rb +40 -0
- data/lib/commute/core/processors/hook.rb +14 -0
- data/lib/commute/core/processors/request_builder.rb +26 -0
- data/lib/commute/core/processors/sequencer.rb +46 -0
- data/lib/commute/core/request.rb +58 -0
- data/lib/commute/core/response.rb +18 -0
- data/lib/commute/core/sequence.rb +180 -0
- data/lib/commute/core/stack.rb +145 -0
- data/lib/commute/version.rb +1 -1
- data/lib/commute.rb +4 -2
- data/spec/commute/aspects/caching_spec.rb +12 -0
- data/spec/commute/aspects/url_spec.rb +61 -0
- data/spec/commute/core/api_spec.rb +70 -0
- data/spec/commute/core/builder_spec.rb +123 -0
- data/spec/commute/core/commuter_spec.rb +64 -0
- data/spec/commute/core/processors/code_status_processor_spec.rb +5 -0
- data/spec/commute/core/processors/hook_spec.rb +25 -0
- data/spec/commute/core/processors/request_builder_spec.rb +25 -0
- data/spec/commute/core/processors/sequencer_spec.rb +33 -0
- data/spec/commute/core/sequence_spec.rb +190 -0
- data/spec/commute/core/stack_spec.rb +96 -0
- data/spec/spec_helper.rb +2 -3
- metadata +73 -18
- data/lib/commute/adapters/typhoeus.rb +0 -13
- data/lib/commute/api.rb +0 -86
- data/lib/commute/context.rb +0 -154
- data/lib/commute/layer.rb +0 -16
- data/lib/commute/stack.rb +0 -104
- data/spec/commute/api_spec.rb +0 -97
- data/spec/commute/context_spec.rb +0 -140
- data/spec/commute/layer_spec.rb +0 -22
- data/spec/commute/stack_spec.rb +0 -125
data/lib/commute/context.rb
DELETED
@@ -1,154 +0,0 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
|
3
|
-
require 'commute/stack'
|
4
|
-
require 'commute/adapters/typhoeus'
|
5
|
-
|
6
|
-
module Commute
|
7
|
-
|
8
|
-
# A context represents all what can better define a HTTP request/response trip. This includes:
|
9
|
-
# * An url.
|
10
|
-
# * A partial request/response stack that defines request/response body transformations.
|
11
|
-
# * Partial options for the request, the stack or behavior of certain {Api} calls.
|
12
|
-
#
|
13
|
-
# Contexts work incrementally to provide a full context (all required options and behaviour)
|
14
|
-
# for a HTTP request/response trip.
|
15
|
-
#
|
16
|
-
# Every Context has notion of an underlying api to easily make requests.
|
17
|
-
#
|
18
|
-
# Contexts can be extended through {#with}. This way you can add options/override options,
|
19
|
-
# alter the stack or specify an url.
|
20
|
-
#
|
21
|
-
class Context
|
22
|
-
extend Forwardable
|
23
|
-
|
24
|
-
# @!attribute options
|
25
|
-
# @return The options of the context.
|
26
|
-
attr_accessor :options
|
27
|
-
|
28
|
-
# @!attribute stack
|
29
|
-
# @return The stack of the context.
|
30
|
-
attr_accessor :stack
|
31
|
-
|
32
|
-
# Create a new Context.
|
33
|
-
#
|
34
|
-
# @param api [Api] The underlying api.
|
35
|
-
# @param options [Hash] all kinds of options (for the request, api call and stack layers).
|
36
|
-
# @param stack [Stack] The request/response stack.
|
37
|
-
def initialize api, options, stack
|
38
|
-
@api = api
|
39
|
-
@options = options
|
40
|
-
@stack = stack
|
41
|
-
end
|
42
|
-
|
43
|
-
# @!method [](key)
|
44
|
-
# @param key [Symbol] The key to get from the options.
|
45
|
-
# @return [Object] The value associated with that key.
|
46
|
-
def_delegator :@options, :[], :[]
|
47
|
-
|
48
|
-
# @!method commute(request)
|
49
|
-
# Queues a request for later parallel execution.
|
50
|
-
# @param request [Typhoeus::Request] The request to queue for later execution.
|
51
|
-
def_delegator :@api, :commute, :commute
|
52
|
-
|
53
|
-
# @!method rush
|
54
|
-
# Executes all the requests in the queue of the api.
|
55
|
-
def_delegator :@api, :rush, :rush
|
56
|
-
|
57
|
-
# Create a new context from this context by providing new options and stack modifications.
|
58
|
-
#
|
59
|
-
# @param options [Hash] Options for the new {Context}.
|
60
|
-
#
|
61
|
-
# @yieldparam stack [Stack] The stack in this {Context}.
|
62
|
-
# @yieldreturn [Stack] The modified {Stack}.
|
63
|
-
def with options = {}
|
64
|
-
# Alter the stack if necessary
|
65
|
-
stack = @stack.clone
|
66
|
-
yield(stack) if block_given?
|
67
|
-
# Create a new context.
|
68
|
-
Context.new @api, mix(@options, options), stack
|
69
|
-
end
|
70
|
-
|
71
|
-
# Builds a Typhoeus::Request from the context.
|
72
|
-
#
|
73
|
-
# @todo This should work with some kind of Typhoeus adapter.
|
74
|
-
# @todo Actually the prepare method should only be called right before the request
|
75
|
-
# is sent. We do not take queueing into account here. Timestamps for authorization could
|
76
|
-
# expire. Can be solved by extending Typhoeus::Request with an UnpreparedRequest and
|
77
|
-
# preparing it right before it is sent.
|
78
|
-
#
|
79
|
-
# @yieldparam response [Typhoeus::Response] The response object as we got it from Typhoeus.
|
80
|
-
# @yieldparam result [Object] The result of the body after going through the {Stack}.
|
81
|
-
#
|
82
|
-
# @return [Typhoeus::Request] The built Typhoeus Request.
|
83
|
-
def build &callback
|
84
|
-
# Call after hook if necessary.
|
85
|
-
context = if @api.respond_to? :after
|
86
|
-
@api.after self
|
87
|
-
else
|
88
|
-
self
|
89
|
-
end
|
90
|
-
# Build the real context (with or without after hook).
|
91
|
-
context.build_without_after &callback
|
92
|
-
end
|
93
|
-
|
94
|
-
# Builds the context without calling the after hook.
|
95
|
-
#
|
96
|
-
# @private
|
97
|
-
def build_without_after &callback
|
98
|
-
# Run the request body through the stack.
|
99
|
-
body = self[:body]
|
100
|
-
raw_body = if body && body != ''
|
101
|
-
@stack.run_request body, options
|
102
|
-
else
|
103
|
-
body
|
104
|
-
end
|
105
|
-
# Build the Typhoeus Request.
|
106
|
-
options[:body] = raw_body
|
107
|
-
request = Typhoeus::ContextualRequest.new self, self[:url], options
|
108
|
-
# Attach an on_complete handler that wraps the callback.
|
109
|
-
request.on_complete do |response|
|
110
|
-
# Run the response body through the stack.
|
111
|
-
body = response.body
|
112
|
-
result = if body && body != '' && response.success?
|
113
|
-
@stack.run_response response.body, options
|
114
|
-
else
|
115
|
-
body
|
116
|
-
end
|
117
|
-
# Call the real commute callback.
|
118
|
-
callback.call response, result if callback
|
119
|
-
end
|
120
|
-
# Return the request.
|
121
|
-
request
|
122
|
-
end
|
123
|
-
|
124
|
-
# Any method missing should be a shortcut to an api call with the current context.
|
125
|
-
# The options and the block that are passed are used to create a new context.
|
126
|
-
# This context is then passed to the API call to return another new context.
|
127
|
-
def method_missing name, *args, &block
|
128
|
-
# Extract the options from the method arguments.
|
129
|
-
options = args.first || {}
|
130
|
-
# Create the new context.
|
131
|
-
context = with options, &block
|
132
|
-
# Call the Api. This returns a new context.
|
133
|
-
@api.send name, context
|
134
|
-
end
|
135
|
-
|
136
|
-
private
|
137
|
-
|
138
|
-
# Merge a 2-level deep hash of options.
|
139
|
-
#
|
140
|
-
# @param options [Hash] The base options hash.
|
141
|
-
# @param override_options [Hash] The options hash considered as more important.
|
142
|
-
#
|
143
|
-
# @return [Hash] A mixed hash.
|
144
|
-
def mix options, override_options
|
145
|
-
options.merge(override_options) { |key, old_value, new_value|
|
146
|
-
if old_value.kind_of? Hash
|
147
|
-
old_value.merge new_value
|
148
|
-
else
|
149
|
-
new_value
|
150
|
-
end
|
151
|
-
}
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
data/lib/commute/layer.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module Commute
|
2
|
-
|
3
|
-
# @todo more docs and tests.
|
4
|
-
class Layer < Struct.new(:name)
|
5
|
-
|
6
|
-
# Default request forwards to the call method.
|
7
|
-
def request request, options
|
8
|
-
call request, options if respond_to? :call
|
9
|
-
end
|
10
|
-
|
11
|
-
# Default response forwards to the call method.
|
12
|
-
def response response, options
|
13
|
-
call response, options if respond_to? :call
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
data/lib/commute/stack.rb
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
require 'delegate'
|
3
|
-
|
4
|
-
require 'commute/layer'
|
5
|
-
|
6
|
-
module Commute
|
7
|
-
|
8
|
-
# A {Stack} is a sequence of layers that define a transfornation of a request/response body.
|
9
|
-
#
|
10
|
-
# Each {Layer} has a optional name. Options of the {Layer} can be passed in the
|
11
|
-
# {Context}'s options under the the equally named key.
|
12
|
-
#
|
13
|
-
# A Stack can both process a request and a response body (that can be either object,
|
14
|
-
# as long as it conforms with the first {Layer}).
|
15
|
-
class Stack
|
16
|
-
extend Forwardable
|
17
|
-
|
18
|
-
# Represents one way of the {Stack}
|
19
|
-
# Delegates methods to the underlying layer array.
|
20
|
-
class Sequence < SimpleDelegator
|
21
|
-
|
22
|
-
# Remove some layers from this sequence.
|
23
|
-
#
|
24
|
-
# @param names [<Symbol>] The names of the layers that must be removed.
|
25
|
-
def without! *names
|
26
|
-
reject! { |layer| names.include? layer.name }
|
27
|
-
end
|
28
|
-
|
29
|
-
# Insert a layer before another layer.
|
30
|
-
#
|
31
|
-
# @param reference [Symbol] The layer to insert before.
|
32
|
-
# @param insert [Layer] The layer to insert.
|
33
|
-
def before! reference, inserting
|
34
|
-
insert(index { |layer| layer.name == reference }, inserting)
|
35
|
-
end
|
36
|
-
|
37
|
-
# Insert a layer after another layer.
|
38
|
-
#
|
39
|
-
# @param reference [Symbol] The layer to insert after.
|
40
|
-
# @param insert [Layer] The layer to insert.
|
41
|
-
def after! reference, insert
|
42
|
-
insert((index { |layer| layer.name == reference }) + 1, insert)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# @!attribute request
|
47
|
-
# The request {Sequence} of the stack.
|
48
|
-
attr_accessor :request
|
49
|
-
|
50
|
-
# @!attribute response
|
51
|
-
# The response {Sequence} of the stack.
|
52
|
-
attr_accessor :response
|
53
|
-
|
54
|
-
# Create a new {Stack} with a few layers.
|
55
|
-
def initialize *layers
|
56
|
-
@request = Sequence.new layers
|
57
|
-
@response = Sequence.new layers.reverse
|
58
|
-
end
|
59
|
-
|
60
|
-
# @param layer [Layer] The layer to be added to the stack.
|
61
|
-
def << layer
|
62
|
-
@request << layer
|
63
|
-
@response.insert 0, layer
|
64
|
-
end
|
65
|
-
|
66
|
-
# Run a request body through the stack.
|
67
|
-
#
|
68
|
-
# @param body [Object] The request body.
|
69
|
-
# @param options [Hash] Options for the layers.
|
70
|
-
#
|
71
|
-
# @return [Object] the transformed body.
|
72
|
-
def run_request body, options = {}
|
73
|
-
@request.inject(body) do |body, layer|
|
74
|
-
# Run through layer to modfiy the request
|
75
|
-
layer.request body, options[layer.name] || {}
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
# Run a response body through the stack.
|
80
|
-
# We want the response to have the inverse manipulations
|
81
|
-
# so we invert the stack.
|
82
|
-
#
|
83
|
-
# @param body [Object] The request body.
|
84
|
-
# @param options [Hash] Options for the layers.
|
85
|
-
#
|
86
|
-
# @return [Object] the transformed body.
|
87
|
-
def run_response body, options = {}
|
88
|
-
@response.inject(body) do |body, layer|
|
89
|
-
# Run through layer to modfiy the request
|
90
|
-
layer.response body, options[layer.name] || {}
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# Override the clone method to clone the layers.
|
95
|
-
#
|
96
|
-
# @return A duplicate of the stack.
|
97
|
-
def clone
|
98
|
-
stack = super
|
99
|
-
stack.request = stack.request.clone
|
100
|
-
stack.response = stack.response.clone
|
101
|
-
stack
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
data/spec/commute/api_spec.rb
DELETED
@@ -1,97 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Commute::Api do
|
4
|
-
|
5
|
-
class TestApi < Commute::Api
|
6
|
-
end
|
7
|
-
|
8
|
-
let(:default) { TestApi.with(user: 'mattias') }
|
9
|
-
|
10
|
-
let(:request1) { default.with(url: 'http://www.example.com', method: :get).build }
|
11
|
-
let(:request2) { default.with(url: 'http://www.example.com', method: :post).build }
|
12
|
-
let(:request3) { default.with(url: 'http://www.example.com', method: :delete).build }
|
13
|
-
|
14
|
-
after do
|
15
|
-
TestApi.instance.queue.clear
|
16
|
-
end
|
17
|
-
|
18
|
-
describe '#method_missing' do
|
19
|
-
before do
|
20
|
-
TestApi.stubs(:default).returns(default)
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should forward calls to the singleton with the default context' do
|
24
|
-
TestApi.instance.expects(:get).with { |c|
|
25
|
-
c.options.must_equal user: 'mattias'
|
26
|
-
}
|
27
|
-
TestApi.get
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'should take no default context if the raw context is used' do
|
31
|
-
TestApi.instance.expects(:get).with { |c|
|
32
|
-
c.options.must_equal id: 1
|
33
|
-
}
|
34
|
-
TestApi.raw.get id: 1
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'should scope on the default context' do
|
38
|
-
TestApi.instance.expects(:get).with { |c|
|
39
|
-
c.options.must_equal user: 'mattias', id: 1
|
40
|
-
}
|
41
|
-
TestApi.get id: 1
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe '#commute' do
|
46
|
-
it 'should add requests to the queue' do
|
47
|
-
default.commute request1
|
48
|
-
default.commute request2
|
49
|
-
TestApi.instance.queue.size.must_equal 2
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe '#rush' do
|
54
|
-
describe 'no authorize' do
|
55
|
-
before do
|
56
|
-
@stub_get = stub_request(:get, "http://www.example.com")
|
57
|
-
@stub_post = stub_request(:post, "http://www.example.com")
|
58
|
-
@stub_delete = stub_request(:delete, "http://www.example.com")
|
59
|
-
|
60
|
-
default.commute request1
|
61
|
-
default.commute request2
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should clear the queue' do
|
65
|
-
default.rush
|
66
|
-
TestApi.instance.queue.size.must_equal 0
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'should run all requests' do
|
70
|
-
default.rush
|
71
|
-
assert_requested(@stub_get)
|
72
|
-
assert_requested(@stub_post)
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'should make the additional requests given to rush' do
|
76
|
-
default.rush request3
|
77
|
-
assert_requested(@stub_get)
|
78
|
-
assert_requested(@stub_post)
|
79
|
-
assert_requested(@stub_delete)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
describe 'authorize method provided' do
|
84
|
-
it 'should authorize if a methode is provided' do
|
85
|
-
request = default.with(url: 'http://www.example.com', method: :get).build
|
86
|
-
TestApi.instance.stubs(:authorize).returns 'Authorized'
|
87
|
-
stub_request(:get, "http://www.example.com")
|
88
|
-
# Actually make the request
|
89
|
-
default.rush request
|
90
|
-
# TestApi.instance.stubs(:authorize).returns('Authorized')
|
91
|
-
assert_requested(:get, "http://www.example.com", :times => 1) { |request|
|
92
|
-
request.headers['Authorization'] = 'Authorized'
|
93
|
-
}
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
@@ -1,140 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'webmock/minitest'
|
3
|
-
|
4
|
-
describe Commute::Context do
|
5
|
-
|
6
|
-
let(:context) {
|
7
|
-
Commute::Context.new api, options, stack
|
8
|
-
}
|
9
|
-
|
10
|
-
let(:stack) { Commute::Stack.new(stub, stub) }
|
11
|
-
|
12
|
-
let(:api) { stub }
|
13
|
-
|
14
|
-
let(:options) {
|
15
|
-
{ body: 1, two: 2, params: { a: 'a' }}
|
16
|
-
}
|
17
|
-
|
18
|
-
describe '#with' do
|
19
|
-
it 'should keep its current options when the context is incremented' do
|
20
|
-
new_context = context.with body: 'one', params: { a: 'A', b: 'b' }
|
21
|
-
context.options.must_equal body: 1, two: 2, params: { a: 'a' }
|
22
|
-
new_context.options.must_equal body: 'one', two: 2, params: { a: 'A', b: 'b' }
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'should not alter the current stack when the context is incremented' do
|
26
|
-
new_context = context.with do |stack|
|
27
|
-
stack << stub
|
28
|
-
end
|
29
|
-
context.stack.request.size.must_equal 2
|
30
|
-
new_context.stack.request.size.must_equal 3
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe '#[]' do
|
35
|
-
it 'should give the specified option' do
|
36
|
-
context[:body].must_equal 1
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe '#build' do
|
41
|
-
let(:stack) { stub_everything }
|
42
|
-
|
43
|
-
it 'should return a typhoeus request with the raw body' do
|
44
|
-
stack.expects(:run_request).with(1, options).returns 2
|
45
|
-
request = context.build
|
46
|
-
request.body.must_equal 2
|
47
|
-
end
|
48
|
-
|
49
|
-
describe 'when called with a callback' do
|
50
|
-
let(:options) {
|
51
|
-
{url: 'www.example.com', method: :get, body: 'sent'}
|
52
|
-
}
|
53
|
-
|
54
|
-
before do
|
55
|
-
# Stub the stack
|
56
|
-
stack.stubs(:run_request).with('sent', options).returns('sent transformed')
|
57
|
-
stack.stubs(:run_response).with('returned', options).returns('returned transformed')
|
58
|
-
end
|
59
|
-
|
60
|
-
describe 'when the requests succeeds with good status' do
|
61
|
-
it 'should call the callback with the original response and the transformed result' do
|
62
|
-
# Stub the http request.
|
63
|
-
stub_request(:get, 'www.example.com').with(body: 'sent transformed').to_return(body: 'returned')
|
64
|
-
# Build the request with a callback
|
65
|
-
callback = Proc.new {}
|
66
|
-
callback.expects(:call).with { |response, result|
|
67
|
-
response.body.must_equal 'returned'
|
68
|
-
result.must_equal 'returned transformed'
|
69
|
-
}
|
70
|
-
request = context.build &callback
|
71
|
-
# Run the request.
|
72
|
-
Typhoeus::Hydra.hydra.queue request
|
73
|
-
Typhoeus::Hydra.hydra.run
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
describe 'when the request succeeds with bad status' do
|
78
|
-
it 'should call the callback with the original response and no result' do
|
79
|
-
# Stub the http request.
|
80
|
-
stub_request(:get, 'www.example.com').with(body: 'sent transformed').to_return(\
|
81
|
-
status: [500, "Internal Server Error"])
|
82
|
-
# Response stack should never be triggered.
|
83
|
-
stack.expects(:run_response).never
|
84
|
-
# Build the request with a callback
|
85
|
-
callback = Proc.new {}
|
86
|
-
callback.expects(:call).with { |response, result|
|
87
|
-
response.code.must_equal 500
|
88
|
-
result.must_equal ''
|
89
|
-
}
|
90
|
-
request = context.build &callback
|
91
|
-
# Run the request.
|
92
|
-
Typhoeus::Hydra.hydra.queue request
|
93
|
-
Typhoeus::Hydra.hydra.run
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
describe 'when an after hook is configured' do
|
98
|
-
it 'should call it with the context' do
|
99
|
-
api.stubs(:after).with(context).returns(context.with(after: true))
|
100
|
-
request = context.build
|
101
|
-
request.context[:after].must_equal true
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
describe '#method_missing' do
|
108
|
-
it 'should call the api with an incremented context when only given options' do
|
109
|
-
api.expects(:test_api_call).with { |context|
|
110
|
-
context[:enhanced].must_equal true
|
111
|
-
}
|
112
|
-
context.test_api_call enhanced: true
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'should call the api with an incremented context when given no options' do
|
116
|
-
api.expects(:test_api_call).with { |incr_context|
|
117
|
-
context.options.must_equal context.options
|
118
|
-
}
|
119
|
-
context.test_api_call
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'should call the api with an incremented context when given options and a block' do
|
123
|
-
api.expects(:test_api_call).with { |context|
|
124
|
-
context[:enhanced].must_equal true
|
125
|
-
context.stack.request.size.must_equal 3
|
126
|
-
}
|
127
|
-
context.test_api_call enhanced: true do |stack|
|
128
|
-
stack << stub
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'should forward commute and rush to the api' do
|
134
|
-
request = stub
|
135
|
-
api.expects(:commute).with(request)
|
136
|
-
api.expects(:rush)
|
137
|
-
context.commute request
|
138
|
-
context.rush
|
139
|
-
end
|
140
|
-
end
|
data/spec/commute/layer_spec.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Commute::Layer do
|
4
|
-
|
5
|
-
let(:layer) { Commute::Layer.new }
|
6
|
-
|
7
|
-
describe 'when there is no call method' do
|
8
|
-
it 'should do nothing' do
|
9
|
-
layer.request nil, nil
|
10
|
-
layer.response nil, nil
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
describe 'when there is a call methode' do
|
15
|
-
it 'should call it' do
|
16
|
-
request = stub
|
17
|
-
options = { test: 1 }
|
18
|
-
layer.expects(:call).with(request, options)
|
19
|
-
layer.request request, options
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
data/spec/commute/stack_spec.rb
DELETED
@@ -1,125 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Commute::Stack do
|
4
|
-
|
5
|
-
let(:stack) { Commute::Stack.new }
|
6
|
-
|
7
|
-
let(:layer1) { stub.tap { |stub|
|
8
|
-
stub.stubs(:name).returns(:first)
|
9
|
-
}}
|
10
|
-
|
11
|
-
let(:layer2) { stub.tap { |stub|
|
12
|
-
stub.stubs(:name).returns(:second)
|
13
|
-
}}
|
14
|
-
|
15
|
-
let(:layer3) { stub.tap { |stub|
|
16
|
-
stub.stubs(:name).returns(:third)
|
17
|
-
}}
|
18
|
-
|
19
|
-
describe '#size' do
|
20
|
-
it 'should return the size of the stack' do
|
21
|
-
stack.request.size.must_equal 0
|
22
|
-
stack << stub
|
23
|
-
stack << stub
|
24
|
-
stack.request.size.must_equal 2
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe '#run_request' do
|
29
|
-
before do
|
30
|
-
stack << layer1
|
31
|
-
stack << layer2
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should run the body through all layers' do
|
35
|
-
layer1.expects(:request).with(1, {}).returns(2)
|
36
|
-
layer2.expects(:request).with(2, {}).returns(3)
|
37
|
-
|
38
|
-
stack.run_request(1).must_equal 3
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should route the options to the layers' do
|
42
|
-
layer1.expects(:request).with(1, 'options').returns(2)
|
43
|
-
layer2.expects(:request).with(2, user: 'defunkt')
|
44
|
-
|
45
|
-
stack.run_request 1, first: 'options', second: {
|
46
|
-
user: 'defunkt'
|
47
|
-
}
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe '#run_response' do
|
52
|
-
before do
|
53
|
-
stack << layer1
|
54
|
-
stack << layer2
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'should run the body through all layers' do
|
58
|
-
layer2.expects(:response).with(1, {}).returns(2)
|
59
|
-
layer1.expects(:response).with(2, {}).returns(3)
|
60
|
-
|
61
|
-
stack.run_response(1).must_equal 3
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should route the options to the layers' do
|
65
|
-
layer2.expects(:response).with(1, user: 'defunkt').returns(2)
|
66
|
-
layer1.expects(:response).with(2, 'options')
|
67
|
-
|
68
|
-
stack.run_response 1, first: 'options', second: {
|
69
|
-
user: 'defunkt'
|
70
|
-
}
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe Commute::Stack::Sequence do
|
75
|
-
|
76
|
-
describe '#without!' do
|
77
|
-
before do
|
78
|
-
stack << layer1
|
79
|
-
stack << layer2
|
80
|
-
stack << layer3
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'should remove the correct layers from the stack' do
|
84
|
-
stack.request.without! :second, :third
|
85
|
-
stack.request.size.must_equal 1
|
86
|
-
stack.response.size.must_equal 3
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe '#before!' do
|
91
|
-
before do
|
92
|
-
stack << layer1
|
93
|
-
stack << layer3
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'should insert a layer before another one' do
|
97
|
-
stack.request.before! :third, layer2
|
98
|
-
stack.request[1].must_equal layer2
|
99
|
-
stack.request.size.must_equal 3
|
100
|
-
stack.response.size.must_equal 2
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
describe '#after!' do
|
105
|
-
before do
|
106
|
-
stack << layer1
|
107
|
-
stack << layer3
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'should insert a layer after another one' do
|
111
|
-
stack.request.after! :first, layer2
|
112
|
-
stack.request[1].must_equal layer2
|
113
|
-
stack.request.size.must_equal 3
|
114
|
-
stack.response.size.must_equal 2
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'should insert after the last one' do
|
118
|
-
stack.request.after! :third, layer2
|
119
|
-
stack.request.last.must_equal layer2
|
120
|
-
stack.request.size.must_equal 3
|
121
|
-
stack.response.size.must_equal 2
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|