sk-hoth 0.0.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +69 -39
- data/THANKS.md +9 -0
- data/TODO +2 -0
- data/lib/hoth/encoding/json.rb +28 -0
- data/lib/hoth/encoding/no_op.rb +19 -0
- data/lib/hoth/endpoint.rb +28 -0
- data/lib/hoth/exceptions.rb +14 -0
- data/lib/hoth/extension/core/exception.rb +15 -0
- data/lib/hoth/modules.rb +27 -0
- data/lib/hoth/providers/bertrpc_provider.rb +35 -0
- data/lib/hoth/providers/rack_provider.rb +45 -0
- data/lib/hoth/service.rb +50 -0
- data/lib/hoth/service_definition.rb +18 -0
- data/lib/hoth/service_module.rb +49 -0
- data/lib/hoth/service_registry.rb +34 -0
- data/lib/hoth/services.rb +51 -0
- data/lib/hoth/transport/base.rb +19 -0
- data/lib/hoth/transport/bert.rb +87 -0
- data/lib/hoth/transport/http.rb +41 -0
- data/lib/hoth/transport/http_hmac.rb +37 -0
- data/lib/hoth/transport/workling.rb +23 -0
- data/lib/hoth/transport.rb +48 -0
- data/lib/hoth/util/logger.rb +46 -0
- data/lib/hoth.rb +56 -0
- data/spec/spec_helper.rb +7 -26
- data/spec/unit/encoding/json_spec.rb +25 -0
- data/spec/unit/endpoint_spec.rb +34 -0
- data/spec/unit/extension/core/exception_spec.rb +34 -0
- data/spec/unit/hoth_spec.rb +30 -0
- data/spec/unit/providers/rack_provider_spec.rb +49 -0
- data/spec/unit/service_definition_spec.rb +21 -0
- data/spec/unit/service_module_spec.rb +59 -0
- data/spec/unit/service_spec.rb +77 -0
- data/spec/unit/transport/base_spec.rb +43 -0
- data/spec/unit/transport/http_hmac_spec.rb +44 -0
- data/spec/unit/transport/http_spec.rb +73 -0
- data/spec/unit/transport/workling_spec.rb +42 -0
- data/spec/unit/transport_spec.rb +29 -0
- metadata +86 -23
- data/lib/king_soa/rack/middleware.rb +0 -47
- data/lib/king_soa/registry.rb +0 -55
- data/lib/king_soa/service.rb +0 -88
- data/lib/king_soa.rb +0 -56
- data/spec/king_soa/rack/middleware_spec.rb +0 -36
- data/spec/king_soa/registry_spec.rb +0 -28
- data/spec/king_soa/service_spec.rb +0 -46
- data/spec/server/app.rb +0 -26
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../../', 'spec_helper'))
|
2
|
+
|
3
|
+
module Hoth
|
4
|
+
module Transport
|
5
|
+
describe HttpHmac do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@service_mock = mock("ServiceMock")
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should post payload encoded with JSON" do
|
13
|
+
stub_request(:any, "localhost:3000/execute")
|
14
|
+
@service_mock.should_receive(:endpoint).and_return(endpoint = mock("EndpointMock"))
|
15
|
+
@service_mock.should_receive(:name).and_return("service_name")
|
16
|
+
endpoint.should_receive(:to_url).and_return("http://localhost:3000/execute")
|
17
|
+
encoder = mock("JsonEncoderMock")
|
18
|
+
encoder.should_receive(:encode).with("params").and_return("encoded_params")
|
19
|
+
|
20
|
+
transport = HttpHmac.new(@service_mock, {:encoder => encoder})
|
21
|
+
transport.hmac_access_id = 'an-hmac_access_id'
|
22
|
+
transport.hmac_secret = 'an-hmac_secret'
|
23
|
+
transport.post_payload("params")
|
24
|
+
transport.req['Authorization'].should include('KingHmac::Auth an-hmac_access_id:')
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "Error Handling" do
|
28
|
+
|
29
|
+
before(:each) do
|
30
|
+
service_mock = mock("ServiceMock")
|
31
|
+
@params = {:first_name => "Seras", :last_name => "Victoria"}
|
32
|
+
encoder = mock("JsonEncoderMock")
|
33
|
+
@transport = HttpHmac.new(service_mock, {:encoder => encoder})
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should handle hmac missing error" do
|
37
|
+
lambda { @transport.call_remote_with(@params) }.should raise_error(TransportError)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../../', 'spec_helper'))
|
2
|
+
|
3
|
+
module Hoth
|
4
|
+
module Transport
|
5
|
+
describe Http do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@service_mock = mock("ServiceMock")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should call a remote via http" do
|
12
|
+
params = {:first_name => "Seras", :last_name => "Victoria"}
|
13
|
+
|
14
|
+
transport = Http.new(@service_mock)
|
15
|
+
transport.should_receive(:post_payload).with([params])
|
16
|
+
transport.call_remote_with(params)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should post payload encoded with JSON" do
|
20
|
+
@service_mock.should_receive(:endpoint).and_return(endpoint = mock("EndpointMock"))
|
21
|
+
@service_mock.should_receive(:name).and_return("service_name")
|
22
|
+
endpoint.should_receive(:to_url).and_return("http://localhost:3000/execute")
|
23
|
+
|
24
|
+
encoder = mock("JsonEncoderMock")
|
25
|
+
encoder.should_receive(:encode).with("params").and_return("encoded_params")
|
26
|
+
|
27
|
+
URI.should_receive(:parse).with("http://localhost:3000/execute").and_return(uri = mock("URIMock"))
|
28
|
+
Net::HTTP.should_receive(:post_form).with(uri, {"name" => "service_name", "params" => "encoded_params"})
|
29
|
+
|
30
|
+
transport = Http.new(@service_mock, {:encoder => encoder})
|
31
|
+
transport.post_payload("params")
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "Error Handling" do
|
35
|
+
|
36
|
+
before(:each) do
|
37
|
+
service_mock = mock("ServiceMock")
|
38
|
+
@params = {:first_name => "Seras", :last_name => "Victoria"}
|
39
|
+
encoder = mock("JsonEncoderMock")
|
40
|
+
@transport = Http.new(service_mock, {:encoder => encoder})
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should handle http connection error" do
|
44
|
+
@transport.should_receive(:post_payload).and_raise(Exception)
|
45
|
+
lambda { @transport.call_remote_with(@params) }.should raise_error(TransportError)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should handle successful response" do
|
49
|
+
@transport.should_receive(:post_payload).and_return(response = Net::HTTPSuccess.allocate)
|
50
|
+
response.should_receive(:body).twice.and_return(response_body = '{"result" : "return_value"}')
|
51
|
+
@transport.encoder.should_receive(:decode).with(response_body).and_return({"result" => "return_value"})
|
52
|
+
@transport.call_remote_with(@params)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should handle remote server error" do
|
56
|
+
@transport.should_receive(:post_payload).and_return(response = Net::HTTPServerError.allocate)
|
57
|
+
response.should_receive(:body).any_number_of_times.and_return(response_body = '{"error" : "ServerError"}')
|
58
|
+
@transport.encoder.should_receive(:decode).with(response_body).and_return({"result" => "return_value"})
|
59
|
+
lambda { @transport.call_remote_with(@params) }.should raise_error(TransportError)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should handle any other HTTP errors" do
|
63
|
+
[Net::HTTPRedirection, Net::HTTPClientError, Net::HTTPInformation, Net::HTTPUnknownResponse].each do |http_response|
|
64
|
+
@transport.should_receive(:post_payload).and_return(response = http_response.allocate)
|
65
|
+
response.should_receive(:body).any_number_of_times.and_return('{"error" : "ServerError"}')
|
66
|
+
lambda { @transport.call_remote_with(@params) }.should raise_error(TransportError)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../../', 'spec_helper'))
|
2
|
+
|
3
|
+
module Hoth
|
4
|
+
module Transport
|
5
|
+
|
6
|
+
describe "Workling" do
|
7
|
+
|
8
|
+
it "should send publish a message via SimplePublisher" do
|
9
|
+
endpoint = mock("EndpointMock")
|
10
|
+
endpoint.should_receive(:host).and_return("localhost")
|
11
|
+
endpoint.should_receive(:port).and_return("22122")
|
12
|
+
|
13
|
+
service_module = mock("ServiceModule")
|
14
|
+
service_module.should_receive(:name).and_return("TestServiceModule")
|
15
|
+
|
16
|
+
service = mock("ServiceMock")
|
17
|
+
service.should_receive(:name).and_return("TestService")
|
18
|
+
service.should_receive(:endpoint).any_number_of_times.and_return(endpoint)
|
19
|
+
service.should_receive(:module).any_number_of_times.and_return(service_module)
|
20
|
+
|
21
|
+
SimplePublisher::Topic.should_receive(:new).with(:name => "test_service_module_subscribers__test_service").and_return(topic = mock("Topic"))
|
22
|
+
|
23
|
+
SimplePublisher::StarlingConnection.should_receive(:new).with(:host => "localhost", :port => "22122").and_return(connection = mock("Connection"))
|
24
|
+
|
25
|
+
SimplePublisher::Publisher.should_receive(:new).with(
|
26
|
+
:topic => topic,
|
27
|
+
:connection => connection
|
28
|
+
).and_return(publisher = mock("PublisherMock"))
|
29
|
+
|
30
|
+
uid = "GC-123546"
|
31
|
+
email_address = "test@example.com"
|
32
|
+
|
33
|
+
publisher.should_receive(:publish).with([uid, email_address])
|
34
|
+
|
35
|
+
transport = Workling.new(service)
|
36
|
+
transport.call_remote_with(uid, email_address)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
module Hoth
|
4
|
+
describe Transport do
|
5
|
+
|
6
|
+
it "should create a new transport by transport name for a given service" do
|
7
|
+
service = mock("ServiceMock")
|
8
|
+
Transport.should_receive(:new_transport_with_encoding).with(:transport_name, service)
|
9
|
+
Transport.create(:transport_name, service)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should create a new transport instance with encoding from a transport name" do
|
13
|
+
transport = Transport.new_transport_with_encoding :json_via_http, mock("ServiceMock")
|
14
|
+
transport.should be_kind_of(Transport::Http)
|
15
|
+
transport.encoder.should == Encoding::Json
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should create a new transport instance with encoding from a transport alias" do
|
19
|
+
transport = Transport.new_transport_with_encoding :http, mock("ServiceMock")
|
20
|
+
transport.should be_kind_of(Transport::Http)
|
21
|
+
transport.encoder.should == Encoding::Json
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should raise an exception if transport name does not exist" do
|
25
|
+
lambda { Transport.new_transport_with_encoding :not_existing_transport, mock("ServiceMock") }.should raise_error(TransportException)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
metadata
CHANGED
@@ -4,21 +4,22 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 3
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.1
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
|
+
- Dirk Breuer
|
12
13
|
- Georg Leciejewski
|
13
14
|
autorequire:
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-04-11 00:00:00 +02:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
22
|
+
name: activesupport
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
25
|
requirements:
|
@@ -30,7 +31,7 @@ dependencies:
|
|
30
31
|
type: :runtime
|
31
32
|
version_requirements: *id001
|
32
33
|
- !ruby/object:Gem::Dependency
|
33
|
-
name:
|
34
|
+
name: bertrpc
|
34
35
|
prerelease: false
|
35
36
|
requirement: &id002 !ruby/object:Gem::Requirement
|
36
37
|
requirements:
|
@@ -42,7 +43,7 @@ dependencies:
|
|
42
43
|
type: :runtime
|
43
44
|
version_requirements: *id002
|
44
45
|
- !ruby/object:Gem::Dependency
|
45
|
-
name:
|
46
|
+
name: json
|
46
47
|
prerelease: false
|
47
48
|
requirement: &id003 !ruby/object:Gem::Requirement
|
48
49
|
requirements:
|
@@ -54,7 +55,7 @@ dependencies:
|
|
54
55
|
type: :runtime
|
55
56
|
version_requirements: *id003
|
56
57
|
- !ruby/object:Gem::Dependency
|
57
|
-
name:
|
58
|
+
name: king_hmac
|
58
59
|
prerelease: false
|
59
60
|
requirement: &id004 !ruby/object:Gem::Requirement
|
60
61
|
requirements:
|
@@ -63,10 +64,10 @@ dependencies:
|
|
63
64
|
segments:
|
64
65
|
- 0
|
65
66
|
version: "0"
|
66
|
-
type: :
|
67
|
+
type: :runtime
|
67
68
|
version_requirements: *id004
|
68
69
|
- !ruby/object:Gem::Dependency
|
69
|
-
name:
|
70
|
+
name: rspec
|
70
71
|
prerelease: false
|
71
72
|
requirement: &id005 !ruby/object:Gem::Requirement
|
72
73
|
requirements:
|
@@ -77,11 +78,35 @@ dependencies:
|
|
77
78
|
version: "0"
|
78
79
|
type: :development
|
79
80
|
version_requirements: *id005
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: simple_publisher
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id006
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: webmock
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
type: :development
|
104
|
+
version_requirements: *id007
|
80
105
|
description: |
|
81
106
|
Creating a SOA requires a centralized location to define all services within the
|
82
107
|
SOA. Furthermore you want to know where to deploy those services.
|
83
108
|
|
84
|
-
email:
|
109
|
+
email: dirk.breuer@gmail.com
|
85
110
|
executables: []
|
86
111
|
|
87
112
|
extensions: []
|
@@ -89,20 +114,49 @@ extensions: []
|
|
89
114
|
extra_rdoc_files:
|
90
115
|
- LICENSE
|
91
116
|
- README.rdoc
|
117
|
+
- TODO
|
92
118
|
files:
|
93
119
|
- README.rdoc
|
94
|
-
-
|
95
|
-
- lib/
|
96
|
-
- lib/
|
97
|
-
- lib/
|
98
|
-
-
|
99
|
-
-
|
100
|
-
-
|
101
|
-
-
|
120
|
+
- THANKS.md
|
121
|
+
- lib/hoth.rb
|
122
|
+
- lib/hoth/encoding/json.rb
|
123
|
+
- lib/hoth/encoding/no_op.rb
|
124
|
+
- lib/hoth/endpoint.rb
|
125
|
+
- lib/hoth/exceptions.rb
|
126
|
+
- lib/hoth/extension/core/exception.rb
|
127
|
+
- lib/hoth/modules.rb
|
128
|
+
- lib/hoth/providers/bertrpc_provider.rb
|
129
|
+
- lib/hoth/providers/rack_provider.rb
|
130
|
+
- lib/hoth/service.rb
|
131
|
+
- lib/hoth/service_definition.rb
|
132
|
+
- lib/hoth/service_module.rb
|
133
|
+
- lib/hoth/service_registry.rb
|
134
|
+
- lib/hoth/services.rb
|
135
|
+
- lib/hoth/transport.rb
|
136
|
+
- lib/hoth/transport/base.rb
|
137
|
+
- lib/hoth/transport/bert.rb
|
138
|
+
- lib/hoth/transport/http.rb
|
139
|
+
- lib/hoth/transport/http_hmac.rb
|
140
|
+
- lib/hoth/transport/workling.rb
|
141
|
+
- lib/hoth/util/logger.rb
|
102
142
|
- spec/spec_helper.rb
|
143
|
+
- spec/unit/encoding/json_spec.rb
|
144
|
+
- spec/unit/endpoint_spec.rb
|
145
|
+
- spec/unit/extension/core/exception_spec.rb
|
146
|
+
- spec/unit/hoth_spec.rb
|
147
|
+
- spec/unit/providers/rack_provider_spec.rb
|
148
|
+
- spec/unit/service_definition_spec.rb
|
149
|
+
- spec/unit/service_module_spec.rb
|
150
|
+
- spec/unit/service_spec.rb
|
151
|
+
- spec/unit/transport/base_spec.rb
|
152
|
+
- spec/unit/transport/http_hmac_spec.rb
|
153
|
+
- spec/unit/transport/http_spec.rb
|
154
|
+
- spec/unit/transport/workling_spec.rb
|
155
|
+
- spec/unit/transport_spec.rb
|
103
156
|
- LICENSE
|
157
|
+
- TODO
|
104
158
|
has_rdoc: true
|
105
|
-
homepage: http://github.com/salesking/
|
159
|
+
homepage: http://github.com/salesking/hoth
|
106
160
|
licenses: []
|
107
161
|
|
108
162
|
post_install_message:
|
@@ -133,7 +187,16 @@ specification_version: 3
|
|
133
187
|
summary: Registry and deployment description abstraction for SOA-Services. Tweaked by SalesKing
|
134
188
|
test_files:
|
135
189
|
- spec/spec_helper.rb
|
136
|
-
- spec/
|
137
|
-
- spec/
|
138
|
-
- spec/
|
139
|
-
- spec/
|
190
|
+
- spec/unit/service_module_spec.rb
|
191
|
+
- spec/unit/service_spec.rb
|
192
|
+
- spec/unit/transport_spec.rb
|
193
|
+
- spec/unit/encoding/json_spec.rb
|
194
|
+
- spec/unit/providers/rack_provider_spec.rb
|
195
|
+
- spec/unit/service_definition_spec.rb
|
196
|
+
- spec/unit/extension/core/exception_spec.rb
|
197
|
+
- spec/unit/transport/http_hmac_spec.rb
|
198
|
+
- spec/unit/transport/workling_spec.rb
|
199
|
+
- spec/unit/transport/http_spec.rb
|
200
|
+
- spec/unit/transport/base_spec.rb
|
201
|
+
- spec/unit/hoth_spec.rb
|
202
|
+
- spec/unit/endpoint_spec.rb
|
@@ -1,47 +0,0 @@
|
|
1
|
-
module KingSoa::Rack
|
2
|
-
class Middleware
|
3
|
-
|
4
|
-
def initialize(app)
|
5
|
-
@app = app
|
6
|
-
end
|
7
|
-
|
8
|
-
# Takes incoming soa requests and calls the passed in method with given params
|
9
|
-
def call(env)
|
10
|
-
# Hoth::Logger.debug "env: #{env.inspect}"
|
11
|
-
if env["PATH_INFO"] =~ /^\/soa/
|
12
|
-
begin
|
13
|
-
req = Rack::Request.new(env)
|
14
|
-
# find service
|
15
|
-
service = KingSoa.find(req.params["name"])
|
16
|
-
# authenticate
|
17
|
-
authenticated?(service, req.params["auth_key"])
|
18
|
-
# perform method with decoded params
|
19
|
-
result = service.perform(*service.decode( req.params["params"] ))
|
20
|
-
# encode result
|
21
|
-
encoded_result = service.encode({"result" => result})
|
22
|
-
# and return
|
23
|
-
[ 200,
|
24
|
-
{'Content-Type' => 'application/json',
|
25
|
-
'Content-Length' => "#{encoded_result.length}"},
|
26
|
-
[encoded_result] ]
|
27
|
-
rescue Exception => e
|
28
|
-
#Hoth::Logger.debug "e: #{e.message}"
|
29
|
-
if service
|
30
|
-
encoded_error = service.encode({"error" => e})
|
31
|
-
[500, {'Content-Type' => 'application/json', 'Content-Length' => "#{encoded_error.length}"}, [encoded_error]]
|
32
|
-
else
|
33
|
-
encoded_error = {"error" => "An error occurred! (#{e.message})"}.to_json
|
34
|
-
[500, {'Content-Type' => "application/json", 'Content-Length' => "#{encoded_error.length}"}, [encoded_error]]
|
35
|
-
end
|
36
|
-
end
|
37
|
-
else
|
38
|
-
@app.call(env)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def authenticated?(service, key)
|
43
|
-
raise "Please provide a valid authentication key" unless service.auth_key == key
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
data/lib/king_soa/registry.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
module KingSoa
|
2
|
-
class Registry
|
3
|
-
include Singleton
|
4
|
-
|
5
|
-
############################################################################
|
6
|
-
# Class methods .. only use those since we are in a Singleton
|
7
|
-
############################################################################
|
8
|
-
|
9
|
-
# Get a method by a given name
|
10
|
-
def self.[](service_name)
|
11
|
-
instance[service_name]
|
12
|
-
end
|
13
|
-
# add a service
|
14
|
-
def self.<<(service)
|
15
|
-
instance << service
|
16
|
-
end
|
17
|
-
# Return an array of defined services
|
18
|
-
def self.services
|
19
|
-
instance.services
|
20
|
-
end
|
21
|
-
|
22
|
-
# find a group of services identified by starting with the same string
|
23
|
-
#
|
24
|
-
def group(name)
|
25
|
-
instance.group(name)
|
26
|
-
end
|
27
|
-
############################################################################
|
28
|
-
# Instance methods - not directly accessible => Singleton
|
29
|
-
############################################################################
|
30
|
-
|
31
|
-
# returns all available methods
|
32
|
-
def services
|
33
|
-
@services ||= []
|
34
|
-
end
|
35
|
-
|
36
|
-
# Add a new method onto the stack
|
37
|
-
def <<(service)
|
38
|
-
(services || []) << service
|
39
|
-
end
|
40
|
-
|
41
|
-
# Get a method by a given name
|
42
|
-
def [](service_name)
|
43
|
-
name = service_name.to_sym
|
44
|
-
services.detect {|s| s.name == name }
|
45
|
-
end
|
46
|
-
|
47
|
-
def group(service_name)
|
48
|
-
name = service_name.to_sym
|
49
|
-
# srvs = []
|
50
|
-
services.collect {|s| s.name[/^#{name}/] }
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
end
|
55
|
-
end
|
data/lib/king_soa/service.rb
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
module KingSoa
|
2
|
-
class Service
|
3
|
-
# endpoint url
|
4
|
-
attr_accessor :debug, :name, :auth_key, :queue
|
5
|
-
attr_reader :request
|
6
|
-
|
7
|
-
def initialize(opts)
|
8
|
-
self.name = opts[:name].to_sym
|
9
|
-
self.url = opts[:url] if opts[:url]
|
10
|
-
self.queue = opts[:queue] if opts[:queue]
|
11
|
-
self.auth_key = opts[:auth_key] if opts[:auth_key]
|
12
|
-
end
|
13
|
-
|
14
|
-
def call_remote(*args)
|
15
|
-
set_request_opts(args)
|
16
|
-
resp_code = @request.perform
|
17
|
-
case resp_code
|
18
|
-
when 200
|
19
|
-
return self.decode(@request.response_body)["result"]
|
20
|
-
else
|
21
|
-
return self.decode(@request.response_body)["error"]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def perform(*args)
|
26
|
-
result = local_class ? local_class.send(:perform, *args) : call_remote(*args)
|
27
|
-
return result
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
def local_class
|
32
|
-
@local_class ||= begin
|
33
|
-
"#{self.name.to_s.camelize}".constantize
|
34
|
-
rescue NameError => e # no local implementation
|
35
|
-
false
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def request
|
40
|
-
@request ||= Typhoeus::Easy.new
|
41
|
-
end
|
42
|
-
|
43
|
-
def set_request_opts(args)
|
44
|
-
request.url = url
|
45
|
-
request.method = :post
|
46
|
-
request.timeout = 100 # milliseconds
|
47
|
-
request.params = params(args)
|
48
|
-
request.user_agent = 'KingSoa'
|
49
|
-
request.follow_location = true
|
50
|
-
request.verbose = 1 if debug
|
51
|
-
end
|
52
|
-
|
53
|
-
# Url receiving the request
|
54
|
-
# TODO. if not present try to grab from endpoint
|
55
|
-
def url
|
56
|
-
@url
|
57
|
-
end
|
58
|
-
def url=(url)
|
59
|
-
@url = "#{url}/soa"
|
60
|
-
end
|
61
|
-
|
62
|
-
# The params for each soa request consisnt of two values:
|
63
|
-
# name => the name of the method to call
|
64
|
-
# params => the parameters for the method
|
65
|
-
# ==== Parameter
|
66
|
-
# params<Hash|Array|String>:: will be json encoded
|
67
|
-
# === Returns
|
68
|
-
# <Hash{String=>String}>:: params added to the POST body
|
69
|
-
def params(payload)
|
70
|
-
{ 'name' => name.to_s,
|
71
|
-
'params' => encode(payload),
|
72
|
-
'auth_key'=> auth_key }
|
73
|
-
end
|
74
|
-
|
75
|
-
def encode(string)
|
76
|
-
string.to_json
|
77
|
-
end
|
78
|
-
|
79
|
-
def decode(string)
|
80
|
-
begin
|
81
|
-
JSON.parse(string)
|
82
|
-
rescue JSON::ParserError => e
|
83
|
-
raise e
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
end
|
data/lib/king_soa.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'singleton'
|
2
|
-
require 'json'
|
3
|
-
require 'typhoeus'
|
4
|
-
require 'active_support/inflector'
|
5
|
-
|
6
|
-
require 'king_soa/registry'
|
7
|
-
require 'king_soa/service'
|
8
|
-
require 'king_soa/rack/middleware'
|
9
|
-
|
10
|
-
# Define available services.
|
11
|
-
#
|
12
|
-
# service:
|
13
|
-
# name: sign_document
|
14
|
-
# url: "https://msg.salesking.eu"
|
15
|
-
# auth: 'a-long-random-string'
|
16
|
-
# queue: a-queue-name
|
17
|
-
#
|
18
|
-
#
|
19
|
-
# method: save_signed_document
|
20
|
-
# url: "https://www.salesking.eu/soa"
|
21
|
-
#
|
22
|
-
#
|
23
|
-
# after defining your services you can call each of them with
|
24
|
-
# <tt>Hoth::Services.service_name(params)</tt>
|
25
|
-
#
|
26
|
-
# KingSoa.sign_document(counter)
|
27
|
-
# current_number = Hoth::Services.value_of_counter(counter)
|
28
|
-
# created_account = Hoth::Services.create_account(account)
|
29
|
-
#
|
30
|
-
module KingSoa
|
31
|
-
|
32
|
-
class << self
|
33
|
-
|
34
|
-
def init_from_hash(services)
|
35
|
-
# create service
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
# Locate service by a given name
|
40
|
-
# ==== Params
|
41
|
-
# service<String|Symbol>:: the name to lookup
|
42
|
-
def find(service)
|
43
|
-
Registry[service]
|
44
|
-
end
|
45
|
-
|
46
|
-
# this is where the services get called
|
47
|
-
def method_missing(meth, *args, &blk) # :nodoc:
|
48
|
-
if service = Registry[meth]
|
49
|
-
service.execute(*args)
|
50
|
-
else
|
51
|
-
super
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../../spec_helper.rb'
|
2
|
-
|
3
|
-
describe KingSoa::Rack::Middleware do
|
4
|
-
include Rack::Test::Methods
|
5
|
-
|
6
|
-
before(:each) do
|
7
|
-
@service = KingSoa::Service.new(:url=>'localhost', :name=>'a_method')
|
8
|
-
KingSoa::Registry << @service
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should be able to handle exceptions" do
|
12
|
-
app = stub("ApplicationStub").as_null_object
|
13
|
-
middleware = KingSoa::Rack::Middleware.new(app)
|
14
|
-
env = {"PATH_INFO" => "/soa", "name" => 'a_method'}
|
15
|
-
|
16
|
-
rack_response = middleware.call env
|
17
|
-
rack_response.first.should == 500 #status code
|
18
|
-
rack_response.last.should be_a_kind_of(Array)
|
19
|
-
rack_response.last.first.should == "{\"error\":\"An error occurred! (Missing rack.input)\"}"
|
20
|
-
end
|
21
|
-
|
22
|
-
xit "says hello" do
|
23
|
-
app = stub("ApplicationStub").as_null_object
|
24
|
-
middleware = Hoth::Providers::RackProvider.new(app)
|
25
|
-
|
26
|
-
get '/soa', :name=>'a_method', :params=> "#{{:number=>1}.to_json}"
|
27
|
-
last_response.should == 'ads'#be_ok
|
28
|
-
last_response.body.should == 'Hello World'
|
29
|
-
end
|
30
|
-
|
31
|
-
# def app
|
32
|
-
# dummy_app = lambda { |env| puts "in dummy"; [200, {}, ""] }
|
33
|
-
# KingSoa::Rack::Middleware.new(dummy_app)
|
34
|
-
# end
|
35
|
-
|
36
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
-
|
3
|
-
describe KingSoa::Registry do
|
4
|
-
before(:each) do
|
5
|
-
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should return empty services" do
|
9
|
-
reg = KingSoa::Registry.new
|
10
|
-
reg.services.should == []
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should add service" do
|
14
|
-
reg = KingSoa::Registry.new
|
15
|
-
s = KingSoa::Service.new(:url=>'http://localhost')
|
16
|
-
reg << s
|
17
|
-
reg.services.should == [s]
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should return a service by name" do
|
21
|
-
reg = KingSoa::Registry.new
|
22
|
-
s = KingSoa::Service.new(:name=>:save_document, :url=>'http://localhost')
|
23
|
-
reg << s
|
24
|
-
reg[:save_document].should == s
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
end
|