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.
Files changed (56) hide show
  1. data/.todo +15 -0
  2. data/Gemfile +5 -2
  3. data/commute.gemspec +2 -1
  4. data/examples/gist_api.rb +71 -0
  5. data/examples/highrise_task_api.rb +59 -0
  6. data/examples/pastie_api.rb +18 -0
  7. data/lib/commute/aspects/caching.rb +37 -0
  8. data/lib/commute/aspects/crud.rb +41 -0
  9. data/lib/commute/aspects/pagination.rb +16 -0
  10. data/lib/commute/aspects/url.rb +57 -0
  11. data/lib/commute/common/basic_auth.rb +20 -0
  12. data/lib/commute/common/cache.rb +43 -0
  13. data/lib/commute/common/chemicals.rb +39 -0
  14. data/lib/commute/common/conditional.rb +27 -0
  15. data/lib/commute/common/em-synchrony_adapter.rb +29 -0
  16. data/lib/commute/common/em_http_request_adapter.rb +57 -0
  17. data/lib/commute/common/json.rb +28 -0
  18. data/lib/commute/common/typhoeus_adapter.rb +40 -0
  19. data/lib/commute/common/xml.rb +7 -0
  20. data/lib/commute/configuration.rb +8 -0
  21. data/lib/commute/core/api.rb +116 -0
  22. data/lib/commute/core/builder.rb +261 -0
  23. data/lib/commute/core/commuter.rb +116 -0
  24. data/lib/commute/core/context.rb +63 -0
  25. data/lib/commute/core/processors/code_status_processor.rb +40 -0
  26. data/lib/commute/core/processors/hook.rb +14 -0
  27. data/lib/commute/core/processors/request_builder.rb +26 -0
  28. data/lib/commute/core/processors/sequencer.rb +46 -0
  29. data/lib/commute/core/request.rb +58 -0
  30. data/lib/commute/core/response.rb +18 -0
  31. data/lib/commute/core/sequence.rb +180 -0
  32. data/lib/commute/core/stack.rb +145 -0
  33. data/lib/commute/version.rb +1 -1
  34. data/lib/commute.rb +4 -2
  35. data/spec/commute/aspects/caching_spec.rb +12 -0
  36. data/spec/commute/aspects/url_spec.rb +61 -0
  37. data/spec/commute/core/api_spec.rb +70 -0
  38. data/spec/commute/core/builder_spec.rb +123 -0
  39. data/spec/commute/core/commuter_spec.rb +64 -0
  40. data/spec/commute/core/processors/code_status_processor_spec.rb +5 -0
  41. data/spec/commute/core/processors/hook_spec.rb +25 -0
  42. data/spec/commute/core/processors/request_builder_spec.rb +25 -0
  43. data/spec/commute/core/processors/sequencer_spec.rb +33 -0
  44. data/spec/commute/core/sequence_spec.rb +190 -0
  45. data/spec/commute/core/stack_spec.rb +96 -0
  46. data/spec/spec_helper.rb +2 -3
  47. metadata +73 -18
  48. data/lib/commute/adapters/typhoeus.rb +0 -13
  49. data/lib/commute/api.rb +0 -86
  50. data/lib/commute/context.rb +0 -154
  51. data/lib/commute/layer.rb +0 -16
  52. data/lib/commute/stack.rb +0 -104
  53. data/spec/commute/api_spec.rb +0 -97
  54. data/spec/commute/context_spec.rb +0 -140
  55. data/spec/commute/layer_spec.rb +0 -22
  56. data/spec/commute/stack_spec.rb +0 -125
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'commute/core/commuter'
3
+
4
+ describe Commute::Commuter do
5
+
6
+ let(:context) { mock }
7
+
8
+ let(:commuter) { Commute::Commuter.new(context, 1) }
9
+
10
+ describe '#change' do
11
+ it 'should take a block that processes the value' do
12
+ commuter.change { |number| number + 1 }
13
+ commuter.get.must_equal 2
14
+ end
15
+
16
+ describe 'when there is no value' do
17
+ let(:commuter) { Commute::Commuter.new(context, nil) }
18
+
19
+ it 'should not call the block' do
20
+ block = Proc.new { |n| n+1 }
21
+ block.expects(:call).never
22
+ commuter.change &block
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#tag' do
28
+ it 'should tag the commuter' do
29
+ commuter.tag :cool
30
+ commuter.tagged?(:cool).must_equal true
31
+ end
32
+ end
33
+
34
+ describe '#context' do
35
+ it 'should return the associated context' do
36
+ commuter.context.must_equal context
37
+ end
38
+ end
39
+
40
+ describe '#parameters' do
41
+ it 'should return parameters needed for a processor to process the commuter' do
42
+ context.expects(:[]).with(:processor).returns operation: :multiply
43
+ commuter.parameters(:processor).must_equal operation: :multiply
44
+ end
45
+ end
46
+
47
+ describe '#wait' do
48
+ it 'waits for a delay and does something when done waiting' do
49
+ done = lambda { @done = true }
50
+ delay = Proc.new { |&done| done.call }
51
+ commuter.delay &delay
52
+ commuter.delayed?.must_equal true
53
+ commuter.wait &done
54
+ @done.must_equal true
55
+
56
+ done = lambda { @done = false }
57
+ delay = Proc.new { }
58
+ commuter.delay &delay
59
+ done.expects(:call).never
60
+ commuter.wait &done
61
+ @done.must_equal true
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+ require 'commute/core/processors/code_status_processor'
3
+
4
+ describe Commute::CodeStatusProcessor do
5
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'commute/core/processors/hook'
3
+
4
+ describe Commute::Hook do
5
+
6
+ let(:hook) { Commute::Hook.new }
7
+
8
+ let(:value) { mock }
9
+
10
+ let(:commuter) { Commute::Commuter.new mock, value }
11
+
12
+ describe 'when no handler is specified' do
13
+ it 'should do nothing' do
14
+ hook.call commuter
15
+ end
16
+ end
17
+
18
+ describe 'when a handler is specified' do
19
+ it 'should call the handler with the commuter value' do
20
+ handler = mock
21
+ handler.expects(:call).with(value)
22
+ hook.call commuter, handler
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'commute/core/processors/request_builder'
3
+
4
+ describe Commute::RequestBuilder do
5
+
6
+ let(:context) { Commute::Context.new }
7
+
8
+ let(:builder) { Commute::RequestBuilder.new }
9
+
10
+ it 'should transform the context' do
11
+ c = context.get.
12
+ transform(:id) { |request, id| request.url = "http://pastie.org/pastes/#{id}/text" }.
13
+ with(id: 1)
14
+
15
+ commuter = Commute::Commuter.new(c, nil)
16
+
17
+ builder.call commuter
18
+
19
+ commuter.get.tap { |request|
20
+ request.host.must_equal 'pastie.org'
21
+ request.path.must_equal '/pastes/1/text'
22
+ request.method.must_equal :get
23
+ }
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'commute/core/processors/sequencer'
3
+
4
+ describe Commute::Sequencer do
5
+
6
+ let(:stack) { Commute::Stack.new {} }
7
+
8
+ let(:context) { Commute::Context.new stack }
9
+
10
+ let(:commuter) { Commute::Commuter.new context, mock }
11
+
12
+ let(:sequencer) { Commute::Sequencer.new(:response) }
13
+
14
+ describe 'The sequence is not defined in the stack' do
15
+ it 'should do nothing' do
16
+ sequencer.call commuter
17
+ end
18
+ end
19
+
20
+ describe 'The sequence is defined in the stack' do
21
+ let(:sequence) { mock }
22
+ let(:commuter) do
23
+ Commute::Commuter.new context.using { |stack, main|
24
+ stack.add sequence, :response
25
+ }.context, mock
26
+ end
27
+
28
+ it 'should call the sequence' do
29
+ sequence.expects(:call).once
30
+ sequencer.call commuter
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,190 @@
1
+ require 'spec_helper'
2
+ require 'commute/core/sequence'
3
+
4
+ describe Commute::Sequence do
5
+
6
+ let(:sequence) { Commute::Sequence.new }
7
+
8
+ let(:processor1) {
9
+ lambda { |result, subject, options = {}| result + 1 }
10
+ }
11
+
12
+ let(:processor2) {
13
+ lambda { |result, subject, options = {}| result * 2 }
14
+ }
15
+
16
+ let(:processor3) {
17
+ Class.new {
18
+ @id = :test
19
+
20
+ def call result, subject, options = {}
21
+ result ** options[:power]
22
+ end
23
+ }.new
24
+ }
25
+
26
+ describe '#new' do
27
+ it 'should alter the sequence when a block is provided' do
28
+ sequence = Commute::Sequence.new do |s|
29
+ s.append processor1
30
+ end
31
+ sequence.processors.size.must_equal 1
32
+ end
33
+ end
34
+
35
+ describe '#alter' do
36
+ it 'should alter the sequence in a block when the arity is 1' do
37
+ sequence.alter do |s|
38
+ s.append processor1
39
+ end
40
+ sequence.processors.size.must_equal 1
41
+ end
42
+
43
+ it 'should evaluate the block when the arity is 0' do
44
+ sequence.alter do
45
+ append Proc.new { |n| n.data += 1 }
46
+ end
47
+ sequence.processors.size.must_equal 1
48
+ end
49
+ end
50
+
51
+ describe '#processors' do
52
+ it 'should return an empty array when there are no processors' do
53
+ sequence.processors.must_equal []
54
+ end
55
+
56
+ it 'should not return a modifiable array' do
57
+ sequence.processors << 'test'
58
+ sequence.processors.must_be_empty
59
+ end
60
+ end
61
+
62
+ describe '#processors=' do
63
+
64
+ it 'should not exist' do
65
+ Proc.new { sequence.processors = nil }.must_raise NoMethodError
66
+ end
67
+ end
68
+
69
+ describe '#call' do
70
+ let(:processor1) do
71
+ Proc.new do |commuter|
72
+ commuter.change { |n| n+1 }
73
+ end
74
+ end
75
+
76
+ let(:processor2) do
77
+ Proc.new do |commuter, operation|
78
+ commuter.change { |n| n.send operation, n }
79
+ end
80
+ end
81
+
82
+ let(:commuter) { Commute::Commuter.new mock, 1 }
83
+
84
+ it 'should call all processors' do
85
+ commuter.expects(:parameters).with(nil).never
86
+ commuter.expects(:parameters).with(:operator).returns(:**)
87
+ sequence.append(processor1)
88
+ sequence.append(processor2, as: :operator)
89
+ sequence.call commuter
90
+ commuter.get.must_equal 4
91
+ end
92
+
93
+ # This is not tested implicitely due to the Proc usage.
94
+ it 'should call processors with the correct number of arguments' do
95
+ commuter.expects(:parameters).with(:operator).returns(:+)
96
+ processor1 = mock
97
+ processor1.expects(:call).with(commuter)
98
+ processor2 = mock
99
+ processor2.expects(:call).with(commuter, :+)
100
+ sequence.append processor1
101
+ sequence.append processor2, as: :operator
102
+ sequence.call commuter
103
+ end
104
+
105
+ it 'should be a processor itself' do
106
+ commuter.expects(:parameters).with(:operator).returns(:**)
107
+
108
+ subsequence = Commute::Sequence.new do |s|
109
+ s.append processor1
110
+ end
111
+
112
+ sequence = Commute::Sequence.new do |s|
113
+ s.append subsequence
114
+ s.append processor2, as: :operator
115
+ end
116
+
117
+ sequence.call commuter
118
+ commuter.get.must_equal 4
119
+ end
120
+
121
+ it 'should return nothing' do
122
+ Commute::Sequence.new { |s|
123
+ s.append processor1
124
+ }.call(commuter).must_equal nil
125
+ end
126
+ end
127
+
128
+ describe '#append' do
129
+ it 'should append the processor to the tail' do
130
+ sequence.append processor1
131
+ sequence.processors.must_equal [processor1]
132
+ assert_equal sequence.append(processor2), processor2
133
+ sequence.processors.must_equal [processor1, processor2]
134
+ end
135
+
136
+ it 'should be able to override the default processor id' do
137
+ sequence.append processor1, as: :increment
138
+ sequence.at(:increment).call(1, nil).must_equal 2
139
+
140
+ sequence.append processor3, as: :power
141
+ sequence.at(:power).must_equal processor3
142
+ end
143
+
144
+ it 'should be able to append after a specified processor' do
145
+ sequence.append processor1, as: :increment
146
+ sequence.append processor2
147
+
148
+ sequence.append processor3, as: :power, after: :increment
149
+ sequence.processors.must_equal [processor1, processor3, processor2]
150
+ end
151
+ end
152
+
153
+ describe '#remove' do
154
+ before do
155
+ sequence.append processor1
156
+ sequence.append processor2, as: :multiplier
157
+ end
158
+
159
+ it 'should remove the processor if the processor itself is given' do
160
+ processor = sequence.remove processor1
161
+ (processor == processor1).must_equal true
162
+ sequence.processors.must_equal [processor2]
163
+ assert_equal sequence.at(:multiplier), processor2
164
+ end
165
+
166
+ it 'should remove the processor if its id is given' do
167
+ processor = sequence.remove :multiplier
168
+ (processor == processor2).must_equal true
169
+ sequence.processors.must_equal [processor1]
170
+ sequence.at(:multiplier).must_be_nil
171
+ end
172
+ end
173
+
174
+ describe '#at' do
175
+ it 'should index processors with a default id' do
176
+ sequence.append processor3
177
+ sequence.at(:test).must_equal processor3
178
+ end
179
+ end
180
+
181
+ describe '#dup' do
182
+ it 'should duplicate the sequence' do
183
+ sequence.append processor1
184
+ cloned = sequence.dup
185
+ cloned.append processor2
186
+ sequence.processors.size.must_equal 1
187
+ cloned.processors.size.must_equal 2
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'commute/core/context'
3
+ require 'commute/core/stack'
4
+
5
+ describe Commute::Stack do
6
+
7
+ let(:sequence) do
8
+ Commute::Sequence.new do
9
+ append Proc.new { |n| n.data += 1 }
10
+ end
11
+ end
12
+
13
+ let(:stack) do
14
+ Commute::Stack.new do |stack, main|
15
+ end
16
+ end
17
+
18
+ describe '#initialize' do
19
+ describe 'when no sequence is given' do
20
+ it 'should yield the main sequence for the stack when the arity is 1' do
21
+ stack = Commute::Stack.new do |sequence|
22
+ sequence.append Proc.new {}
23
+ end
24
+ stack.sequence.processors.size.must_equal 1
25
+ end
26
+
27
+ it 'should yield the stack and the main sequence when the arity is 2' do
28
+ stack = Commute::Stack.new do |stack, sequence|
29
+ sequence.append Proc.new {}
30
+ end
31
+ stack.sequence.processors.size.must_equal 1
32
+ end
33
+ end
34
+
35
+ describe 'when a sequence is given' do
36
+ it 'should use that sequence' do
37
+ sequence = Commute::Sequence.new do
38
+ append Proc.new {}
39
+ end
40
+ stack = Commute::Stack.new sequence
41
+ stack.sequence.processors.size.must_equal 1
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '#add' do
47
+ it 'should add a sequence to the stack by name' do
48
+ stack.add sequence, :calculation
49
+ stack.sequence(:calculation).must_equal sequence
50
+ end
51
+ end
52
+
53
+ describe '#sequence' do
54
+ it 'should add the sequence when it was not found' do
55
+ stack.sequence(:calculation) do
56
+ append Proc.new { |c| c.set(c.get + 1) }
57
+ end
58
+
59
+ stack.sequence(:calculation).processors.size.must_equal 1
60
+ end
61
+ end
62
+
63
+ describe '#sequencer' do
64
+ it 'should allow us to embed a sequence in a sequence' do
65
+ stack = Commute::Stack.new do |stack, sequence|
66
+ sequence.append Commute::Sequencer.new(:calculation)
67
+ sequence.append Proc.new { |c|
68
+ c.change { |n| "The result is #{n}" }
69
+ }
70
+
71
+ stack.sequence(:calculation) do
72
+ append Proc.new { |c| c.set(c.get + 1) }
73
+ append Proc.new { |c| c.set(c.get + 2) }
74
+ end
75
+ end
76
+ commuter = Commute::Commuter.new(Commute::Context.new(stack), 1)
77
+ stack.call(commuter)
78
+ commuter.get.must_equal 'The result is 4'
79
+ end
80
+ end
81
+
82
+ describe '#dup' do
83
+ it 'should duplicate the stack and all its sequences' do
84
+ stack.add sequence, :calculation
85
+ cloned = stack.dup.alter do
86
+ sequence.append Proc.new {}
87
+
88
+ sequence(:calculation).append Proc.new {}
89
+ end
90
+ stack.sequence.processors.size.must_equal 0
91
+ stack.sequence(:calculation).processors.size.must_equal 1
92
+ cloned.sequence.processors.size.must_equal 1
93
+ cloned.sequence(:calculation).processors.size.must_equal 2
94
+ end
95
+ end
96
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,9 +6,8 @@ SimpleCov.start
6
6
  require 'minitest/autorun'
