commute 0.2.0.rc.2 → 0.3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.todo +28 -12
  2. data/README.md +0 -1
  3. data/commute.gemspec +4 -5
  4. data/lib/commute/common/basic_auth.rb +10 -9
  5. data/lib/commute/common/caching.rb +208 -0
  6. data/lib/commute/common/chemicals.rb +47 -24
  7. data/lib/commute/common/eventmachine.rb +68 -0
  8. data/lib/commute/common/synchrony.rb +42 -0
  9. data/lib/commute/common/typhoeus.rb +64 -0
  10. data/lib/commute/core/api.rb +42 -29
  11. data/lib/commute/core/builder.rb +4 -15
  12. data/lib/commute/core/context.rb +156 -15
  13. data/lib/commute/core/http.rb +124 -0
  14. data/lib/commute/core/layer.rb +187 -0
  15. data/lib/commute/core/sequence.rb +83 -132
  16. data/lib/commute/core/stack.rb +63 -72
  17. data/lib/commute/core/status.rb +45 -0
  18. data/lib/commute/core/util/event_emitter.rb +58 -0
  19. data/lib/commute/core/util/path.rb +37 -0
  20. data/lib/commute/core/util/stream.rb +141 -0
  21. data/lib/commute/extensions/crud.rb +88 -0
  22. data/lib/commute/extensions/param.rb +20 -0
  23. data/lib/commute/extensions/url.rb +53 -0
  24. data/lib/commute/version.rb +1 -1
  25. data/spec/commute/common/caching_spec.rb +158 -0
  26. data/spec/commute/common/eventmachine_spec.rb +74 -0
  27. data/spec/commute/common/typhoeus_spec.rb +67 -0
  28. data/spec/commute/core/api_spec.rb +3 -1
  29. data/spec/commute/core/builder_spec.rb +8 -8
  30. data/spec/commute/core/http_spec.rb +39 -0
  31. data/spec/commute/core/layer_spec.rb +81 -0
  32. data/spec/commute/core/sequence_spec.rb +36 -150
  33. data/spec/commute/core/stack_spec.rb +33 -83
  34. data/spec/commute/core/util/event_emitter_spec.rb +35 -0
  35. data/spec/commute/core/util/path_spec.rb +29 -0
  36. data/spec/commute/core/util/stream_spec.rb +90 -0
  37. data/spec/commute/extensions/url_spec.rb +76 -0
  38. data/spec/spec_helper.rb +3 -1
  39. metadata +61 -48
  40. data/examples/gist_api.rb +0 -71
  41. data/examples/highrise_task_api.rb +0 -59
  42. data/examples/pastie_api.rb +0 -18
  43. data/lib/commute/aspects/caching.rb +0 -37
  44. data/lib/commute/aspects/crud.rb +0 -41
  45. data/lib/commute/aspects/pagination.rb +0 -16
  46. data/lib/commute/aspects/url.rb +0 -57
  47. data/lib/commute/common/cache.rb +0 -43
  48. data/lib/commute/common/conditional.rb +0 -27
  49. data/lib/commute/common/em-synchrony_adapter.rb +0 -29
  50. data/lib/commute/common/em_http_request_adapter.rb +0 -57
  51. data/lib/commute/common/typhoeus_adapter.rb +0 -40
  52. data/lib/commute/common/xml.rb +0 -7
  53. data/lib/commute/core/commuter.rb +0 -116
  54. data/lib/commute/core/processors/code_status_processor.rb +0 -40
  55. data/lib/commute/core/processors/hook.rb +0 -14
  56. data/lib/commute/core/processors/request_builder.rb +0 -26
  57. data/lib/commute/core/processors/sequencer.rb +0 -46
  58. data/lib/commute/core/request.rb +0 -58
  59. data/lib/commute/core/response.rb +0 -18
  60. data/spec/commute/aspects/caching_spec.rb +0 -12
  61. data/spec/commute/aspects/url_spec.rb +0 -61
  62. data/spec/commute/core/commuter_spec.rb +0 -64
  63. data/spec/commute/core/processors/code_status_processor_spec.rb +0 -5
  64. data/spec/commute/core/processors/hook_spec.rb +0 -25
  65. data/spec/commute/core/processors/request_builder_spec.rb +0 -25
  66. data/spec/commute/core/processors/sequencer_spec.rb +0 -33
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'commute/core/context'
3
+ require 'commute/core/http'
4
+ require 'commute/extensions/url'
5
+
6
+ describe Commute::Extension::Url do
7
+
8
+ class TestApi < Commute::Api
9
+ include Commute::Extension::Url
10
+ end
11
+
12
+ class TestApiClass < Commute::Api
13
+ include Commute::Extension::Url
14
+ url 'http://www.example.com'
15
+ end
16
+
17
+ let(:request) { Commute::Http::Request.new nil }
18
+
19
+ it 'should be available both on class and instance level' do
20
+ TestApi.builder.respond_to?(:url).must_equal true
21
+ TestApi.new.respond_to?(:url).must_equal true
22
+ end
23
+
24
+ it 'should work on class level' do
25
+ TestApiClass.builder.transformations.size.must_equal 1
26
+ TestApiClass.new.transformations.size.must_equal 1
27
+ end
28
+
29
+ it 'should create an url transformation without parameters' do
30
+ transformation = example('http://api.example.com')
31
+ # Hash that acts as a context.
32
+ context = Commute::Context.new
33
+ transformation.call(request, context)
34
+ # Checks.
35
+ request.uri.scheme.must_equal 'http'
36
+ request.uri.host.must_equal 'api.example.com'
37
+ request.uri.path.must_be_empty
38
+ end
39
+
40
+ it 'should create an url transformation with parameters in the host and path' do
41
+ transformation = example('https://api.%{root}.com:3000/1/%{scope}/%{id}.xml')
42
+ # Hash that acts as a context.
43
+ context = Commute::Context.new(nil, {
44
+ root: 'example',
45
+ scope: 'people',
46
+ id: '3'
47
+ })
48
+ transformation.call(request, context)
49
+ # Checks.
50
+ request.uri.scheme.must_equal 'https'
51
+ request.uri.host.must_equal 'api.example.com'
52
+ request.uri.path.must_equal '/1/people/3.xml'
53
+ request.uri.port.must_equal 3000
54
+ end
55
+
56
+ it 'should create an url transformation with parameters in the host and path when only some of the values are present' do
57
+ transformation = example('https://api.%{root}.com:3000/1/%{scope}/%{id}.xml')
58
+ # Hash that acts as a context.
59
+ context = Commute::Context.new(nil, {
60
+ root: 'example',
61
+ scope: 'people'
62
+ })
63
+ transformation.call(request, context)
64
+ # Checks.
65
+ request.uri.scheme.must_equal 'https'
66
+ request.uri.host.must_equal 'api.example.com'
67
+ request.uri.path.must_equal '/1/people.xml'
68
+ request.uri.port.must_equal 3000
69
+ end
70
+
71
+ private
72
+
73
+ def example pattern
74
+ TestApi.new(Commute::Context.new).url(pattern).transformations.first
75
+ end
76
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,8 +6,10 @@ SimpleCov.start
6
6
  require 'minitest/autorun'
