tom 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -0
- data/VERSION +1 -1
- data/lib/dispatcher.rb +39 -14
- data/lib/http.rb +1 -1
- data/lib/tom/routes.rb +1 -1
- data/spec/lib/dispatcher_spec.rb +8 -4
- data/spec/spec_helper.rb +1 -1
- data/tom.gemspec +2 -2
- metadata +31 -31
data/README.md
CHANGED
@@ -49,6 +49,8 @@ This class basically does what is pictured in the flow above:
|
|
49
49
|
|
50
50
|
To add APIs or change the behavior of Tom, you don't have to touch this class, though. `Adapter` and `Merger` is what you're looking for.
|
51
51
|
|
52
|
+
Adapters are optional, so it's completely valid for a route to only have a merger. If, however, there is no merger registered for a given route, a 404 will be emitted.
|
53
|
+
|
52
54
|
## Tom::Adapter
|
53
55
|
|
54
56
|
The `Adapter` class comes with the class methods:
|
@@ -115,6 +117,7 @@ Same goes for mergers.
|
|
115
117
|
|
116
118
|
# Todo
|
117
119
|
|
120
|
+
- document special headers we inject (e.g. which adapters were used, etc)
|
118
121
|
- handle adapter errors/states in mergers
|
119
122
|
- use Goliath::Rack::Heartbeat
|
120
123
|
- think about consensus protocols
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/lib/dispatcher.rb
CHANGED
@@ -11,21 +11,18 @@ module Tom
|
|
11
11
|
#
|
12
12
|
# @return [Array] Whatever {Tom::Dispatcher.merge} had to say
|
13
13
|
def self.dispatch(env)
|
14
|
-
route, method = route_and_method(env)
|
15
|
-
adapters = Tom::Routes.adapters_for_route(route, method)
|
16
|
-
return [404, {}, '{reason: "No adapters for this route"}'] if adapters.empty?
|
17
14
|
|
18
|
-
#
|
19
|
-
responses = {}
|
15
|
+
# 1. hit APIs. All at the same time. Oh, mygodd!
|
20
16
|
Tom::LOG.info "#{env['REQUEST_METHOD'].upcase} #{env['REQUEST_URI']}"
|
21
|
-
|
22
|
-
EM::Synchrony::FiberIterator.new(adapters, adapters.count).map do |clazz|
|
23
|
-
Tom::LOG.info " -> #{clazz}"
|
24
|
-
(responses[clazz] ||= []) << clazz.new.handle(env)
|
25
|
-
end
|
17
|
+
responses = parallel_adapter_dispatch(env)
|
26
18
|
|
19
|
+
# 2. merge
|
27
20
|
merged = merge(env, responses)
|
21
|
+
|
22
|
+
# 3. ???
|
28
23
|
Tom::LOG.info "-------------------------------------------------------n"
|
24
|
+
|
25
|
+
# 4. profit
|
29
26
|
merged
|
30
27
|
end
|
31
28
|
|
@@ -42,10 +39,15 @@ module Tom
|
|
42
39
|
# [200, {}, "Hi!"]
|
43
40
|
def self.merge(env, responses)
|
44
41
|
route, method = route_and_method(env)
|
45
|
-
merger = Tom::Routes.merger_for_route(route, method)
|
46
|
-
|
47
|
-
|
48
|
-
|
42
|
+
if merger = Tom::Routes.merger_for_route(route, method)
|
43
|
+
Tom::LOG.info "Merging with:"
|
44
|
+
Tom::LOG.info " -> #{merger}"
|
45
|
+
merged = merger.new.merge(env, responses)
|
46
|
+
else
|
47
|
+
merged = [404, {}, ""]
|
48
|
+
end
|
49
|
+
merged[1]["Adapters-Used"] = responses.keys.join(",")
|
50
|
+
merged
|
49
51
|
end
|
50
52
|
|
51
53
|
|
@@ -61,5 +63,28 @@ module Tom
|
|
61
63
|
env["REQUEST_METHOD"].downcase.to_sym]
|
62
64
|
end
|
63
65
|
|
66
|
+
private
|
67
|
+
|
68
|
+
# Uses a EM::Synchrony::FiberIterator to call all adapters that registered
|
69
|
+
# for this route at the same time.
|
70
|
+
#
|
71
|
+
# @param env [Hash] A rack env object
|
72
|
+
#
|
73
|
+
# @return [Hash] Keys are the adapter classes, values are arrays of responses
|
74
|
+
# they generated (the [status_code, headers, body] triplets)
|
75
|
+
def self.parallel_adapter_dispatch(env)
|
76
|
+
responses = {}
|
77
|
+
route, method = route_and_method(env)
|
78
|
+
adapters = Tom::Routes.adapters_for_route(route, method)
|
79
|
+
return responses if adapters.empty?
|
80
|
+
|
81
|
+
Tom::LOG.info "Dispatching to:"
|
82
|
+
EM::Synchrony::FiberIterator.new(adapters, adapters.count).map do |clazz|
|
83
|
+
Tom::LOG.info " -> #{clazz}"
|
84
|
+
(responses[clazz] ||= []) << clazz.new.handle(env)
|
85
|
+
end
|
86
|
+
responses
|
87
|
+
end
|
88
|
+
|
64
89
|
end
|
65
90
|
end
|
data/lib/http.rb
CHANGED
@@ -30,7 +30,7 @@ module Tom
|
|
30
30
|
|
31
31
|
def self.handle_errors(method, url, result)
|
32
32
|
result.errback do
|
33
|
-
raise "Tom::Adapter.forward_request error #{method} #{url}"
|
33
|
+
raise "Tom::Adapter.forward_request error '#{result.error}' for #{method} #{url}"
|
34
34
|
end
|
35
35
|
return unless result.response_header.status == 0
|
36
36
|
raise "EM::HttpRequest returned response code 0 for #{url} - timeout?"
|
data/lib/tom/routes.rb
CHANGED
data/spec/lib/dispatcher_spec.rb
CHANGED
@@ -12,9 +12,6 @@ class APIAdapter2 < Tom::Adapter
|
|
12
12
|
end
|
13
13
|
APIAdapter2.host = 'http://api_host_2.com'
|
14
14
|
|
15
|
-
class Merger < Tom::Merger
|
16
|
-
register_route ".*"
|
17
|
-
end
|
18
15
|
|
19
16
|
describe Tom do
|
20
17
|
|
@@ -22,9 +19,16 @@ describe Tom do
|
|
22
19
|
|
23
20
|
end
|
24
21
|
|
25
|
-
it "emits a
|
22
|
+
it "emits a 200 even when there are no adapters for the route" do
|
26
23
|
with_api(Tom::GoliathAPI) do
|
27
24
|
request = get_request(:path => '/walruses/5')
|
25
|
+
request.response_header.status.should == 200
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "emits a 404 when there are no mergers for the route" do
|
30
|
+
with_api(Tom::GoliathAPI) do
|
31
|
+
request = get_request(:path => '/')
|
28
32
|
request.response_header.status.should == 404
|
29
33
|
end
|
30
34
|
end
|
data/spec/spec_helper.rb
CHANGED
data/tom.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "tom"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jannis Hermanns"]
|
12
|
-
s.date = "2011-12-
|
12
|
+
s.date = "2011-12-09"
|
13
13
|
s.description = " Tom uses Goliath to dispatch HTTP requests to multiple other APIs (via Adapters) in parallel. In a next step, a Merger merges the result and responds to the clients request."
|
14
14
|
s.email = "jannis@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-09 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: goliath
|
16
|
-
requirement: &
|
16
|
+
requirement: &70294091681900 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70294091681900
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: em-synchrony
|
27
|
-
requirement: &
|
27
|
+
requirement: &70294091681320 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70294091681320
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: em-http-request
|
38
|
-
requirement: &
|
38
|
+
requirement: &70294091680840 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70294091680840
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: json
|
49
|
-
requirement: &
|
49
|
+
requirement: &70294091680220 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70294091680220
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &70294091679620 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 0.9.2
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70294091679620
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: bundler
|
71
|
-
requirement: &
|
71
|
+
requirement: &70294091679080 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 1.0.0
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70294091679080
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: jeweler
|
82
|
-
requirement: &
|
82
|
+
requirement: &70294091678600 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: 1.6.4
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70294091678600
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: shoulda
|
93
|
-
requirement: &
|
93
|
+
requirement: &70294091677960 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70294091677960
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: rcov
|
104
|
-
requirement: &
|
104
|
+
requirement: &70294091677440 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70294091677440
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: ruby-debug19
|
115
|
-
requirement: &
|
115
|
+
requirement: &70294091676940 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,10 +120,10 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *70294091676940
|
124
124
|
- !ruby/object:Gem::Dependency
|
125
125
|
name: rspec
|
126
|
-
requirement: &
|
126
|
+
requirement: &70294091667620 !ruby/object:Gem::Requirement
|
127
127
|
none: false
|
128
128
|
requirements:
|
129
129
|
- - ! '>='
|
@@ -131,10 +131,10 @@ dependencies:
|
|
131
131
|
version: '0'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
|
-
version_requirements: *
|
134
|
+
version_requirements: *70294091667620
|
135
135
|
- !ruby/object:Gem::Dependency
|
136
136
|
name: webmock
|
137
|
-
requirement: &
|
137
|
+
requirement: &70294091667000 !ruby/object:Gem::Requirement
|
138
138
|
none: false
|
139
139
|
requirements:
|
140
140
|
- - ! '>='
|
@@ -142,10 +142,10 @@ dependencies:
|
|
142
142
|
version: '0'
|
143
143
|
type: :development
|
144
144
|
prerelease: false
|
145
|
-
version_requirements: *
|
145
|
+
version_requirements: *70294091667000
|
146
146
|
- !ruby/object:Gem::Dependency
|
147
147
|
name: yard
|
148
|
-
requirement: &
|
148
|
+
requirement: &70294091666380 !ruby/object:Gem::Requirement
|
149
149
|
none: false
|
150
150
|
requirements:
|
151
151
|
- - ! '>='
|
@@ -153,10 +153,10 @@ dependencies:
|
|
153
153
|
version: '0'
|
154
154
|
type: :development
|
155
155
|
prerelease: false
|
156
|
-
version_requirements: *
|
156
|
+
version_requirements: *70294091666380
|
157
157
|
- !ruby/object:Gem::Dependency
|
158
158
|
name: rdiscount
|
159
|
-
requirement: &
|
159
|
+
requirement: &70294091665740 !ruby/object:Gem::Requirement
|
160
160
|
none: false
|
161
161
|
requirements:
|
162
162
|
- - ! '>='
|
@@ -164,7 +164,7 @@ dependencies:
|
|
164
164
|
version: '0'
|
165
165
|
type: :development
|
166
166
|
prerelease: false
|
167
|
-
version_requirements: *
|
167
|
+
version_requirements: *70294091665740
|
168
168
|
description: ! ' Tom uses Goliath to dispatch HTTP requests to multiple other APIs
|
169
169
|
(via Adapters) in parallel. In a next step, a Merger merges the result and responds
|
170
170
|
to the clients request.'
|
@@ -212,7 +212,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
212
212
|
version: '0'
|
213
213
|
segments:
|
214
214
|
- 0
|
215
|
-
hash:
|
215
|
+
hash: -4134106071848066876
|
216
216
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
217
217
|
none: false
|
218
218
|
requirements:
|