7
7
  require 'minitest/spec'
8
8
 
9
- require 'mocha'
9
+ require 'dominance/minitest'
10
10
 
11
- require 'webmock'
12
- require 'webmock/minitest'
11
+ require 'mocha'
13
12
 
14
13
  require 'commute'
metadata CHANGED
@@ -1,18 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.2.0.rc.1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mattias Putman
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-18 00:00:00.000000000 Z
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.5.0.rc
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.5.0.rc
30
+ - !ruby/object:Gem::Dependency
31
+ name: em-http-request
16
32
  requirement: !ruby/object:Gem::Requirement
17
33
  none: false
18
34
  requirements:
@@ -163,6 +179,7 @@ extensions: []
163
179
  extra_rdoc_files: []
164
180
  files:
165
181
  - .gitignore
182
+ - .todo
166
183
  - .travis.yml
167
184
  - Gemfile
168
185
  - Guardfile
@@ -170,17 +187,48 @@ files:
170
187
  - README.md
171
188
  - Rakefile
172
189
  - commute.gemspec
190
+ - examples/gist_api.rb
191
+ - examples/highrise_task_api.rb
192
+ - examples/pastie_api.rb
173
193
  - lib/commute.rb
174
- - lib/commute/adapters/typhoeus.rb
175
- - lib/commute/api.rb
176
- - lib/commute/context.rb
177
- - lib/commute/layer.rb
178
- - lib/commute/stack.rb
194
+ - lib/commute/aspects/caching.rb
195
+ - lib/commute/aspects/crud.rb
196
+ - lib/commute/aspects/pagination.rb
197
+ - lib/commute/aspects/url.rb
198
+ - lib/commute/common/basic_auth.rb
199
+ - lib/commute/common/cache.rb
200
+ - lib/commute/common/chemicals.rb
201
+ - lib/commute/common/conditional.rb
202
+ - lib/commute/common/em-synchrony_adapter.rb
203
+ - lib/commute/common/em_http_request_adapter.rb
204
+ - lib/commute/common/json.rb
205
+ - lib/commute/common/typhoeus_adapter.rb
206
+ - lib/commute/common/xml.rb
207
+ - lib/commute/configuration.rb
208
+ - lib/commute/core/api.rb
209
+ - lib/commute/core/builder.rb
210
+ - lib/commute/core/commuter.rb
211
+ - lib/commute/core/context.rb
212
+ - lib/commute/core/processors/code_status_processor.rb
213
+ - lib/commute/core/processors/hook.rb
214
+ - lib/commute/core/processors/request_builder.rb
215
+ - lib/commute/core/processors/sequencer.rb
216
+ - lib/commute/core/request.rb
217
+ - lib/commute/core/response.rb
218
+ - lib/commute/core/sequence.rb
219
+ - lib/commute/core/stack.rb
179
220
  - lib/commute/version.rb
180
- - spec/commute/api_spec.rb
181
- - spec/commute/context_spec.rb
182
- - spec/commute/layer_spec.rb
183
- - spec/commute/stack_spec.rb
221
+ - spec/commute/aspects/caching_spec.rb
222
+ - spec/commute/aspects/url_spec.rb
223
+ - spec/commute/core/api_spec.rb
224
+ - spec/commute/core/builder_spec.rb
225
+ - spec/commute/core/commuter_spec.rb
226
+ - spec/commute/core/processors/code_status_processor_spec.rb
227
+ - spec/commute/core/processors/hook_spec.rb
228
+ - spec/commute/core/processors/request_builder_spec.rb
229
+ - spec/commute/core/processors/sequencer_spec.rb
230
+ - spec/commute/core/sequence_spec.rb
231
+ - spec/commute/core/stack_spec.rb
184
232
  - spec/spec_helper.rb
185
233
  homepage: http://challengee.github.com/commute/
186
234
  licenses: []
@@ -197,9 +245,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
245
  required_rubygems_version: !ruby/object:Gem::Requirement
198
246
  none: false
199
247
  requirements:
200
- - - ! '>='
248
+ - - ! '>'
201
249
  - !ruby/object:Gem::Version
202
- version: '0'
250
+ version: 1.3.1
203
251
  requirements: []
