nps_sdk 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f52f959344e61f3c2229da2ad383f76992f4404e801a0b6d2af437af91b3bad
4
- data.tar.gz: 20dcafc06db6c03aa8d3b61f2e35fb89dddbd0740923eec590fb4b735c8e515d
3
+ metadata.gz: 91bef4b7f40520eb77e2e6a0da83b4bced5c1553c2e39b078d0ef9588e20122e
4
+ data.tar.gz: 28ea5d36c97e0e452ff428eee01f3134d62e6478a89adb16233bd3cb26ca307c
5
5
  SHA512:
6
- metadata.gz: 351ab4c51b195f2927f5d0e6eddc57ee6ff927cef3df0ada65d3847417677620b8f358917bc16ef96e1dc0b02724b44d000c541e72ef520be9947606b4385dbb
7
- data.tar.gz: 11b49693d978ef533d26696422ed646e4400a2dfaa829b31f9b99ca718745210f1a176f4d1ce96da2b1ab3862aaa1815ace8dbb7cc2148824bc243d00d2456fb
6
+ metadata.gz: 7ff32579588daa0f8fa0dc2fc2ed214d9867356cd922338236d223d22516d733b45346cde0c59d99adf309823b69a829d0ab9ba666cd2991cdbec2d808748856
7
+ data.tar.gz: b0b0a774e27e9d059a8720f89251e0254325aa4abbdccd319790a4fda82587825d4c901f6f72f49b9ed0c07d920972808613f8e6fb7b40bbc3967735babaaee1
@@ -8,4 +8,44 @@ class LoggerException < StandardError
8
8
  def message
9
9
  "DEBUG level is now allowed in PRODUCTION ENVIRONMENT"
10
10
  end
11
+ end
12
+
13
+ class EmptyCustomUrlsException < StandardError
14
+ def message
15
+ "CustomUrls cannot by empty"
16
+ end
17
+ end
18
+
19
+ class InvalidUrlException < StandardError
20
+ def message
21
+ "Invalid URL"
22
+ end
23
+
24
+ def self.error_message_with_url (url)
25
+ "Invalid URL - #{url}"
26
+ end
27
+ end
28
+
29
+ class WrongConfigurationException < StandardError
30
+ def message
31
+ "Only CUSTOM_ENV environment is available with CustomUrls"
32
+ end
33
+ end
34
+
35
+ class MissingEnvironmentException < StandardError
36
+ def message
37
+ "Environment field not specified"
38
+ end
39
+ end
40
+
41
+ class MaxCustomUrlsException < StandardError
42
+ def message
43
+ "Custom Urls must contain at most 2 urls"
44
+ end
45
+ end
46
+
47
+ class EmptyCustomUrlsWithCustomEnvException < StandardError
48
+ def message
49
+ "CustomEnvUrls cannot be empty using CUSTOM_ENV environment"
50
+ end
11
51
  end
@@ -14,6 +14,7 @@ module Nps
14
14
  attr_accessor :proxy_url
15
15
  attr_accessor :proxy_username
16
16
  attr_accessor :proxy_password
17
+ attr_accessor :custom_env_urls
17
18
 
18
19
  end
19
20
  end
@@ -1,8 +1,40 @@
1
1
  module Nps
2
2
  class Environments
3
- SANDBOX_ENV = "sandbox.wsdl"
4
- STAGING_ENV = "staging.wsdl"
5
- PRODUCTION_ENV = "production.wsdl"
6
- DEVELOPMENT_ENV = "development.wsdl"
3
+ PRODUCTION_ENV = 0
4
+ SANDBOX_ENV = 1
5
+ STAGING_ENV = 2
6
+ DEVELOPMENT_ENV = 3
7
+ CUSTOM_ENV = 4
8
+
9
+ SANDBOX_WSDL = "sandbox.wsdl"
10
+ STAGING_WSDL = "staging.wsdl"
11
+ PRODUCTION_WSDL = "production.wsdl"
12
+ DEVELOPMENT_WSDL = "development.wsdl"
13
+ CUSTOM_WSDL = "production.wsdl"
14
+
15
+ private_constant :SANDBOX_WSDL
16
+ private_constant :STAGING_WSDL
17
+ private_constant :PRODUCTION_WSDL
18
+ private_constant :DEVELOPMENT_WSDL
19
+ private_constant :CUSTOM_WSDL
20
+
21
+ def self.get_wsdl (environment)
22
+ begin
23
+ if environment < 0
24
+ raise "Invalid environment"
25
+ end
26
+ environments = [
27
+ PRODUCTION_WSDL,
28
+ STAGING_WSDL,
29
+ SANDBOX_WSDL,
30
+ DEVELOPMENT_WSDL,
31
+ CUSTOM_WSDL
32
+ ]
33
+ return environments[environment]
34
+ rescue
35
+ raise "Environment not found"
36
+ end
37
+ end
38
+
7
39
  end
8
40
  end
@@ -5,7 +5,7 @@ module Nps
5
5
 
6
6
  def call(severity, time, progname, msg)
7
7
  msg = ofuscate(msg)
8
- "#{time} - #{severity} - NpsSDK - #{msg2str(msg)}"
8
+ "#{time} - #{severity} - NpsSDK - #{msg2str(msg)} \n"
9
9
  end
10
10
 
11
11
  def ofuscate_cvc(msg)
@@ -1,5 +1,7 @@
1
1
  require 'certifi'
2
2
  require_relative 'services'
3
+ require_relative 'environments'
4
+ require_relative 'utils'
3
5
  module Nps
4
6
  class SoapClient
5
7
 
@@ -13,12 +15,48 @@ module Nps
13
15
  end
14
16
  if conf.log_level == Logger::DEBUG and conf.environment == Nps::Environments::PRODUCTION_ENV
15
17
  raise LoggerException
16
- end
18
+ end
19
+
20
+ if conf.environment.nil?
21
+ raise MissingEnvironmentException
22
+ end
23
+
24
+ if conf.environment == Nps::Environments::CUSTOM_ENV and conf.custom_env_urls.nil?
25
+ raise EmptyCustomUrlsWithCustomEnvException
26
+ end
27
+
28
+ if conf.custom_env_urls
29
+ if conf.environment != Nps::Environments::CUSTOM_ENV
30
+ raise WrongConfigurationException
31
+ end
32
+
33
+ if conf.custom_env_urls.length > Nps::Utils::MAX_CUSTOM_URLS
34
+ raise MaxCustomUrlsException
35
+ end
36
+
37
+ if conf.custom_env_urls.kind_of?(Array) and conf.custom_env_urls.empty?
38
+ raise EmptyCustomUrlsException
39
+ end
40
+
41
+ conf.custom_env_urls.each do |url|
42
+ # if url =~ /\A#{URI::regexp}\z/
43
+ # raise InvalidUrlException.error_message_with_url url
44
+ # end
45
+
46
+ unless url_valid? url
47
+ raise InvalidUrlException.error_message_with_url url
48
+ end
49
+
50
+ # unless valid_url_1? url
51
+ # raise InvalidUrlException.error_message_with_url url
52
+ # end
53
+ end
54
+ end
17
55
 
18
56
  @key = conf.key
19
57
  @log = true
20
58
  @logger = conf.logger
21
- @wsdl = conf.environment
59
+ @wsdl = Nps::Environments::get_wsdl(conf.environment)
22
60
  @open_timeout = conf.o_timeout.nil? ? 5 : conf.o_timeout
23
61
  @read_timeout = conf.r_timeout.nil? ? 60 : conf.r_timeout
24
62
  @verify_ssl = conf.verify_ssl.nil? ? true : conf.verify_ssl
@@ -27,15 +65,28 @@ module Nps
27
65
  @proxy = conf.proxy_url ? conf.proxy_url : nil
28
66
  @proxy_username = conf.proxy_username ? @proxy_username : nil
29
67
  @proxy_password = conf.proxy_password ? @proxy_password : nil
68
+ @custom_env_urls = conf.custom_env_urls ? conf.custom_env_urls : nil
30
69
 
31
70
  setup
32
71
  end
33
72
 
73
+ def url_valid?(url)
74
+ url = URI.parse(url) rescue false
75
+ url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
76
+ end
77
+
78
+ def valid_url_1?(url)
79
+ uri = URI.parse(url)
80
+ uri.is_a?(URI::HTTP) && !uri.host.nil?
81
+ rescue URI::InvalidURIError
82
+ false
83
+ end
84
+
34
85
  def setup
35
- client_config = {
86
+ @client_config = {
36
87
  ssl_verify_mode: :none,
37
- wsdl: File.join(File.dirname(File.expand_path(__FILE__)), "/wsdl/" + @wsdl),
38
88
  logger: @logger,
89
+ wsdl: File.join(File.dirname(File.expand_path(__FILE__)), "/wsdl/" + @wsdl),
39
90
  open_timeout: @open_timeout,
40
91
  read_timeout: @read_timeout,
41
92
  pretty_print_xml: true,
@@ -46,7 +97,7 @@ module Nps
46
97
  lvl_config = {
47
98
  log_level: :debug
48
99
  }
49
- client_config.merge!(lvl_config)
100
+ @client_config.merge!(lvl_config)
50
101
  end
51
102
 
52
103
  if @verify_ssl
@@ -54,26 +105,23 @@ module Nps
54
105
  ssl_verify_mode: :peer,
55
106
  ssl_ca_cert_file: Certifi.where
56
107
  }
57
- client_config.merge!(ssl_config)
108
+ @client_config.merge!(ssl_config)
58
109
  end
59
110
 
60
111
  if @proxy
61
112
  proxy = {
62
113
  proxy: @proxy
63
114
  }
64
- client_config.merge!(proxy)
115
+ @client_config.merge!(proxy)
65
116
  end
66
117
 
67
118
  if @proxy_username
68
119
  proxy_auth = {
69
120
  headers: { "Proxy-Authorization" => "Basic #{secret}" }
70
121
  }
71
- client_config.merge!(proxy_auth)
122
+ @client_config.merge!(proxy_auth)
72
123
  end
73
124
 
74
-
75
- @client = Savon.client client_config
76
-
77
125
  end
78
126
 
79
127
  def add_secure_hash(params)
@@ -124,11 +172,43 @@ module Nps
124
172
  params = add_secure_hash(params)
125
173
  end
126
174
  params = {"Requerimiento" => params}
127
- begin
128
- @client.call(service, message: params).body
129
- rescue TimeoutError
130
- raise ApiException
131
- end
175
+
176
+ if @custom_env_urls
177
+ internal_connection_timeout = @read_timeout
178
+ @custom_env_urls.each do |url|
179
+ begin
180
+ start_time = Time.now
181
+ @client_config.merge!({endpoint: url,
182
+ open_timeout: Nps::Utils::CUSTOM_URL_CONNECTION_TIMEOUT,
183
+ read_timeout: internal_connection_timeout})
184
+ @client = Savon.client @client_config
185
+ @client.call(service, message: params).body
186
+ break
187
+ rescue SocketError, HTTPClient::ConnectTimeoutError
188
+ if @custom_env_urls.last == url
189
+ raise ApiException
190
+ end
191
+ @logger.warn("Could not connect to #{url}")
192
+ finish_time = Time.now
193
+ delta_time = finish_time - start_time
194
+ internal_connection_timeout -= delta_time.ceil
195
+ next
196
+ rescue HTTPClient::ReceiveTimeoutError
197
+ raise ApiException
198
+ rescue StandardError
199
+ raise 'An unexpected error occurred'
200
+ end
201
+ end
202
+ else
203
+ begin
204
+ @client = Savon.client @client_config
205
+ @client.call(service, message: params).body
206
+ rescue HTTPClient::ReceiveTimeoutError
207
+ raise ApiException
208
+ rescue StandardError
209
+ raise 'An unexpected error occurred'
210
+ end
211
+ end
132
212
  end
133
213
  end
134
214
  end
@@ -4,6 +4,8 @@ module Nps
4
4
  API_VERSION = {version: '2.2'}
5
5
  CACHE_TTL = 1
6
6
  TIMEOUT = 90
7
+ MAX_CUSTOM_URLS = 2
8
+ CUSTOM_URL_CONNECTION_TIMEOUT = 3
7
9
 
8
10
  SANITIZE = {
9
11
  'psp_Person.FirstName.max_length' => '128',
@@ -1,5 +1,5 @@
1
1
  module Nps
2
2
  class Version
3
- VERSION = '1.2.0'
3
+ VERSION = '1.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nps_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - merchantservices@ingenico.com