LVS-JSONService 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .DS_Store
2
2
  coverage/
3
3
  spec/reports
4
- pkg/
4
+ pkg/
5
+ JSONService-*.gem
data/.specification 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.2.0
4
+ version: 0.2.1
5
5
  require_paths:
6
6
  - lib
7
7
  platform: ruby
data/JSONService.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{JSONService}
5
- s.version = "0.2.0"
5
+ s.version = "0.2.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["LVS"]
9
- s.date = %q{2009-06-18}
9
+ s.date = %q{2009-06-19}
10
10
  s.email = %q{info@lvs.co.uk}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -7,130 +7,129 @@ module LVS
7
7
  class Base
8
8
  include ::LVS::JsonService::Request
9
9
 
10
- @@services = []
11
- @@cache = CACHE if defined?(CACHE)
12
- @@service_prefix = ""
13
10
  attr_accessor :fields
14
- cattr_accessor :service_prefix
15
- cattr_accessor :field_prefix
16
- cattr_accessor :cache
17
11
 
18
12
  protected
19
13
 
20
- def self.site=(value)
21
- # value is containing AGP_LOCATION already sometimes:
22
- if SSL_DISABLED
23
- value.gsub!(/^#{AGP_LOCATION}/, '') if AGP_LOCATION && value.match(/#{AGP_LOCATION}/)
24
- agp = AGP_LOCATION.gsub(/\/$/, '')
25
- else
26
- value.gsub!(/^#{AGP_LOCATION}/, '')
27
- value.gsub!(/^#{SSL_AGP_LOCATION}/, '') if SSL_AGP_LOCATION && value.match(/#{SSL_AGP_LOCATION}/)
28
- agp = SSL_AGP_LOCATION.gsub(/\/$/, '')
29
- end
30
- value.gsub!(/^\//, '')
31
- @@site = (agp + '/' + value)
32
- end
33
-
34
- def self.debug(message)
35
- Rails.logger.debug " \033[1;4;32mLVS::JsonService\033[0m #{message}"
36
- end
37
-
38
- def self.define_cached_service(name, service, options)
39
- (class<<self;self;end).send :define_method, name do |*args|
40
- begin
41
- args = args.first || {}
42
- options[:cache_time] ||= 10
43
- service_name = "call_#{name}"
44
- mutex ||= Mutex::new
45
- tried = false
46
- mutex.lock if AppTools.is_memcached_threaded?
47
- begin
48
- jsn_data = { }
49
- if ActionController::Base.perform_caching && @@cache
50
- Rails.logger.info("JSON API CACHED CALL: #{service} - #{args.to_yaml}")
51
- key = "json:call:" + Digest::MD5.hexdigest("#{service}:#{args.to_s}")
52
- cached = @@cache.get(key)
53
- if cached.nil?
54
- result = self.send(service_name, args)
55
- @@cache.set(key, result, options[:cache_time])
56
- jsn_data = result
57
- else
58
- jsn_data = cached
59
- end
60
- else
61
- Rails.logger.info("JSON API CALL: #{method} - #{args.to_yaml}")
62
- jsn_data = self.send(service_name, args)
63
- end
64
- jsn_data
65
- rescue MemCache::MemCacheError => err
66
- raise err if tried
67
- Rails.logger.info("JSON API CALL RETRY: #{err} - #{method} - #{args.to_yaml}")
68
- tried = true
69
- retry
70
- end
71
- ensure
72
- mutex.unlock if AppTools.is_memcached_threaded?
73
- end
14
+ class << self
15
+ @site = ""
16
+ @services = []
17
+ @service_prefix = ""
18
+ @field_prefix = ""
19
+ @encrypted = false
20
+ @auth_cert = ""
21
+ @auth_key = ""
22
+ @auth_key_pass = ""
23
+
24
+ def encrypted=(value)
25
+ @encrypted = value
26
+ end
27
+
28
+ def auth_cert=(value)
29
+ @auth_cert = value
30
+ end
31
+
32
+ def auth_key=(value)
33
+ @auth_key = value
34
+ end
35
+
36
+ def auth_key_pass=(value)
37
+ @auth_key_pass = value
74
38
  end
75
- end
76
39
 
77
- def self.define_service(name, service, options = {})
78
- service_name = name
40
+ def agp_location=(value)
41
+ @agp_location = value
42
+ end
43
+
44
+ def add_service(service)
45
+ @services ||= []
46
+ @services = @services << service
47
+ end
48
+
49
+ def service_prefix=(value)
50
+ @service_prefix = value
51
+ end
52
+
53
+ def field_prefix=(value)
54
+ @field_prefix = value
55
+ end
56
+
57
+ def site=(value)
58
+ # value is containing AGP_LOCATION already sometimes:
59
+ value.gsub!(/^#{AGP_LOCATION}/, '') if AGP_LOCATION && value.match(/#{AGP_LOCATION}/)
60
+ agp = @agp_location ? @agp_location : AGP_LOCATION
61
+ agp.gsub!(/\/$/, '')
62
+ value.gsub!(/^\//, '')
63
+ @site = (agp + '/' + value)
64
+ end
79
65
 
80
- service_path = service.split('.')
81
- if service_path.size <= 2
82
- internal_service = service
83
- prefix = @@service_prefix
84
- else
85
- internal_service = service_path[-2..-1].join('.')
86
- prefix = service_path[0..-3].join('.') + '.'
66
+ def debug(message)
67
+ LVS::JsonService::Logger.debug " \033[1;4;32mLVS::JsonService\033[0m #{message}"
87
68
  end
88
69
 
89
- if options[:cached]
90
- service_name = "call_#{name}"
91
- self.define_cached_service(name, service, options)
70
+ def require_ssl?
71
+ (Module.const_defined?(:SSL_ENABLED) && SSL_ENABLED) || (Module.const_defined?(:SSL_DISABLED) && !SSL_DISABLED)
92
72
  end
93
73
 
94
- (class<<self;self;end).send :define_method, service_name do |args|
95
- method_params, flags = args
74
+ def define_service(name, service, options = {})
75
+ service_name = name
96
76
 
97
- method_params ||= {}
98
- options[:defaults] ||= {}
99
- options[:defaults].each_pair do |key, value|
100
- method_params[key] = value if method_params[key].blank?
101
- end
102
- options[:required] ||= {}
103
- options[:required].each do |key|
104
- raise LVS::JsonService::Error.new("Required field #{key} wasn't supplied", internal_service, '0', method_params) if method_params[key].blank?
105
- end
106
- result = self.run_remote_request(@@site + prefix + internal_service, method_params, options)
107
- if flags && flags[:raw]
108
- result
77
+ service_path = service.split('.')
78
+ if service_path.size <= 2
79
+ internal_service = service
80
+ prefix = @service_prefix
109
81
  else
110
- self.parse_result(result)
82
+ internal_service = service_path[-2..-1].join('.')
83
+ prefix = service_path[0..-3].join('.') + '.'
111
84
  end
112
- end
113
85
 
114
- @@services << name
115
- end
86
+ options[:encrypted] = @encrypted if @encrypted
87
+ options[:auth_cert] = @auth_cert if @auth_cert
88
+ options[:auth_key] = @auth_key if @auth_key
89
+ options[:auth_key_pass] = @auth_key_pass if @auth_key_pass
90
+
91
+ (class<<self;self;end).send :define_method, service_name do |args|
92
+ method_params, flags = args
93
+
94
+ method_params ||= {}
95
+ options[:defaults] ||= {}
96
+ options[:defaults].each_pair do |key, value|
97
+ method_params[key] = value if method_params[key].blank?
98
+ end
99
+ options[:required] ||= {}
100
+ options[:required].each do |key|
101
+ raise LVS::JsonService::Error.new("Required field #{key} wasn't supplied", internal_service, '0', method_params) if method_params[key].blank?
102
+ end
103
+ result = self.run_remote_request(@site + prefix + internal_service, method_params, options)
104
+ if flags && flags[:raw]
105
+ result
106
+ else
107
+ self.parse_result(result)
108
+ end
109
+ end
116
110
 
117
- def self.fake_service(name, json)
118
- (class<<self;self;end).send :define_method, name do |*args|
119
- self.parse_result(JSON.parse(json))
111
+ add_service(name)
120
112
  end
121
- @@services << name
122
- end
123
113
 
124
- def self.services
125
- @@services
126
- end
114
+ def fake_service(name, json)
115
+ (class<<self;self;end).send :define_method, name do |*args|
116
+ self.parse_result(JSON.parse(json))
117
+ end
118
+ add_service(name)
119
+ end
127
120
 
128
- def self.parse_result(response)
129
- if response.is_a?(Array)
130
- response.map { |x| self.new(x) }
131
- else
132
- self.new(response)
121
+ def services
122
+ @services
123
+ end
124
+
125
+ def parse_result(response)
126
+ if response.is_a?(Array)
127
+ response.map { |x| self.new(x) }
128
+ else
129
+ self.new(response)
130
+ end
133
131
  end
132
+
134
133
  end
135
134
 
136
135
  def initialize(values = {})
@@ -2,9 +2,8 @@ module LVS
2
2
  module JsonService
3
3
  module Logger
4
4
  def self.debug(message)
5
- if const_defined?(:RAILS_DEFAULT_LOGGER)
6
- RAILS_DEFAULT_LOGGER.debug(message)
7
- end
5
+ message = " \033[1;4;32mLVS::JsonService\033[0m #{message}"
6
+ Rails.logger.debug(message)
8
7
  end
9
8
  end
10
9
  end
@@ -17,9 +17,14 @@ module LVS
17
17
 
18
18
  http = Net::HTTP.new(uri.host, uri.port)
19
19
 
20
- if options[:encrypted] && !SSL_DISABLED
20
+ http = Net::HTTP.new(uri.host, uri.port)
21
+ if options[:encrypted] || require_ssl?
21
22
  http.use_ssl = true
23
+ # Self-signed certs give streams of "warning: peer certificate won't be verified in this SSL session"
24
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
25
+ LVS::JsonService::Logger.debug "Using SSL"
22
26
  if options[:auth_cert]
27
+ LVS::JsonService::Logger.debug "Using Auth"
23
28
  http.cert = OpenSSL::X509::Certificate.new(File.read(options[:auth_cert]))
24
29
  http.key = OpenSSL::PKey::RSA.new(File.read(options[:auth_key]), options[:auth_key_password])
25
30
  end
@@ -37,7 +42,7 @@ module LVS
37
42
  retries -= 1
38
43
  response = http.start { |connection| connection.request(req) }
39
44
 
40
- rescue Timeout::Error
45
+ rescue Timeout::Error => e
41
46
  if retries >= 0
42
47
  LVS::JsonService::Logger.debug(
43
48
  "Retrying #{service} due to TimeoutError"
@@ -46,7 +51,7 @@ module LVS
46
51
  end
47
52
  raise LVS::JsonService::TimeoutError.new("Backend failed to respond in time", 500, service, args)
48
53
 
49
- rescue Errno::ECONNREFUSED
54
+ rescue Errno::ECONNREFUSED => e
50
55
  if retries >= 0
51
56
  LVS::JsonService::Logger.debug(
52
57
  "Retrying #{service} due to Errno::ECONNREFUSED"
@@ -55,6 +60,10 @@ module LVS
55
60
  retry
56
61
  end
57
62
  raise LVS::JsonService::BackendUnavailableError.new("Backend unavailable", 500, service, args)
63
+
64
+ rescue OpenSSL::SSL::SSLError => e
65
+ raise LVS::JsonService::BackendUnavailableError.new("Backend unavailable #{e}", 500, service, args)
66
+
58
67
  end
59
68
 
60
69
  if response.is_a?(Net::HTTPNotFound)
@@ -65,12 +74,26 @@ module LVS
65
74
  end
66
75
 
67
76
  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)
77
+ LVS::JsonService::Logger.debug "Requesting '#{service}' with #{args.to_json}"
78
+
79
+ if options[:cached_for]
80
+ timing = "CACHED"
81
+ response = Rails.cache.fetch([service, args].cache_key, :expires_in => options[:cached_for]) do
82
+ start = Time.now
83
+ response = http_request_with_timeout(service, args, options)
84
+ timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
85
+ response
86
+ end
87
+ else
88
+ start = Time.now
89
+ response = http_request_with_timeout(service, args, options)
90
+ timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
91
+ end
92
+
70
93
  if response.body.size < 1024
71
- LVS::JsonService::Logger.debug "Response: #{response.body.gsub(/\n/, '')}"
94
+ LVS::JsonService::Logger.debug "Response (#{timing}): #{response.body.gsub(/\n/, '')}"
72
95
  else
73
- LVS::JsonService::Logger.debug "Response Snippet: #{response.body.gsub(/\n/, '')[0..1024]}"
96
+ LVS::JsonService::Logger.debug "Response Snippet (#{timing}): #{response.body.gsub(/\n/, '')[0..1024]}"
74
97
  end
75
98
  result = JSON.parse(response.body)
76
99
  if result.is_a?(Hash) && result.has_key?("PCode")
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - LVS
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-18 00:00:00 -07:00
12
+ date: 2009-06-19 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15