204
252
  rubyforge_project:
205
253
  rubygems_version: 1.8.19
@@ -209,9 +257,16 @@ summary: ! 'Commute helps you to: 1) Dynamically build HTTP requests for your fa
209
257
  HTTP library (currently only Typhoeus). 2) Easily process request and response bodies.
210
258
  3) Execute parallel HTTP requests.'
211
259
  test_files:
212
- - spec/commute/api_spec.rb
213
- - spec/commute/context_spec.rb
214
- - spec/commute/layer_spec.rb
215
- - spec/commute/stack_spec.rb
260
+ - spec/commute/aspects/caching_spec.rb
261
+ - spec/commute/aspects/url_spec.rb
262
+ - spec/commute/core/api_spec.rb
263
+ - spec/commute/core/builder_spec.rb
264
+ - spec/commute/core/commuter_spec.rb
265
+ - spec/commute/core/processors/code_status_processor_spec.rb
266
+ - spec/commute/core/processors/hook_spec.rb
267
+ - spec/commute/core/processors/request_builder_spec.rb
268
+ - spec/commute/core/processors/sequencer_spec.rb
269
+ - spec/commute/core/sequence_spec.rb
270
+ - spec/commute/core/stack_spec.rb
216
271
  - spec/spec_helper.rb
217
272
  has_rdoc:
