creditsafe 0.6.2 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +17 -17
- data/.gitignore +4 -3
- data/.rspec +1 -1
- data/.rubocop.yml +14 -11
- data/.ruby-version +1 -1
- data/CHANGELOG.md +57 -53
- data/Gemfile +5 -5
- data/LICENSE.txt +22 -22
- data/README.md +175 -175
- data/creditsafe.gemspec +34 -35
- data/data/creditsafe-live.xml +342 -342
- data/data/creditsafe-test.xml +342 -342
- data/lib/creditsafe.rb +4 -4
- data/lib/creditsafe/client.rb +165 -165
- data/lib/creditsafe/constants.rb +49 -49
- data/lib/creditsafe/errors.rb +17 -17
- data/lib/creditsafe/match_type.rb +115 -115
- data/lib/creditsafe/messages.rb +98 -98
- data/lib/creditsafe/namespace.rb +20 -20
- data/lib/creditsafe/request/company_report.rb +42 -42
- data/lib/creditsafe/request/find_company.rb +120 -120
- data/lib/creditsafe/version.rb +5 -5
- data/spec/creditsafe/client_spec.rb +431 -431
- data/spec/creditsafe/messages_spec.rb +76 -76
- data/spec/fixtures/company-report-not-found.xml +13 -13
- data/spec/fixtures/company-report-request.xml +1 -1
- data/spec/fixtures/company-report-successful.xml +582 -582
- data/spec/fixtures/error-fault.xml +8 -8
- data/spec/fixtures/error-invalid-credentials.html +31 -31
- data/spec/fixtures/find-companies-error-no-text.xml +11 -11
- data/spec/fixtures/find-companies-error.xml +11 -11
- data/spec/fixtures/find-companies-none-found.xml +13 -13
- data/spec/fixtures/find-companies-request.xml +1 -1
- data/spec/fixtures/find-companies-successful-multi.xml +493 -493
- data/spec/fixtures/find-companies-successful.xml +29 -29
- data/spec/spec_helper.rb +14 -14
- metadata +39 -40
- data/Gemfile.lock +0 -129
data/lib/creditsafe.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "creditsafe/errors"
|
4
|
-
require "creditsafe/client"
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "creditsafe/errors"
|
4
|
+
require "creditsafe/client"
|
data/lib/creditsafe/client.rb
CHANGED
@@ -1,165 +1,165 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "securerandom"
|
4
|
-
require "savon"
|
5
|
-
require "excon"
|
6
|
-
|
7
|
-
require "creditsafe/errors"
|
8
|
-
require "creditsafe/messages"
|
9
|
-
require "creditsafe/namespace"
|
10
|
-
|
11
|
-
require "creditsafe/request/company_report"
|
12
|
-
require "creditsafe/request/find_company"
|
13
|
-
|
14
|
-
require "active_support/notifications"
|
15
|
-
|
16
|
-
module Creditsafe
|
17
|
-
class Client
|
18
|
-
ENVIRONMENTS = %i[live test].freeze
|
19
|
-
|
20
|
-
def initialize(username: nil, password: nil, savon_opts: {},
|
21
|
-
environment: :live, log_level: :warn)
|
22
|
-
raise ArgumentError, "Username must be provided" if username.nil?
|
23
|
-
raise ArgumentError, "Password must be provided" if password.nil?
|
24
|
-
|
25
|
-
unless ENVIRONMENTS.include?(environment.to_sym)
|
26
|
-
raise ArgumentError, "Environment needs to be one of #{ENVIRONMENTS.join('/')}"
|
27
|
-
end
|
28
|
-
|
29
|
-
@environment = environment.to_s
|
30
|
-
@log_level = log_level
|
31
|
-
@username = username
|
32
|
-
@password = password
|
33
|
-
@savon_opts = savon_opts
|
34
|
-
end
|
35
|
-
|
36
|
-
def find_company(search_criteria = {})
|
37
|
-
request = Creditsafe::Request::FindCompany.new(search_criteria)
|
38
|
-
response = invoke_soap(:find_companies, request.message)
|
39
|
-
|
40
|
-
companies = response.
|
41
|
-
fetch(:find_companies_response).
|
42
|
-
fetch(:find_companies_result).
|
43
|
-
fetch(:companies)
|
44
|
-
|
45
|
-
companies.nil? ? nil : companies.fetch(:company)
|
46
|
-
end
|
47
|
-
|
48
|
-
def company_report(creditsafe_id, custom_data: nil)
|
49
|
-
request =
|
50
|
-
Creditsafe::Request::CompanyReport.new(creditsafe_id, custom_data)
|
51
|
-
response = invoke_soap(:retrieve_company_online_report, request.message)
|
52
|
-
|
53
|
-
response.
|
54
|
-
fetch(:retrieve_company_online_report_response).
|
55
|
-
fetch(:retrieve_company_online_report_result).
|
56
|
-
fetch(:reports).
|
57
|
-
fetch(:report)
|
58
|
-
end
|
59
|
-
|
60
|
-
def inspect
|
61
|
-
"#<#{self.class} @username='#{@username}'>"
|
62
|
-
end
|
63
|
-
|
64
|
-
private
|
65
|
-
|
66
|
-
def handle_message_for_response(response)
|
67
|
-
[
|
68
|
-
*response.xpath("//q1:Message"),
|
69
|
-
*response.xpath("//xmlns:Message"),
|
70
|
-
].each do |message|
|
71
|
-
api_message = Creditsafe::Messages.for_code(message.attributes["Code"].value)
|
72
|
-
|
73
|
-
api_error_message = api_message.message
|
74
|
-
api_error_message += " (#{message.text})" unless message.text.blank?
|
75
|
-
|
76
|
-
raise api_message.error_class, api_error_message if api_message.error?
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# rubocop:disable Style/RescueStandardError
|
81
|
-
# rubocop:disable Metrics/MethodLength
|
82
|
-
# rubocop:disable Metrics/AbcSize
|
83
|
-
def invoke_soap(message_type, message)
|
84
|
-
started = Time.now
|
85
|
-
notification_payload = { request: message }
|
86
|
-
|
87
|
-
response = client.call(message_type, message: message)
|
88
|
-
handle_message_for_response(response)
|
89
|
-
notification_payload[:response] = response.body
|
90
|
-
rescue Excon::Errors::Timeout => raw_error
|
91
|
-
notification_payload[:error] = handle_error(raw_error)
|
92
|
-
raise TimeoutError
|
93
|
-
rescue Excon::Errors::BadGateway => raw_error
|
94
|
-
notification_payload[:error] = handle_error(raw_error)
|
95
|
-
raise BadGatewayError
|
96
|
-
rescue => raw_error
|
97
|
-
processed_error = handle_error(raw_error)
|
98
|
-
notification_payload[:error] = processed_error
|
99
|
-
raise processed_error
|
100
|
-
ensure
|
101
|
-
publish("creditsafe.#{message_type}", started, Time.now,
|
102
|
-
SecureRandom.hex(10), notification_payload)
|
103
|
-
end
|
104
|
-
# rubocop:enable Metrics/AbcSize
|
105
|
-
# rubocop:enable Metrics/MethodLength
|
106
|
-
# rubocop:enable Style/RescueStandardError
|
107
|
-
|
108
|
-
def publish(*args)
|
109
|
-
ActiveSupport::Notifications.publish(*args)
|
110
|
-
end
|
111
|
-
|
112
|
-
# There's a potential bug in the creditsafe API where they actually return
|
113
|
-
# an HTTP 401 if you're unauthorized, hence the sad special case below
|
114
|
-
#
|
115
|
-
# rubocop:disable Metrics/MethodLength
|
116
|
-
def handle_error(error)
|
117
|
-
case error
|
118
|
-
when Savon::SOAPFault
|
119
|
-
return UnknownApiError.new(error.message)
|
120
|
-
when Savon::HTTPError
|
121
|
-
if error.to_hash[:code] == 401
|
122
|
-
return AccountError.new("Unauthorized: invalid credentials")
|
123
|
-
end
|
124
|
-
|
125
|
-
return UnknownApiError.new(error.message)
|
126
|
-
when Excon::Errors::Error
|
127
|
-
return HttpError.new("Error making HTTP request: #{error.message}")
|
128
|
-
end
|
129
|
-
error
|
130
|
-
end
|
131
|
-
# rubocop:enable Metrics/MethodLength
|
132
|
-
|
133
|
-
def client
|
134
|
-
@client ||= build_savon_client
|
135
|
-
end
|
136
|
-
|
137
|
-
def auth_header
|
138
|
-
auth_value = "Basic " + Base64.encode64("#{@username}:#{@password}").chomp
|
139
|
-
{ "Authorization" => auth_value }
|
140
|
-
end
|
141
|
-
|
142
|
-
# rubocop:disable Metrics/MethodLength
|
143
|
-
def build_savon_client
|
144
|
-
options = {
|
145
|
-
env_namespace: "soapenv",
|
146
|
-
namespace_identifier: Creditsafe::Namespace::OPER,
|
147
|
-
namespaces: Creditsafe::Namespace::ALL,
|
148
|
-
wsdl: wsdl_path,
|
149
|
-
headers: auth_header,
|
150
|
-
convert_request_keys_to: :none,
|
151
|
-
adapter: :excon,
|
152
|
-
log: true,
|
153
|
-
log_level: @log_level,
|
154
|
-
pretty_print_xml: true,
|
155
|
-
}
|
156
|
-
Savon.client(options.merge(@savon_opts))
|
157
|
-
end
|
158
|
-
# rubocop:enable Metrics/MethodLength
|
159
|
-
|
160
|
-
def wsdl_path
|
161
|
-
root_dir = File.join(File.dirname(__FILE__), "..", "..")
|
162
|
-
File.join(root_dir, "data", "creditsafe-#{@environment}.xml")
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "securerandom"
|
4
|
+
require "savon"
|
5
|
+
require "excon"
|
6
|
+
|
7
|
+
require "creditsafe/errors"
|
8
|
+
require "creditsafe/messages"
|
9
|
+
require "creditsafe/namespace"
|
10
|
+
|
11
|
+
require "creditsafe/request/company_report"
|
12
|
+
require "creditsafe/request/find_company"
|
13
|
+
|
14
|
+
require "active_support/notifications"
|
15
|
+
|
16
|
+
module Creditsafe
|
17
|
+
class Client
|
18
|
+
ENVIRONMENTS = %i[live test].freeze
|
19
|
+
|
20
|
+
def initialize(username: nil, password: nil, savon_opts: {},
|
21
|
+
environment: :live, log_level: :warn)
|
22
|
+
raise ArgumentError, "Username must be provided" if username.nil?
|
23
|
+
raise ArgumentError, "Password must be provided" if password.nil?
|
24
|
+
|
25
|
+
unless ENVIRONMENTS.include?(environment.to_sym)
|
26
|
+
raise ArgumentError, "Environment needs to be one of #{ENVIRONMENTS.join('/')}"
|
27
|
+
end
|
28
|
+
|
29
|
+
@environment = environment.to_s
|
30
|
+
@log_level = log_level
|
31
|
+
@username = username
|
32
|
+
@password = password
|
33
|
+
@savon_opts = savon_opts
|
34
|
+
end
|
35
|
+
|
36
|
+
def find_company(search_criteria = {})
|
37
|
+
request = Creditsafe::Request::FindCompany.new(search_criteria)
|
38
|
+
response = invoke_soap(:find_companies, request.message)
|
39
|
+
|
40
|
+
companies = response.
|
41
|
+
fetch(:find_companies_response).
|
42
|
+
fetch(:find_companies_result).
|
43
|
+
fetch(:companies)
|
44
|
+
|
45
|
+
companies.nil? ? nil : companies.fetch(:company)
|
46
|
+
end
|
47
|
+
|
48
|
+
def company_report(creditsafe_id, custom_data: nil)
|
49
|
+
request =
|
50
|
+
Creditsafe::Request::CompanyReport.new(creditsafe_id, custom_data)
|
51
|
+
response = invoke_soap(:retrieve_company_online_report, request.message)
|
52
|
+
|
53
|
+
response.
|
54
|
+
fetch(:retrieve_company_online_report_response).
|
55
|
+
fetch(:retrieve_company_online_report_result).
|
56
|
+
fetch(:reports).
|
57
|
+
fetch(:report)
|
58
|
+
end
|
59
|
+
|
60
|
+
def inspect
|
61
|
+
"#<#{self.class} @username='#{@username}'>"
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def handle_message_for_response(response)
|
67
|
+
[
|
68
|
+
*response.xpath("//q1:Message"),
|
69
|
+
*response.xpath("//xmlns:Message"),
|
70
|
+
].each do |message|
|
71
|
+
api_message = Creditsafe::Messages.for_code(message.attributes["Code"].value)
|
72
|
+
|
73
|
+
api_error_message = api_message.message
|
74
|
+
api_error_message += " (#{message.text})" unless message.text.blank?
|
75
|
+
|
76
|
+
raise api_message.error_class, api_error_message if api_message.error?
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# rubocop:disable Style/RescueStandardError
|
81
|
+
# rubocop:disable Metrics/MethodLength
|
82
|
+
# rubocop:disable Metrics/AbcSize
|
83
|
+
def invoke_soap(message_type, message)
|
84
|
+
started = Time.now
|
85
|
+
notification_payload = { request: message }
|
86
|
+
|
87
|
+
response = client.call(message_type, message: message)
|
88
|
+
handle_message_for_response(response)
|
89
|
+
notification_payload[:response] = response.body
|
90
|
+
rescue Excon::Errors::Timeout => raw_error
|
91
|
+
notification_payload[:error] = handle_error(raw_error)
|
92
|
+
raise TimeoutError
|
93
|
+
rescue Excon::Errors::BadGateway => raw_error
|
94
|
+
notification_payload[:error] = handle_error(raw_error)
|
95
|
+
raise BadGatewayError
|
96
|
+
rescue => raw_error
|
97
|
+
processed_error = handle_error(raw_error)
|
98
|
+
notification_payload[:error] = processed_error
|
99
|
+
raise processed_error
|
100
|
+
ensure
|
101
|
+
publish("creditsafe.#{message_type}", started, Time.now,
|
102
|
+
SecureRandom.hex(10), notification_payload)
|
103
|
+
end
|
104
|
+
# rubocop:enable Metrics/AbcSize
|
105
|
+
# rubocop:enable Metrics/MethodLength
|
106
|
+
# rubocop:enable Style/RescueStandardError
|
107
|
+
|
108
|
+
def publish(*args)
|
109
|
+
ActiveSupport::Notifications.publish(*args)
|
110
|
+
end
|
111
|
+
|
112
|
+
# There's a potential bug in the creditsafe API where they actually return
|
113
|
+
# an HTTP 401 if you're unauthorized, hence the sad special case below
|
114
|
+
#
|
115
|
+
# rubocop:disable Metrics/MethodLength
|
116
|
+
def handle_error(error)
|
117
|
+
case error
|
118
|
+
when Savon::SOAPFault
|
119
|
+
return UnknownApiError.new(error.message)
|
120
|
+
when Savon::HTTPError
|
121
|
+
if error.to_hash[:code] == 401
|
122
|
+
return AccountError.new("Unauthorized: invalid credentials")
|
123
|
+
end
|
124
|
+
|
125
|
+
return UnknownApiError.new(error.message)
|
126
|
+
when Excon::Errors::Error
|
127
|
+
return HttpError.new("Error making HTTP request: #{error.message}")
|
128
|
+
end
|
129
|
+
error
|
130
|
+
end
|
131
|
+
# rubocop:enable Metrics/MethodLength
|
132
|
+
|
133
|
+
def client
|
134
|
+
@client ||= build_savon_client
|
135
|
+
end
|
136
|
+
|
137
|
+
def auth_header
|
138
|
+
auth_value = "Basic " + Base64.encode64("#{@username}:#{@password}").chomp
|
139
|
+
{ "Authorization" => auth_value }
|
140
|
+
end
|
141
|
+
|
142
|
+
# rubocop:disable Metrics/MethodLength
|
143
|
+
def build_savon_client
|
144
|
+
options = {
|
145
|
+
env_namespace: "soapenv",
|
146
|
+
namespace_identifier: Creditsafe::Namespace::OPER,
|
147
|
+
namespaces: Creditsafe::Namespace::ALL,
|
148
|
+
wsdl: wsdl_path,
|
149
|
+
headers: auth_header,
|
150
|
+
convert_request_keys_to: :none,
|
151
|
+
adapter: :excon,
|
152
|
+
log: true,
|
153
|
+
log_level: @log_level,
|
154
|
+
pretty_print_xml: true,
|
155
|
+
}
|
156
|
+
Savon.client(options.merge(@savon_opts))
|
157
|
+
end
|
158
|
+
# rubocop:enable Metrics/MethodLength
|
159
|
+
|
160
|
+
def wsdl_path
|
161
|
+
root_dir = File.join(File.dirname(__FILE__), "..", "..")
|
162
|
+
File.join(root_dir, "data", "creditsafe-#{@environment}.xml")
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
data/lib/creditsafe/constants.rb
CHANGED
@@ -1,49 +1,49 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Constants
|
4
|
-
module Country
|
5
|
-
AUSTRALIA = "AU"
|
6
|
-
AUSTRIA = "AT"
|
7
|
-
BELGIUM = "BE"
|
8
|
-
BULGARIA = "BG"
|
9
|
-
CANADA = "CA"
|
10
|
-
CROATIA = "HR"
|
11
|
-
CZECH_REPUBLIC = "CZ"
|
12
|
-
DENMARK = "DK"
|
13
|
-
ESTONIA = "EE"
|
14
|
-
FINLAND = "FI"
|
15
|
-
FRANCE = "FR"
|
16
|
-
GERMANY = "DE"
|
17
|
-
GREAT_BRITAIN = "GB"
|
18
|
-
GREECE = "GR"
|
19
|
-
HUNGARY = "HU"
|
20
|
-
ICELAND = "IS"
|
21
|
-
IRELAND = "IE"
|
22
|
-
ITALY = "IT"
|
23
|
-
LATVIA = "LV"
|
24
|
-
LIECHTENSTEIN = "LI"
|
25
|
-
LITHUANIA = "LT"
|
26
|
-
LUXEMBOURG = "LU"
|
27
|
-
MALTA = "MT"
|
28
|
-
MOLDOVA = "MD"
|
29
|
-
NETHERLANDS = "NL"
|
30
|
-
NEW_ZEALAND = "NZ"
|
31
|
-
NORWAY = "NO"
|
32
|
-
POLAND = "PL"
|
33
|
-
PORTUGAL = "PT"
|
34
|
-
ROMANIA = "RO"
|
35
|
-
SLOVAKIA = "SK"
|
36
|
-
SLOVENIA = "SI"
|
37
|
-
SPAIN = "ES"
|
38
|
-
SWEDEN = "SE"
|
39
|
-
SWITZERLAND = "CH"
|
40
|
-
UK = "GB"
|
41
|
-
UNITED_STATES = "US"
|
42
|
-
|
43
|
-
VAT_NUMBER_SUPPORTED = [
|
44
|
-
GERMANY, FRANCE, CZECH_REPUBLIC, SLOVAKIA, BELGIUM, POLAND, PORTUGAL, SPAIN,
|
45
|
-
UNITED_STATES, SLOVENIA, CROATIA, BULGARIA, ROMANIA, LATVIA, ESTONIA, MOLDOVA,
|
46
|
-
AUSTRIA, ITALY, HUNGARY, FINLAND, DENMARK, AUSTRALIA, GREECE
|
47
|
-
].freeze
|
48
|
-
end
|
49
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Constants
|
4
|
+
module Country
|
5
|
+
AUSTRALIA = "AU"
|
6
|
+
AUSTRIA = "AT"
|
7
|
+
BELGIUM = "BE"
|
8
|
+
BULGARIA = "BG"
|
9
|
+
CANADA = "CA"
|
10
|
+
CROATIA = "HR"
|
11
|
+
CZECH_REPUBLIC = "CZ"
|
12
|
+
DENMARK = "DK"
|
13
|
+
ESTONIA = "EE"
|
14
|
+
FINLAND = "FI"
|
15
|
+
FRANCE = "FR"
|
16
|
+
GERMANY = "DE"
|
17
|
+
GREAT_BRITAIN = "GB"
|
18
|
+
GREECE = "GR"
|
19
|
+
HUNGARY = "HU"
|
20
|
+
ICELAND = "IS"
|
21
|
+
IRELAND = "IE"
|
22
|
+
ITALY = "IT"
|
23
|
+
LATVIA = "LV"
|
24
|
+
LIECHTENSTEIN = "LI"
|
25
|
+
LITHUANIA = "LT"
|
26
|
+
LUXEMBOURG = "LU"
|
27
|
+
MALTA = "MT"
|
28
|
+
MOLDOVA = "MD"
|
29
|
+
NETHERLANDS = "NL"
|
30
|
+
NEW_ZEALAND = "NZ"
|
31
|
+
NORWAY = "NO"
|
32
|
+
POLAND = "PL"
|
33
|
+
PORTUGAL = "PT"
|
34
|
+
ROMANIA = "RO"
|
35
|
+
SLOVAKIA = "SK"
|
36
|
+
SLOVENIA = "SI"
|
37
|
+
SPAIN = "ES"
|
38
|
+
SWEDEN = "SE"
|
39
|
+
SWITZERLAND = "CH"
|
40
|
+
UK = "GB"
|
41
|
+
UNITED_STATES = "US"
|
42
|
+
|
43
|
+
VAT_NUMBER_SUPPORTED = [
|
44
|
+
GERMANY, FRANCE, CZECH_REPUBLIC, SLOVAKIA, BELGIUM, POLAND, PORTUGAL, SPAIN,
|
45
|
+
UNITED_STATES, SLOVENIA, CROATIA, BULGARIA, ROMANIA, LATVIA, ESTONIA, MOLDOVA,
|
46
|
+
AUSTRIA, ITALY, HUNGARY, FINLAND, DENMARK, AUSTRALIA, GREECE
|
47
|
+
].freeze
|
48
|
+
end
|
49
|
+
end
|
data/lib/creditsafe/errors.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Creditsafe
|
4
|
-
class Error < StandardError; end
|
5
|
-
|
6
|
-
class HttpError < Error; end
|
7
|
-
class ApiError < Error; end
|
8
|
-
|
9
|
-
class TimeoutError < HttpError; end
|
10
|
-
class BadGatewayError < HttpError; end
|
11
|
-
|
12
|
-
class DataError < ApiError; end
|
13
|
-
class AccountError < ApiError; end
|
14
|
-
class RequestError < ApiError; end
|
15
|
-
class ProcessingError < ApiError; end
|
16
|
-
class UnknownApiError < ApiError; end
|
17
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Creditsafe
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
class HttpError < Error; end
|
7
|
+
class ApiError < Error; end
|
8
|
+
|
9
|
+
class TimeoutError < HttpError; end
|
10
|
+
class BadGatewayError < HttpError; end
|
11
|
+
|
12
|
+
class DataError < ApiError; end
|
13
|
+
class AccountError < ApiError; end
|
14
|
+
class RequestError < ApiError; end
|
15
|
+
class ProcessingError < ApiError; end
|
16
|
+
class UnknownApiError < ApiError; end
|
17
|
+
end
|