7
7
  require 'minitest/spec'
8
8
 
9
+ require 'webmock/minitest'
10
+
9
11
  require 'dominance/minitest'
10
12
 
11
- require 'mocha'
13
+ require 'mocha/setup'
12
14
 
13
15
  require 'commute'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.rc.2
4
+ version: 0.3.0.pre
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-26 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - '='
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.5.0.rc
22
- type: :runtime
21
+ version: '0'
22
+ type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - '='
27
+ - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.5.0.rc
29
+ version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: em-http-request
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -35,7 +35,23 @@ dependencies:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
- type: :runtime
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: yajl-ruby
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
39
55
  prerelease: false
40
56
  version_requirements: !ruby/object:Gem::Requirement
41
57
  none: false
@@ -76,7 +92,7 @@ dependencies:
76
92
  - !ruby/object:Gem::Version
77
93
  version: '0'
78
94
  - !ruby/object:Gem::Dependency
79
- name: guard
95
+ name: rb-fsevent
80
96
  requirement: !ruby/object:Gem::Requirement
81
97
  none: false
82
98
  requirements:
@@ -92,7 +108,7 @@ dependencies:
92
108
  - !ruby/object:Gem::Version
93
109
  version: '0'
94
110
  - !ruby/object:Gem::Dependency
95
- name: guard-minitest
111
+ name: guard
96
112
  requirement: !ruby/object:Gem::Requirement