@@ -1,13 +0,0 @@
1
- module Commute
2
- module Typhoeus
3
-
4
- class ContextualRequest < ::Typhoeus::Request
5
- attr_reader :context
6
-
7
- def initialize context, url, options = {}
8
- @context = context
9
- super(url, options)
10
- end
11
- end
12
- end
13
- end
data/lib/commute/api.rb DELETED
@@ -1,86 +0,0 @@
1
- require 'singleton'
2
- require 'forwardable'
3
-
4
- require 'typhoeus'
5
-
6
- require 'commute/context'
7
-
8
- module Commute
9
-
10
- # An Api holds:
11
- # * Contexts of defaults.
12
- # * Named contexts (= Api calls)
13
- #
14
- # Every Api is a singleton, it contains no state, only some name configurations (contexts).
15
- class Api
16
- include Singleton
17
-
18
- # @!attribute queue
19
- # @return List of requests waiting for execution.
20
- attr_reader :queue
21
-
22
- class << self
23
- extend Forwardable
24
-
25
- def_delegators :instance, :default, :raw, :with
26
-
27
- # Call missing methods on the default context.
28
- def method_missing name, *args, &block
29
- default.send name, *args, &block
30
- end
31
- end
32
-
33
- # Initializes an Api with a parallel manager.
34
- def initialize
35
- @queue = []
36
- @hydra = ::Typhoeus::Hydra.new
37
- end
38
-
39
- # A pretty standard starting point is the `default` context.
40
- # An Api class can implement it to provide some default options and stack layers.
41
- #
42
- # @return [Context] A default context.
43
- def default
44
- @default ||= raw
45
- end
46
-
47
- # Get a raw context without any defaults.
48
- #
49
- # @return [Context] A raw context for this api.
50
- def raw
51
- @raw ||= Context.new self, {}, Stack.new
52
- end
53
-
54
- # Start scoping on this Api.
55
- # Creates a context with provided options and stack.
56
- #
57
- # @return [Context] The created context.
58
- def with options = {}, &stack_mod
59
- default.with options, &stack_mod
60
- end
61
-
62
- # Queue a request for later parallel execution.
63
- #
64
- # @param request [Typhoeus::Request] The request to queue.
65
- def commute request
66
- @queue << request
67
- end
68
-
69
- # Executes all requests in the queue in parallel.
70
- #
71
- # @param request [Typhoeus::Request] Last request to add to the queue.
72
- # Shortcut to commute and rush one request in one line.
73
- def rush request = nil
74
- commute request if request
75
- # Authorize each request right before executing
76
- @queue.each do |request|
77
- request.headers['Authorization'] = authorize(request.context) if respond_to? :authorize
78
- @hydra.queue request
79
- end
80
- # Clear the queue.
81
- @queue.clear
82
- # Run all requests.
83
- @hydra.run
84
- end
85
- end
86
- end