king_soa 0.0.3 → 0.0.4
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/README.rdoc +3 -3
- data/lib/king_soa/service.rb +24 -24
- data/spec/server/app.rb +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -72,12 +72,12 @@ returns values or errors.
|
|
72
72
|
=== Local Services
|
73
73
|
|
74
74
|
A local service calls a class with the CamelCased service name. This class MUST
|
75
|
-
have a self.perform method which receives all of the
|
75
|
+
have a self.perform method which receives all of the given arguments.
|
76
76
|
|
77
77
|
=== Queued Services
|
78
78
|
|
79
|
-
The service is put onto a resque queue and somewhere in
|
80
|
-
have a worker
|
79
|
+
The service is put onto a resque queue and somewhere in your cloud you should
|
80
|
+
have a worker looking for it. The service class should also have the resque
|
81
81
|
@queue attribute set so the job can be resceduled if it fails.
|
82
82
|
|
83
83
|
== Integration
|
data/lib/king_soa/service.rb
CHANGED
@@ -2,11 +2,10 @@ module KingSoa
|
|
2
2
|
class Service
|
3
3
|
# endpoint url
|
4
4
|
attr_accessor :debug, :name, :auth, :queue
|
5
|
-
attr_reader :request
|
6
5
|
|
7
6
|
def initialize(opts)
|
8
7
|
self.name = opts[:name].to_sym
|
9
|
-
self.url = opts[:url] if opts[:url]
|
8
|
+
self.url = opts[:url] if opts[:url]
|
10
9
|
self.queue = opts[:queue] if opts[:queue]
|
11
10
|
self.auth = opts[:auth] if opts[:auth]
|
12
11
|
end
|
@@ -14,13 +13,14 @@ module KingSoa
|
|
14
13
|
# Call a service living somewhere in the soa universe. This is done by
|
15
14
|
# making a POST request to the url
|
16
15
|
def call_remote(*args)
|
17
|
-
|
18
|
-
|
16
|
+
request = Typhoeus::Easy.new
|
17
|
+
set_request_opts(request, args)
|
18
|
+
resp_code = request.perform
|
19
19
|
case resp_code
|
20
20
|
when 200
|
21
|
-
return self.decode(
|
21
|
+
return self.decode(request.response_body)["result"]
|
22
22
|
else
|
23
|
-
return self.decode(
|
23
|
+
return self.decode(request.response_body)["error"]
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -46,15 +46,15 @@ module KingSoa
|
|
46
46
|
result = local_class ? local_class.send(:perform, *args) : call_remote(*args)
|
47
47
|
return result
|
48
48
|
end
|
49
|
-
end
|
49
|
+
end
|
50
50
|
|
51
51
|
# The local class, if found
|
52
52
|
def local_class
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
53
|
+
begin
|
54
|
+
local_class_name.constantize
|
55
|
+
rescue NameError => e # no local implementation
|
56
|
+
false
|
57
|
+
end
|
58
58
|
end
|
59
59
|
|
60
60
|
# Return the classname infered from the camelized service name.
|
@@ -63,18 +63,18 @@ module KingSoa
|
|
63
63
|
self.name.to_s.camelize
|
64
64
|
end
|
65
65
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
def set_request_opts(args)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
66
|
+
# Set options for the typhoeus curl request
|
67
|
+
# === Parameter
|
68
|
+
# req<Typhoeus::Easy>:: request object
|
69
|
+
# args<Array[]>:: the arguments for the soa method, will be json encoded and added to post body
|
70
|
+
def set_request_opts(req, args)
|
71
|
+
req.url = url
|
72
|
+
req.method = :post
|
73
|
+
req.timeout = 10000 # milliseconds
|
74
|
+
req.params = params(args)
|
75
|
+
req.user_agent = 'KingSoa'
|
76
|
+
req.follow_location = true
|
77
|
+
req.verbose = 1 if debug
|
78
78
|
end
|
79
79
|
|
80
80
|
# Url receiving the request
|
data/spec/server/app.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'rubygems'
|
3
3
|
require 'sinatra'
|
4
|
-
# change since we want the files in here not from installed gem
|
4
|
+
# change PATH since we want the files in here not from installed gem
|
5
5
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
6
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
|
7
7
|
require "king_soa"
|