97
113
  none: false
98
114
  requirements:
@@ -108,7 +124,7 @@ dependencies:
108
124
  - !ruby/object:Gem::Version
109
125
  version: '0'
110
126
  - !ruby/object:Gem::Dependency
111
- name: yard
127
+ name: guard-minitest
112
128
  requirement: !ruby/object:Gem::Requirement
113
129
  none: false
114
130
  requirements:
@@ -124,7 +140,7 @@ dependencies:
124
140
  - !ruby/object:Gem::Version
125
141
  version: '0'
126
142
  - !ruby/object:Gem::Dependency
127
- name: simplecov
143
+ name: yard
128
144
  requirement: !ruby/object:Gem::Requirement
129
145
  none: false
130
146
  requirements:
@@ -140,7 +156,7 @@ dependencies:
140
156
  - !ruby/object:Gem::Version
141
157
  version: '0'
142
158
  - !ruby/object:Gem::Dependency
143
- name: webmock
159
+ name: simplecov
144
160
  requirement: !ruby/object:Gem::Requirement
145
161
  none: false
146
162
  requirements:
@@ -156,7 +172,7 @@ dependencies:
156
172
  - !ruby/object:Gem::Version
157
173
  version: '0'
158
174
  - !ruby/object:Gem::Dependency
159
- name: yajl-ruby
175
+ name: webmock
160
176
  requirement: !ruby/object:Gem::Requirement
161
177
  none: false
162
178
  requirements:
@@ -187,48 +203,43 @@ files:
187
203
  - README.md
188
204
  - Rakefile
189
205
  - commute.gemspec
190
- - examples/gist_api.rb
191
- - examples/highrise_task_api.rb
192
- - examples/pastie_api.rb
193
206
  - lib/commute.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
207
  - lib/commute/common/basic_auth.rb
199
- - lib/commute/common/cache.rb
208
+ - lib/commute/common/caching.rb
200
209
  - 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
210
+ - lib/commute/common/eventmachine.rb
204
211
  - lib/commute/common/json.rb
205
- - lib/commute/common/typhoeus_adapter.rb
206
- - lib/commute/common/xml.rb
212
+ - lib/commute/common/synchrony.rb
213
+ - lib/commute/common/typhoeus.rb
207
214
  - lib/commute/configuration.rb
208
215
  - lib/commute/core/api.rb
209
216
  - lib/commute/core/builder.rb
210
- - lib/commute/core/commuter.rb
211
217
  - 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/http.rb
219
+ - lib/commute/core/layer.rb
218
220
  - lib/commute/core/sequence.rb
219
221
  - lib/commute/core/stack.rb
222
+ - lib/commute/core/status.rb
223
+ - lib/commute/core/util/event_emitter.rb
224
+ - lib/commute/core/util/path.rb
225
+ - lib/commute/core/util/stream.rb
226
+ - lib/commute/extensions/crud.rb
227
+ - lib/commute/extensions/param.rb
228
+ - lib/commute/extensions/url.rb
220
229
  - lib/commute/version.rb
