king_soa 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,7 @@ module KingSoa
21
21
 
22
22
  # find a group of services identified by starting with the same string
23
23
  #
24
- def group(name)
24
+ def self.group(name)
25
25
  instance.group(name)
26
26
  end
27
27
  ############################################################################
@@ -52,9 +52,7 @@ module KingSoa
52
52
 
53
53
  # untested
54
54
  def group(service_name)
55
- name = service_name.to_sym
56
- # srvs = []
57
- services.collect {|s| s.name[/^#{name}/] }
55
+ services.select {|s| !s.name.to_s.match(/^#{service_name}/).nil? }
58
56
  end
59
57
 
60
58
 
@@ -2,16 +2,21 @@ module KingSoa
2
2
  class Service
3
3
 
4
4
  # name<String/Symbol>:: name of the service class to call
5
+ #
5
6
  # auth<String/Int>:: password for the remote service. Used by rack middleware
6
7
  # to authentify the callee
7
- # url<String>:: Url where the service is located. If the rack middleware is
8
- # used on the receiving side, make sure to append /soa to the url so the
9
- # middleware can grab the incoming call. A custom endpoint path can be set in
10
- # the middleware.
8
+ #
9
+ #
11
10
  # queue<Boolean>:: turn on queueing for this service call. The incoming
12
11
  # request(className+parameter) will be put onto a resque queue
13
- # debug<Boolean>:: turn on verbose outpur for typhoeus request
14
- attr_accessor :debug, :name, :auth, :queue, :url
12
+ #
13
+ # debug<Boolean>:: turn on verbose output for typhoeus request
14
+ #
15
+ # request_method<Symbol>:: :get :post, :put : delete, used for curl request
16
+
17
+ attr_accessor :debug, :name, :auth, :queue, :request_method
18
+ # raw_url<String>:: raw incoming url string with request method prefixed "GET http://whatever"
19
+ attr_reader :raw_url
15
20
 
16
21
  def initialize(opts)
17
22
  self.name = opts[:name].to_sym
@@ -26,6 +31,10 @@ module KingSoa
26
31
  request = Typhoeus::Easy.new
27
32
  set_request_opts(request, args)
28
33
  resp_code = request.perform
34
+ parse_response(resp_code, request)
35
+ end
36
+
37
+ def parse_response(resp_code, request)
29
38
  case resp_code
30
39
  when 200
31
40
  if request.response_header.include?('Content-Type: application/json')
@@ -36,7 +45,7 @@ module KingSoa
36
45
  end
37
46
  else
38
47
  if request.response_header.include?('Content-Type: application/json')
39
- #decode incoming json .. most likely from KingSoa's rack middleware
48
+ #decode incoming json carriing an error.. most likely from KingSoa's rack middleware
40
49
  return self.decode(request.response_body)["error"]
41
50
  else # return plain body
42
51
  return request.response_body
@@ -94,7 +103,7 @@ module KingSoa
94
103
  # args<Array[]>:: arguments for the soa method, added to post body json encoded
95
104
  def set_request_opts(req, args)
96
105
  req.url = url
97
- req.method = :post
106
+ req.method = request_method || :post
98
107
  req.timeout = 10000 # milliseconds
99
108
  req.params = params(args)
100
109
  req.user_agent = 'KingSoa'
@@ -102,6 +111,38 @@ module KingSoa
102
111
  req.verbose = 1 if debug
103
112
  end
104
113
 
114
+ #=== Params
115
+ #url_string<String>::service location to call.
116
+ #
117
+ #
118
+ # POST(default)
119
+ # http://myUrl.com
120
+ # http://myUrl.com:3000/my_path
121
+ # http://myUrl.com:3000/soa
122
+ #
123
+ # Request types can be defined within url string:
124
+ # GET http://myUrl.com
125
+ # DELETE http://myUrl.com:3000/my_path
126
+ # PUT http://myUrl.com:3000/soa
127
+ # POST http://myUrl.com:3000/custom_post_receiving_path
128
+ #
129
+ # === Returns
130
+ # url<String>:: service Url
131
+ #
132
+ def url=(url_string)
133
+ # grab leading req type, case-insensitive
134
+ if req_type = url_string[/^(GET|POST|PUT|DELETE)/i, 0]
135
+ @request_method = req_type.downcase.to_sym
136
+ end
137
+ @raw_url = url_string
138
+ # grab only url string starting with ht until its end
139
+ @url = url_string[/ht.*/i, 0]
140
+ end
141
+
142
+ def url
143
+ @url
144
+ end
145
+
105
146
  # Params for a soa request consist of following values:
106
147
  # name => name of the soa class to call
107
148
  # args => arguments for the soa class method -> Class.perform(args)
@@ -21,5 +21,17 @@ describe KingSoa::Registry do
21
21
  @reg[:save_document].should == s
22
22
  end
23
23
 
24
+ it "should find a service group" do
25
+ s = KingSoa::Service.new(:name=>:soa_document, :url=>'http://localhost')
26
+ s2 = KingSoa::Service.new(:name=>:soa_test, :url=>'http://localhost')
27
+ s3 = KingSoa::Service.new(:name=>:tester1, :url=>'http://localhost')
28
+ @reg << s
29
+ @reg << s2
30
+ @reg << s3
31
+ @reg.group('soa').should == [s,s2]
32
+ @reg.group('soa').should_not include(s3)
33
+
34
+ end
35
+
24
36
 
25
37
  end
@@ -10,6 +10,27 @@ describe KingSoa::Service, 'in general' do
10
10
  s = KingSoa::Service.new()
11
11
  }.should raise_error
12
12
  end
13
+
14
+ it "should parse request type from url" do
15
+ s = KingSoa::Service.new(:name=>'get_settings_form', :url=>"GET #{test_url}")
16
+ s.url.should == test_url
17
+ s.request_method.should == :get
18
+
19
+ s.url = "DELETE https://whatever"
20
+ s.raw_url.should == "DELETE https://whatever"
21
+ s.url.should == "https://whatever"
22
+ s.request_method.should == :delete
23
+
24
+ s.url = "PUT https://localhost:3000"
25
+ s.url.should == "https://localhost:3000"
26
+ s.request_method.should == :put
27
+ end
28
+
29
+ it "should parse url with invalid request type" do
30
+ s = KingSoa::Service.new(:name=>'get_settings_form', :url=>"NOWAY #{test_url}")
31
+ s.url.should == test_url
32
+ s.request_method.should be_nil
33
+ end
13
34
  end
14
35
 
15
36
  describe KingSoa::Service, 'local request' do
@@ -24,6 +45,8 @@ describe KingSoa::Service, 'queued' do
24
45
  it "should call queue" do
25
46
  s = KingSoa::Service.new(:name=>:local_soa_class, :queue=>:test_queue)
26
47
  s.perform(1,2,3).should be_nil
48
+ #...also test for queue content
49
+
27
50
  end
28
51
  end
29
52
 
@@ -41,18 +64,56 @@ describe KingSoa::Service, 'remote request' do
41
64
  s.perform(1,2,3).should == [1,2,3]
42
65
  end
43
66
 
44
- it "should call a remote service and return auth error" do
67
+ it "should call remote service and return auth error" do
45
68
  s = KingSoa::Service.new(:name=>:soa_test_service, :url=>test_soa_url, :auth=>'wrong')
46
69
  s.perform(1,2,3).should == "Please provide a valid authentication key"
47
70
  end
48
71
 
49
- it "should call a service remote and return not found error" do
72
+ it "should call remote service and return not found error" do
50
73
  s = KingSoa::Service.new(:name=>:wrong_service, :url=>test_soa_url, :auth=>'12345')
51
74
  s.perform(1,2,3).should include("The service: wrong_service could not be found")
52
75
  end
53
76
 
54
- it "should call a remote service without using middleware and returning plain text" do
77
+ it "should call remote service without middleware and return plain text" do
55
78
  s = KingSoa::Service.new(:name=>:non_soa_test_service, :url=> "#{test_url}/non_json_response")
56
79
  s.perform().should == "<h1>hello World</h1>"
57
80
  end
81
+
82
+ it "should GET remote service with params added to url and return params" do
83
+ s = KingSoa::Service.new(:name=>:non_soa_test_service,
84
+ :url=> "GET #{test_url}/get_with_params_test",
85
+ :auth=>'12345' )
86
+ s.request_method.should == :get
87
+ s.perform(:opt_one=>'hi').should == "name=>non_soa_test_service, args=>[{\"opt_one\":\"hi\"}], auth=>12345"
88
+ end
89
+
90
+ it "should GET remote service with multiple params" do
91
+ s = KingSoa::Service.new(:name=>:non_soa_test_service,
92
+ :url=> "GET #{test_url}/get_with_params_test",
93
+ :auth=>'12345' )
94
+ s.request_method.should == :get
95
+ res = s.perform(:opt_one=>'hi', :opt_two=>'there' )
96
+ res.should include("\"opt_one\":\"hi\"")
97
+ res.should include("\"opt_two\":\"there\"")
98
+ end
99
+
100
+ it "should GET remote service and return plain text" do
101
+ s = KingSoa::Service.new(:name=>:non_soa_test_service, :url=> "GET #{test_url}/get_test")
102
+ s.request_method.should == :get
103
+ s.perform().should == "go get it"
104
+ end
105
+
106
+ it "should PUT a remote service and return plain text" do
107
+ s = KingSoa::Service.new(:name=>:non_soa_test_service, :url=> "PUT #{test_url}/put_test")
108
+ s.request_method.should == :put
109
+ s.perform().should == "put it down"
110
+ end
111
+
112
+ it "should DELETE a remote service and return plain text" do
113
+ s = KingSoa::Service.new(:name=>:non_soa_test_service, :url=> "DELETE #{test_url}/delete_test")
114
+ s.request_method.should == :delete
115
+ s.perform().should == "ereased the sucker"
116
+ end
117
+
118
+
58
119
  end
@@ -9,6 +9,10 @@ require "king_soa"
9
9
  # this grabs all /soa requests
10
10
  use KingSoa::Rack::Middleware
11
11
 
12
+ ################################################################################
13
+ # Sinatra endpoints
14
+ ################################################################################
15
+
12
16
  # method to kill this server instance
13
17
  get '/die' do
14
18
  exit!
@@ -20,6 +24,26 @@ post '/non_json_response' do
20
24
  "<h1>hello World</h1>"
21
25
  end
22
26
 
27
+ delete "/delete_test" do
28
+ "ereased the sucker"
29
+ end
30
+
31
+ put "/put_test" do
32
+ "put it down"
33
+ end
34
+
35
+ get "/get_test" do
36
+ "go get it"
37
+ end
38
+
39
+ get "/get_with_params_test" do
40
+ str = []
41
+ params.each do |k,v|
42
+ str << "#{k}=>#{v}"
43
+ end
44
+ str.join(', ')
45
+ end
46
+
23
47
  ################################################################################
24
48
  # Somewhere in you app you define a local service, receiving the incoming call
25
49
  #
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: king_soa
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Georg Leciejewski
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-29 00:00:00 +02:00
18
+ date: 2010-07-16 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency