LVS-JSONService 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ coverage/
3
+ spec/reports
4
+ pkg/
@@ -0,0 +1,7 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: LVS-JSONService
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ require_paths:
6
+ - lib
7
+ platform: ruby
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{JSONService}
5
- s.version = "0.1.0"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["LVS"]
@@ -13,7 +13,9 @@ Gem::Specification.new do |s|
13
13
  "README.rdoc"
14
14
  ]
15
15
  s.files = [
16
- "JSONService.gemspec",
16
+ ".gitignore",
17
+ ".specification",
18
+ "JSONService.gemspec",
17
19
  "LICENSE",
18
20
  "README.rdoc",
19
21
  "Rakefile",
@@ -31,23 +33,22 @@ Gem::Specification.new do |s|
31
33
  "spec/spec.opts",
32
34
  "spec/spec_helper.rb"
33
35
  ]
34
- s.has_rdoc = true
35
36
  s.homepage = %q{http://github.com/lvs/JSONService}
36
37
  s.rdoc_options = ["--charset=UTF-8"]
37
38
  s.require_paths = ["lib"]
38
- s.rubygems_version = %q{1.3.1}
39
- s.summary = %q{TODO}
39
+ s.rubygems_version = %q{1.3.3}
40
+ s.summary = %q{A Ruby library for interacting with external JSON services}
40
41
  s.test_files = [
41
- "spec/lvs/json_service/base_spec.rb",
42
+ "spec/json_service_spec.rb",
43
+ "spec/lvs/json_service/base_spec.rb",
42
44
  "spec/lvs/json_service/logger_spec.rb",
43
45
  "spec/lvs/json_service/request_spec.rb",
44
- "spec/spec_helper.rb",
45
- "spec/json_service_spec.rb"
46
+ "spec/spec_helper.rb"
46
47
  ]
47
48
 
48
49
  if s.respond_to? :specification_version then
49
50
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
- s.specification_version = 2
51
+ s.specification_version = 3
51
52
 
52
53
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
54
  else
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -4,72 +4,80 @@ require 'lvs/json_service/logger'
4
4
  module LVS
5
5
  module JsonService
6
6
  module Request
7
- def self.http_request_with_timeout(service, args, options)
7
+
8
+ def self.included(base) # :nodoc:
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ def http_request_with_timeout(service, args, options)
8
15
 
9
- uri = URI.parse(service)
16
+ uri = URI.parse(service)
10
17
 
11
- http = Net::HTTP.new(uri.host, uri.port)
18
+ http = Net::HTTP.new(uri.host, uri.port)
12
19
 
13
- if options[:encrypted] && !SSL_DISABLED
14
- http.use_ssl = true
15
- if options[:auth_cert]
16
- http.cert = OpenSSL::X509::Certificate.new(File.read(options[:auth_cert]))
17
- http.key = OpenSSL::PKey::RSA.new(File.read(options[:auth_key]), options[:auth_key_password])
20
+ if options[:encrypted] && !SSL_DISABLED
21
+ http.use_ssl = true
22
+ if options[:auth_cert]
23
+ http.cert = OpenSSL::X509::Certificate.new(File.read(options[:auth_cert]))
24
+ http.key = OpenSSL::PKey::RSA.new(File.read(options[:auth_key]), options[:auth_key_password])
25
+ end
18
26
  end
19
- end
20
27
 
21
- http.open_timeout = options[:timeout] || 1
22
- http.read_timeout = options[:timeout] || 1
28
+ http.open_timeout = options[:timeout] || 1
29
+ http.read_timeout = options[:timeout] || 1
23
30
 
24
- req = Net::HTTP::Post.new(uri.path)
25
- req.form_data = { "object_request" => args.to_json }
31
+ req = Net::HTTP::Post.new(uri.path)
32
+ req.form_data = { "object_request" => args.to_json }
26
33
 
27
- retries = options[:retries] || 0
34
+ retries = options[:retries] || 0
28
35
 
29
- begin
30
- retries -= 1
31
- response = http.start { |connection| connection.request(req) }
36
+ begin
37
+ retries -= 1
38
+ response = http.start { |connection| connection.request(req) }
32
39
 
33
- rescue Timeout::Error
34
- if retries >= 0
35
- LVS::JsonService::Logger.debug(
36
- "Retrying #{service} due to TimeoutError"
37
- )
38
- retry
39
- end
40
- raise LVS::JsonService::TimeoutError.new("Backend failed to respond in time", 500, service, args)
40
+ rescue Timeout::Error
41
+ if retries >= 0
42
+ LVS::JsonService::Logger.debug(
43
+ "Retrying #{service} due to TimeoutError"
44
+ )
45
+ retry
46
+ end
47
+ raise LVS::JsonService::TimeoutError.new("Backend failed to respond in time", 500, service, args)
41
48
 
42
- rescue Errno::ECONNREFUSED
43
- if retries >= 0
44
- LVS::JsonService::Logger.debug(
45
- "Retrying #{service} due to Errno::ECONNREFUSED"
46
- )
47
- sleep(1)
48
- retry
49
+ rescue Errno::ECONNREFUSED
50
+ if retries >= 0
51
+ LVS::JsonService::Logger.debug(
52
+ "Retrying #{service} due to Errno::ECONNREFUSED"
53
+ )
54
+ sleep(1)
55
+ retry
56
+ end
57
+ raise LVS::JsonService::BackendUnavailableError.new("Backend unavailable", 500, service, args)
49
58
  end
50
- raise LVS::JsonService::BackendUnavailableError.new("Backend unavailable", 500, service, args)
51
- end
52
-
53
- if response.is_a?(Net::HTTPNotFound)
54
- raise LVS::JsonService::NotFoundError.new("404 Found for the service", 404, service, args)
55
- end
56
59
 
57
- response
58
- end
60
+ if response.is_a?(Net::HTTPNotFound)
61
+ raise LVS::JsonService::NotFoundError.new("404 Found for the service", 404, service, args)
62
+ end
59
63
 
60
- def self.run_remote_request(service, args, options = {})
61
- LVS::JsonService::Logger.debug "run_remote_request('#{service}', #{args.to_json}"
62
- response = http_request_with_timeout(service, args, options)
63
- if response.body.size < 1024
64
- LVS::JsonService::Logger.debug "Response: #{response.body.gsub(/\n/, '')}"
65
- else
66
- LVS::JsonService::Logger.debug "Response Snippet: #{response.body.gsub(/\n/, '')[0..1024]}"
64
+ response
67
65
  end
68
- result = JSON.parse(response.body)
69
- if result.is_a?(Hash) && result.has_key?("PCode")
70
- raise LVS::JsonService::Error.new(result["message"], result["PCode"], service, args, result)
66
+
67
+ def run_remote_request(service, args, options = {})
68
+ LVS::JsonService::Logger.debug "run_remote_request('#{service}', #{args.to_json}"
69
+ response = http_request_with_timeout(service, args, options)
70
+ if response.body.size < 1024
71
+ LVS::JsonService::Logger.debug "Response: #{response.body.gsub(/\n/, '')}"
72
+ else
73
+ LVS::JsonService::Logger.debug "Response Snippet: #{response.body.gsub(/\n/, '')[0..1024]}"
74
+ end
75
+ result = JSON.parse(response.body)
76
+ if result.is_a?(Hash) && result.has_key?("PCode")
77
+ raise LVS::JsonService::Error.new(result["message"], result["PCode"], service, args, result)
78
+ end
79
+ result
71
80
  end
72
- result
73
81
  end
74
82
  end
75
83
  end
@@ -121,12 +121,12 @@ describe LVS::JsonService::Request do
121
121
 
122
122
  before :each do
123
123
  @options = {:retries => 2}
124
- LVS::JsonService::Request.stub!(:sleep)
124
+ ClassWithRequest.stub!(:sleep)
125
125
  end
126
126
 
127
127
  it "should sleep for 1 second before each timeout" do
128
128
  @connection.stub!(:request).and_raise(Errno::ECONNREFUSED)
129
- LVS::JsonService::Request.should_receive(:sleep).with(1)
129
+ ClassWithRequest.should_receive(:sleep).with(1)
130
130
  do_request_catching_errors
131
131
  end
132
132
 
@@ -181,7 +181,7 @@ describe LVS::JsonService::Request do
181
181
  end
182
182
 
183
183
  def do_request
184
- LVS::JsonService::Request.http_request_with_timeout(@url, @args, @options)
184
+ ClassWithRequest.http_request_with_timeout(@url, @args, @options)
185
185
  end
186
186
 
187
187
  def do_request_catching_errors
@@ -197,10 +197,10 @@ describe LVS::JsonService::Request do
197
197
  end
198
198
 
199
199
  it "should call http_request_with_timeout with service, args and options" do
200
- LVS::JsonService::Request.should_receive(:http_request_with_timeout).
200
+ ClassWithRequest.should_receive(:http_request_with_timeout).
201
201
  with(@url, @args, @options).
202
202
  and_return(@response)
203
- LVS::JsonService::Request.run_remote_request(@url, @args, @options)
203
+ ClassWithRequest.run_remote_request(@url, @args, @options)
204
204
  end
205
205
 
206
206
  it "should return the parsed JSON result" do
@@ -208,17 +208,17 @@ describe LVS::JsonService::Request do
208
208
  {"id"=>1100, "description"=>"Handball (ABP)"},
209
209
  {"id"=>978400, "description"=>"Casino Roulette"}
210
210
  ]
211
- LVS::JsonService::Request.stub!(:http_request_with_timeout).and_return(@response)
212
- LVS::JsonService::Request.run_remote_request(@url, @args, @options).should == expected_result
211
+ ClassWithRequest.stub!(:http_request_with_timeout).and_return(@response)
212
+ ClassWithRequest.run_remote_request(@url, @args, @options).should == expected_result
213
213
  end
214
214
 
215
215
  it "should raise an error if the response contains PCode" do
216
216
  error_response = load_fixture('error_response.yml')
217
- LVS::JsonService::Request.stub!(:http_request_with_timeout).
217
+ ClassWithRequest.stub!(:http_request_with_timeout).
218
218
  and_return(error_response)
219
219
 
220
220
  lambda {
221
- LVS::JsonService::Request.run_remote_request(@url, @args, @options)
221
+ ClassWithRequest.run_remote_request(@url, @args, @options)
222
222
  }.should raise_error(LVS::JsonService::Error)
223
223
  end
224
224
  end
@@ -247,3 +247,8 @@ class MockNetHttp
247
247
  end
248
248
 
249
249
  end
250
+
251
+ class ClassWithRequest
252
+ include LVS::JsonService::Request
253
+ end
254
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: LVS-JSONService
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LVS
@@ -23,6 +23,8 @@ extra_rdoc_files:
23
23
  - LICENSE
24
24
  - README.rdoc
25
25
  files:
26
+ - .gitignore
27
+ - .specification
26
28
  - JSONService.gemspec
27
29
  - LICENSE
28
30
  - README.rdoc
@@ -40,7 +42,7 @@ files:
40
42
  - spec/lvs/json_service/request_spec.rb
41
43
  - spec/spec.opts
42
44
  - spec/spec_helper.rb
43
- has_rdoc: true
45
+ has_rdoc: false
44
46
  homepage: http://github.com/lvs/JSONService
45
47
  post_install_message:
46
48
  rdoc_options:
@@ -64,11 +66,11 @@ requirements: []
64
66
  rubyforge_project:
65
67
  rubygems_version: 1.2.0
66
68
  signing_key:
67
- specification_version: 2
68
- summary: TODO
69
+ specification_version: 3
70
+ summary: A Ruby library for interacting with external JSON services
69
71
  test_files:
72
+ - spec/json_service_spec.rb
70
73
  - spec/lvs/json_service/base_spec.rb
71
74
  - spec/lvs/json_service/logger_spec.rb
72
75
  - spec/lvs/json_service/request_spec.rb
73
76
  - spec/spec_helper.rb
74
- - spec/json_service_spec.rb