221
- - spec/commute/aspects/caching_spec.rb
222
- - spec/commute/aspects/url_spec.rb
230
+ - spec/commute/common/caching_spec.rb
231
+ - spec/commute/common/eventmachine_spec.rb
232
+ - spec/commute/common/typhoeus_spec.rb
223
233
  - spec/commute/core/api_spec.rb
224
234
  - 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
235
+ - spec/commute/core/http_spec.rb
236
+ - spec/commute/core/layer_spec.rb
230
237
  - spec/commute/core/sequence_spec.rb
231
238
  - spec/commute/core/stack_spec.rb
239
+ - spec/commute/core/util/event_emitter_spec.rb
240
+ - spec/commute/core/util/path_spec.rb
241
+ - spec/commute/core/util/stream_spec.rb
242
+ - spec/commute/extensions/url_spec.rb
232
243
  - spec/spec_helper.rb
233
244
  homepage: http://challengee.github.com/commute/
234
245
  licenses: []
@@ -257,16 +268,18 @@ summary: ! 'Commute helps you to: 1) Dynamically build HTTP requests for your fa
257
268
  HTTP library (currently only Typhoeus). 2) Easily process request and response bodies.
258
269
  3) Execute parallel HTTP requests.'
259
270
  test_files:
260
- - spec/commute/aspects/caching_spec.rb
261
- - spec/commute/aspects/url_spec.rb
271
+ - spec/commute/common/caching_spec.rb
272
+ - spec/commute/common/eventmachine_spec.rb
273
+ - spec/commute/common/typhoeus_spec.rb
262
274
  - spec/commute/core/api_spec.rb
263
275
  - 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
276
+ - spec/commute/core/http_spec.rb
277
+ - spec/commute/core/layer_spec.rb
269
278
  - spec/commute/core/sequence_spec.rb
270
279
  - spec/commute/core/stack_spec.rb
280
+ - spec/commute/core/util/event_emitter_spec.rb
281
+ - spec/commute/core/util/path_spec.rb
282
+ - spec/commute/core/util/stream_spec.rb
283
+ - spec/commute/extensions/url_spec.rb
271
284
  - spec/spec_helper.rb
272
285
  has_rdoc:
data/examples/gist_api.rb DELETED
@@ -1,71 +0,0 @@
1
- require 'commute'
2
- require 'commute/common/json'
3
- require 'commute/common/basic_auth'
4
-
5
- class GistApi < Commute::Api
6
-
7
- transform do |request, context|
8
- if context[:auth][:username]
9
- request.url = "https://api.github.com/gists"
10
- else
11
- request.url = "https://api.github.com/#{context[:auth][:username]}/gists"
12
- end
13
- request.url << "/#{context[:id]}" if context[:id]
14
- end
15
-
16
- using(:request) do
17
- append Commute::Common::Json::Render.new
18
- append Commute::Common::BasicAuth.new
19
- end
20
-
21
- using(:response) do
22
- append Commute::Common::Json::Parse.new
23
- end
24
-
25
- def find id = nil
26
- # Id for url.
27
- where id: id if id
28
- # Get method.
29
- get
30
- end
31
-
32
- def update gist = nil
33
- body(gist).patch
34
- end
35
-
36
- def create gist = nil
37
- body(gist).post
38
- end
39
-
40
- private
41
-
42
- def body gist = nil
43
- with gist: gist if gist
44
- transform(:gist) { |request, gist| request.body = gist }
45
- end
46
- end
47
-
48
- ID = '46ecd83c5e13cca5a18e'
49
-
50
- api = GistApi.with auth: {
51
- username: 'challengee',
52
- password: 'challengeez0'
53
- }
54
-
55
- # gist, status = api.find(ID).run
56
- # p status
57
-
58
- # gist[:description] = 'cool'
59
-
60
- gist = {
61
- :public => false
62
- :files => {
63
- "file1.txt" => {
64
- :content => 'test'
65
- }
66
- }
67
- }
68
-
69
- gist, status = api.create(gist).run
70
- p gist
71
- p status
@@ -1,59 +0,0 @@
1
- require 'uri'
2
-
3
- require 'commute'
4
-
5
- require 'commute/aspects/url'
6
- require 'commute/aspects/crud'
7
- require 'commute/common/basic_auth'
8
-
9
- require 'active_support/core_ext/hash/conversions'
10
-
11
- class HighriseTaskApi < Commute::Api
12
- include Commute::Aspect::Url
13
- include Commute::Aspect::Crud
14
-
15
- using(:request) do
16
- append Proc.new { |c|
17
- c.get.body = c.get.body.to_xml(root: 'task').gsub("\n", '') if c.get.body
18
- }
19
- append Commute::Common::BasicAuth.new
20
- end
21
-
22
- using(:response) do
23
- # append Proc.new { |c|
24
- # c.get.body = Array.from_xml(c.get.body)['tasks'] if c.get.body
25
- # }
26
- end
27
-
28
- url 'https://$account.highrisehq.com/$scope/tasks/$id.xml'
29
-
30
- transform(:task) do |request, context|
31
- request.body = context[:task]
32
- end
33
- # transform :task => :body
34
-
35
- transform { |request| request.headers['Content-Type'] = 'application/xml' }
36
-
37
- def update task = nil
38
- super.transform { |request| request.query['reload'] = 'true' }
39
- end
40
- end
41
-
42
- api = HighriseTaskApi.with account: 'piepetest', auth: {
43
- username: '610bf60020bb861761f07feef88ba42d',
44
- password: 'X'
45
- }
46
-
47
- task = {
48
- body: 'Kmoe iets doen'
49
- }
50
-
51
- task, status = api.all.run
52
- p task.size
53
- p status
54
-
55
- p 'AGAIN ------------------'
56
-
57
- task, status = api.all.run
58
- p task.size
59
- p status
@@ -1,18 +0,0 @@
1
- require 'commute'
2
-
3
- class PastieApi < Commute::Api
4
-
5
- transform(:id) do |request, id|
6
- request.url = "http://pastie.org/pastes/#{id}/download"
7
- end
8
-
9
- def find id = nil
10
- # Id for url.
11
- where id: id if id
12
- # Get method.
13
- get
14
- end
15
- end
16
-
17
- pastie, status = PastieApi.find(5119067).run.data
18
- # p pastie
@@ -1,37 +0,0 @@
1
- require 'commute/common/cache'
2
- require 'commute/common/conditional'
3
-
4
- module Commute
5
- module Aspect
6
-
7
- # Public: Enables caching in your api by inserting
8
- # cache processors in the api stack.
9
- #
10
- # Requires you to have a processor called request, before
11
- # which the fetch processor will be inserted.
12
- #
13
- module Caching
14
-
15
- def self.included api
16
- # Insert a processor that fetches response from the cache.
17
- api.builder.using(:request) do
18
- insert Common::Cache::Fetch.new
19
- end
20
-
21
- # Insert a processor that inserts responses in the cache.
22
- api.builder.using(:response) do
23
- append Common::Cache::Store.new
24
- end
25
-
26
- # Wrap some phases so that they will only be executed on a miss.
27
- api.builder.using do
28
- [:execution, :response, :status].each do |id|
29
- sequence.replace id do |processor|
30
- Common::Conditional::NotTagged.new(processor, :hit)
31
- end
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,41 +0,0 @@
1
- module Commute
2
- module Aspect
3
-
4
- module Crud
5
-
6
- def self.included base
7
- base.builder.transform(:body) do |request, resource|
8
- request.body = resource
9
- end
10
- end
11
-
12
- def all
13
- get.multiple
14
- end
15
-
16
- def find id = nil
17
- get.where(id: id).single
18
- end
19
-
20
- def update body = nil
21
- put.with(body: body).single
22
- end
23
-
24
- def create body = nil
25
- post.with(body: body).single
26
- end
27
-
28
- def destroy id = nil
29
- delete.where(id: id).single
30
- end
31
-
32
- def single
33
- self
34
- end
35
-
36
- def multiple
37
- self
38
- end
39
- end
40
- end
41
- end
@@ -1,16 +0,0 @@
1
- module Commute
2
- module Aspect
3
-
4
- module Pagination
5
-
6
- def offset n = 0
7
- end
8
-
9
- def limit n = 1
10
- end
11
-
12
- def next limit = 0
13
- end
14
- end
15
- end
16
- end
@@ -1,57 +0,0 @@
1
- module Commute
2
- module Aspect
3
-
4
- module Url
5
-
6
- def self.included base
7
- base.extend ClassMethods
8
- end
9
-
10
- module ClassMethods
11
- def url pattern
12
- transform &generate(pattern)
13
- end
14
-
15
- private
16
-
17
- def generate pattern
18
- # Regex to match different parts of a URL.
19
- matcher = \
20
- /(?<scheme>https?):\/\/(?<host>[^\/:]+):?(?<port>\d+)?(?<path>[^\.]*)(?<extension>\..+)?/
21
- # Match the pattern.
22
- matched = matcher.match(pattern)
23
- raise 'Invalid url: At least a host and scheme required' \
24
- unless matched && matched[:host] && matched[:scheme]
25
- # Collect the parts.
26
- parts = {
27
- scheme: matched[:scheme],
28
- host: matched[:host].split('.'),
29
- port: matched[:port],
30
- path: matched[:path].split('/'),
31
- extension: matched[:extension]
32
- }
33
-
34
- Proc.new do |request, context|
35
- request.scheme = parts[:scheme]
36
- request.host = fill(parts[:host], context).join '.'
37
- request.path = fill(parts[:path], context).join('/') + (parts[:extension] || '')
38
- request.port = matched[:port]
39
- end
40
- end
41
-
42
- def fill parts, params
43
- new_parts = []
44
- parts.each do |part|
45
- if part.start_with?('$')
46
- param = params[part[1..-1].to_sym]
47
- new_parts << param if param
48
- else
49
- new_parts << part
50
- end
51
- end
52
- new_parts
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,43 +0,0 @@
1
- module Commute
2
- module Common
3
-
4
- module Cache
5
-
6
- class Success
7
-
8
- def success?
9
- true
10
- end
11
- end
12
-
13
- SUCCESS = Success.new
14
-
15
- class Fetch
16
- @id = :cache
17
-
18
- def call commuter, cache = nil
19
- return unless cache
20
- request = commuter.get
21
- # Hit.
22
- if request.method == :get && result = cache.get(request.url)
23
- commuter.set [result, SUCCESS]
24
- commuter.tag :hit
25
- commuter.return
26
- end
27
- end
28
- end
29
-
30
- class Store
31
- @id = :cache
32
-
33
- def call commuter, cache = nil
34
- return unless cache
35
- response = commuter.get
36
- if response.request.method == :get && response.body
37
- cache.set response.request.url, response.body
38
- end
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,27 +0,0 @@
1
- require 'commute/core/commuter'
2
-
3
- module Commute
4
- module Common
5
- module Conditional
6
-
7
- class Tagged
8
-
9
- def initialize processor, *tags
10
- @processor = processor
11
- @tags = tags
12
- end
13
-
14
- def call commuter
15
- @processor.call unless @tags.any? { |tag| commuter.tagged? tag }
16
- end
17
- end
18
-
19
- class NotTagged < Tagged
20
-
21
- def call commuter
22
- @processor.call commuter if @tags.all? { |tag| !commuter.tagged? tag }
23
- end
24
- end
25
- end
26
- end
27
- end