Avatax_AddressService 1.0.12 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,200 +1,188 @@
1
- require 'savon'
2
- require 'erb'
3
-
4
- module AvaTax
5
- #Avalara address class
6
- class AddressService
7
- def initialize(credentials)
8
-
9
- #Set @def_locn to the Avatax-x.x.x gem install library. This enables the ruby programs to
10
- #find other objects that it needs.
11
- spec = Gem::Specification.find_by_name("Avatax_AddressService")
12
- gem_root = spec.gem_dir
13
- @def_locn = gem_root + "/lib"
14
-
15
- #Extract data from hash
16
- username = credentials[:username]
17
- password = credentials[:password]
18
- if username.nil? and password.nil?
19
- raise ArgumentError, "username and password are required"
20
- end
21
- name = credentials[:name]
22
- clientname = credentials[:clientname]
23
- adapter = credentials[:adapter]
24
- machine = credentials[:machine]
25
- use_production_account = credentials[:use_production_account]
26
-
27
- #Set credentials and Profile information
28
- @username = username == nil ? "" : username
29
- @password = password == nil ? "" : password
30
- @name = name == nil ? "" : name
31
- @clientname = (clientname == nil or clientname == "") ? "AvataxRubySDK" : clientname
32
- @adapter = (adapter == nil or adapter == "") ? spec.summary + spec.version.to_s : adapter
33
- @machine = machine == nil ? "" : machine
34
- @use_production_account = (use_production_account != true) ? false : use_production_account
35
-
36
- #Open Avatax Error Log
37
- @log = File.new(@def_locn + '/address_log.txt', "w")
38
-
39
- #Get service details from WSDL - control_array[2] contains the WSDL read from the address_control file
40
- #log :false turns off HTTP logging. Select either Dev or Prod depending on the value of the boolean value 'use_production_account'
41
- if @use_production_account
42
- @log.puts "#{Time.now}: Avalara Production Address service started"
43
- #log :false turns off HTTP logging
44
- @client = Savon.client(wsdl: @def_locn + '/addressservice_prd.wsdl', log: false)
45
- else
46
- @log.puts "#{Time.now}: Avalara Development Address service started"
47
- #log :false turns off HTTP logging
48
- @client = Savon.client(wsdl: @def_locn + '/addressservice_dev.wsdl', log: false)
49
- end
50
-
51
- begin
52
- #Read in the SOAP template for Ping
53
- @template_ping = ERB.new(File.read(@def_locn + '/template_ping.erb'))
54
- rescue
55
- @log.puts "#{Time.now}: Error loading the Ping template"
56
- end
57
-
58
- begin
59
- #Read in the SOAP template for Validate
60
- @template_validate = ERB.new(File.read(@def_locn + '/template_validate.erb'))
61
- rescue
62
- @log.puts "#{Time.now}: Error loading the Validate template"
63
- end
64
-
65
- begin
66
- #Read in the SOAP template for IsAuthorized
67
- @template_isauthorized = ERB.new(File.read(@def_locn + '/template_isauthorized.erb'))
68
- rescue
69
- @log.puts "#{Time.now}: Error loading the IsAuthorized template"
70
- end
71
-
72
- # Create hash for result
73
- @response = Hash.new
74
- end
75
-
76
- ############################################################################################################
77
- # ping - Verifies connectivity to the web service and returns version information
78
- ############################################################################################################
79
- def ping(message = nil)
80
-
81
- @service = 'Ping'
82
-
83
- #Read in the SOAP template
84
- @message = message == nil ? "?" : message
85
-
86
- # Subsitute real vales for template place holders
87
- @soap = @template_ping.result(binding)
88
-
89
- # Make the call to the Avalara Ping service
90
- begin
91
- @response = @client.call(:ping, xml: @soap).to_hash
92
-
93
- #Strip off outer layer of the hash - not needed
94
- return messages_to_array(@response[:ping_response][:ping_result])
95
-
96
-
97
- #Capture unexpected errors
98
- rescue Savon::Error => error
99
- abend(error)
100
- end
101
- end
102
-
103
- ############################################################################################################
104
- # validate - call the adddress validation service
105
- ############################################################################################################
106
- def validate(address)
107
-
108
- @service = 'Validate'
109
-
110
- #create instance variables for each entry in input
111
- address.each do |k,v|
112
- instance_variable_set("@#{k}",v)
113
- end
114
- #set required default values for missing required inputs
115
- @taxregionid ||= "0"
116
- @textcase ||= "Default"
117
- @coordinates ||= false
118
- @taxability ||= false
119
-
120
- # Subsitute real values for template place holders
121
- @soap = @template_validate.result(binding)
122
-
123
- # Make the call to the Avalara Validate service
124
- begin
125
- @response = @client.call(:validate, xml: @soap).to_hash
126
-
127
- #Strip off outer layer of the hash - not needed
128
- return messages_to_array(@response[:validate_response][:validate_result])
129
-
130
-
131
- #Capture unexpected errors
132
- rescue Savon::Error => error
133
- abend(error)
134
- end
135
- end
136
-
137
- ############################################################################################################
138
- #Verifies connectivity to the web service and returns version information about the service.
139
- ############################################################################################################
140
- def isauthorized(operation = nil)
141
-
142
- @service = 'IsAuthorized'
143
-
144
- #Read in the SOAP template
145
- @operation = operation == nil ? "?" : operation
146
-
147
- # Subsitute real vales for template place holders
148
- @soap = @template_isauthorized.result(binding)
149
-
150
- # Make the call to the Avalara Ping service
151
- begin
152
- @response = @client.call(:is_authorized, xml: @soap).to_hash
153
-
154
- #Strip off outer layer of the hash - not needed
155
- return messages_to_array(@response[:is_authorized_response][:is_authorized_result])
156
-
157
- #Capture unexpected errors
158
- rescue Savon::Error => error
159
- abend(error)
160
- end
161
- end
162
-
163
- private
164
- ############################################################################################################
165
- # abend - Unexpected error handling
166
- ############################################################################################################
167
- def abend(error)
168
- @log.puts "An unexpected error occurred: Response from server = #{error}"
169
- @log.puts "#{Time.now}: Error calling #{@service} service ... check that your account name and password are correct."
170
- @response = error.to_hash
171
- @response[:result_code] = 'Error'
172
- @response[:summary] = @response[:fault][:faultcode]
173
- @response[:details] = @response[:fault][:faultstring]
174
- return messages_to_array(@response)
175
- end
176
-
177
- ############################################################################################################
178
- #standardizes error message format to an array of messages - nokogiri will collapse a single element array into the response hash.
179
- ############################################################################################################
180
- def messages_to_array(response)
181
- if not response[:messages].nil?
182
- return response
183
- end
184
- # add the messages array to the response - if we got here, there was only one error.
185
- response[:messages] = [{
186
- :summary => response[:summary],
187
- :details => response[:details],
188
- :helplink => response[:helplink],
189
- :refersto => response[:refersto],
190
- :severity => response[:severity],
191
- :source => response[:source]
192
- }]
193
- #remove all the error information from the hash
194
- response[:messages][0].each do |k,v|
195
- response.delete(k)
196
- end
197
- return response
198
- end
199
- end
1
+ require 'savon'
2
+ require 'erb'
3
+
4
+ module AvaTax
5
+ #Avalara address class
6
+ class AddressService
7
+ def initialize(credentials)
8
+
9
+ #Set @def_locn to the Avatax-x.x.x gem install library. This enables the ruby programs to
10
+ #find other objects that it needs.
11
+ spec = Gem::Specification.find_by_name("Avatax_AddressService")
12
+ gem_root = spec.gem_dir
13
+ @def_locn = gem_root + "/lib"
14
+
15
+ #Extract data from hash
16
+ username = credentials[:username] || ""
17
+ password = credentials[:password] || ""
18
+ service_url = credentials[:service_url]
19
+ if service_url.nil? or service_url.empty?
20
+ raise ArgumentError, "service_url is required"
21
+ end
22
+ name = credentials[:name] || ""
23
+ clientname = (credentials[:clientname].nil? or credentials[:clientname].empty? ) ? "AvataxRubySDK" : credentials[:clientname]
24
+ adapter = (credentials[:adapter].nil? or credentials[:adapter].empty?) ? spec.summary + spec.version.to_s : credentials[:adapter]
25
+ machine = credentials[:machine] || ""
26
+
27
+
28
+ #Set credentials and Profile information
29
+ @username = username
30
+ @password = password
31
+ @name = name
32
+ @clientname = clientname
33
+ @adapter = adapter
34
+ @machine = machine
35
+
36
+ #Open Avatax Error Log
37
+ @log = File.new(@def_locn + '/address_log.txt', "w")
38
+
39
+ #Get service details from WSDL - control_array[2] contains the WSDL read from the address_control file
40
+ #log :false turns off HTTP logging. Select either Dev or Prod depending on the value of the boolean value 'use_production_account'
41
+ @log.puts "#{Time.now}: Avalara Address service started"
42
+ #log :false turns off HTTP logging
43
+ @client = Savon.client(wsdl: @def_locn + '/addressservice.wsdl', endpoint: URI.parse(service_url+"/Address/AddressSvc.asmx"), log: false)
44
+
45
+ begin
46
+ #Read in the SOAP template for Ping
47
+ @template_ping = ERB.new(File.read(@def_locn + '/template_ping.erb'))
48
+ rescue
49
+ @log.puts "#{Time.now}: Error loading the Ping template"
50
+ end
51
+
52
+ begin
53
+ #Read in the SOAP template for Validate
54
+ @template_validate = ERB.new(File.read(@def_locn + '/template_validate.erb'))
55
+ rescue
56
+ @log.puts "#{Time.now}: Error loading the Validate template"
57
+ end
58
+
59
+ begin
60
+ #Read in the SOAP template for IsAuthorized
61
+ @template_isauthorized = ERB.new(File.read(@def_locn + '/template_isauthorized.erb'))
62
+ rescue
63
+ @log.puts "#{Time.now}: Error loading the IsAuthorized template"
64
+ end
65
+
66
+ # Create hash for result
67
+ @response = Hash.new
68
+ end
69
+
70
+ ############################################################################################################
71
+ # ping - Verifies connectivity to the web service and returns version information
72
+ ############################################################################################################
73
+ def ping(message = nil)
74
+
75
+ #Read in the SOAP template
76
+ @message = message == nil ? "?" : message
77
+
78
+ # Subsitute real vales for template place holders
79
+ @soap = @template_ping.result(binding)
80
+
81
+ # Make the call to the Avalara Ping service
82
+ begin
83
+ @response = @client.call(:ping, xml: @soap).to_hash
84
+
85
+ #Strip off outer layer of the hash - not needed
86
+ return messages_to_array(@response[:ping_response][:ping_result])
87
+
88
+
89
+ #Capture unexpected errors
90
+ rescue Savon::Error => error
91
+ abend(error)
92
+ end
93
+ end
94
+
95
+ ############################################################################################################
96
+ # validate - call the adddress validation service
97
+ ############################################################################################################
98
+ def validate(address)
99
+
100
+ #create instance variables for each entry in input
101
+ address.each do |k,v|
102
+ instance_variable_set("@#{k}",v)
103
+ end
104
+ #set required default values for missing required inputs
105
+ @taxregionid ||= "0"
106
+ @textcase ||= "Default"
107
+ @coordinates ||= false
108
+ @taxability ||= false
109
+
110
+ # Subsitute real values for template place holders
111
+ @soap = @template_validate.result(binding)
112
+
113
+ # Make the call to the Avalara Validate service
114
+ begin
115
+ @response = @client.call(:validate, xml: @soap).to_hash
116
+
117
+ #Strip off outer layer of the hash - not needed
118
+ return messages_to_array(@response[:validate_response][:validate_result])
119
+
120
+
121
+ #Capture unexpected errors
122
+ rescue Savon::Error => error
123
+ abend(error)
124
+ end
125
+ end
126
+
127
+ ############################################################################################################
128
+ #Verifies connectivity to the web service and returns version information about the service.
129
+ ############################################################################################################
130
+ def isauthorized(operation = nil)
131
+
132
+ #Read in the SOAP template
133
+ @operation = operation == nil ? "?" : operation
134
+
135
+ # Subsitute real vales for template place holders
136
+ @soap = @template_isauthorized.result(binding)
137
+
138
+ # Make the call to the Avalara Ping service
139
+ begin
140
+ @response = @client.call(:is_authorized, xml: @soap).to_hash
141
+
142
+ #Strip off outer layer of the hash - not needed
143
+ return messages_to_array(@response[:is_authorized_response][:is_authorized_result])
144
+
145
+ #Capture unexpected errors
146
+ rescue Savon::Error => error
147
+ abend(error)
148
+ end
149
+ end
150
+
151
+ private
152
+ ############################################################################################################
153
+ # abend - Unexpected error handling
154
+ ############################################################################################################
155
+ def abend(error)
156
+ @log.puts "An unexpected error occurred: Response from server = #{error}"
157
+ @log.puts "#{Time.now}: Error calling #{@service} service ... check that your account name and password are correct."
158
+ @response = error.to_hash
159
+ @response[:result_code] = 'Error'
160
+ @response[:summary] = @response[:fault][:faultcode]
161
+ @response[:details] = @response[:fault][:faultstring]
162
+ return messages_to_array(@response)
163
+ end
164
+
165
+ ############################################################################################################
166
+ #standardizes error message format to an array of messages - nokogiri will collapse a single element array into the response hash.
167
+ ############################################################################################################
168
+ def messages_to_array(response)
169
+ if not response[:messages].nil?
170
+ return response
171
+ end
172
+ # add the messages array to the response - if we got here, there was only one error.
173
+ response[:messages] = [{
174
+ :summary => response[:summary],
175
+ :details => response[:details],
176
+ :helplink => response[:helplink],
177
+ :refersto => response[:refersto],
178
+ :severity => response[:severity],
179
+ :source => response[:source]
180
+ }]
181
+ #remove all the error information from the hash
182
+ response[:messages][0].each do |k,v|
183
+ response.delete(k)
184
+ end
185
+ return response
186
+ end
187
+ end
200
188
  end
@@ -1,22 +1,22 @@
1
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://avatax.avalara.com/services">
2
- <soapenv:Header>
3
- <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
4
- <wsse:UsernameToken wsu:Id="UsernameToken-7" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
5
- <wsse:Username><%= @username %></wsse:Username>
6
- <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"><%= @password %></wsse:Password>
7
- <wsu:Created>2012-03-02T23:41:44.511Z</wsu:Created>
8
- </wsse:UsernameToken>
9
- </wsse:Security>
10
- <ser:Profile>
11
- <ser:Name><%= @name %></ser:Name>
12
- <ser:Client><%= @clientname %></ser:Client>
13
- <ser:Adapter><%= @adapter %></ser:Adapter>
14
- <ser:Machine><%= @machine %></ser:Machine>
15
- </ser:Profile>
16
- </soapenv:Header>
17
- <soapenv:Body>
18
- <ser:IsAuthorized>
19
- <ser:Operations><%= @operation %></ser:Operations>
20
- </ser:IsAuthorized>
21
- </soapenv:Body>
1
+ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://avatax.avalara.com/services">
2
+ <soapenv:Header>
3
+ <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
4
+ <wsse:UsernameToken wsu:Id="UsernameToken-7" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
5
+ <wsse:Username><%= @username %></wsse:Username>
6
+ <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"><%= @password %></wsse:Password>
7
+ <wsu:Created>2012-03-02T23:41:44.511Z</wsu:Created>
8
+ </wsse:UsernameToken>
9
+ </wsse:Security>
10
+ <ser:Profile>
11
+ <ser:Name><%= @name %></ser:Name>
12
+ <ser:Client><%= @clientname %></ser:Client>
13
+ <ser:Adapter><%= @adapter %></ser:Adapter>
14
+ <ser:Machine><%= @machine %></ser:Machine>
15
+ </ser:Profile>
16
+ </soapenv:Header>
17
+ <soapenv:Body>
18
+ <ser:IsAuthorized>
19
+ <ser:Operations><%= @operation %></ser:Operations>
20
+ </ser:IsAuthorized>
21
+ </soapenv:Body>
22
22
  </soapenv:Envelope>
@@ -1,22 +1,22 @@
1
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://avatax.avalara.com/services">
2
- <soapenv:Header>
3
- <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
4
- <wsse:UsernameToken wsu:Id="UsernameToken-7" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
5
- <wsse:Username><%= @username %></wsse:Username>
6
- <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"><%= @password %></wsse:Password>
7
- <wsu:Created>2012-03-02T23:41:44.511Z</wsu:Created>
8
- </wsse:UsernameToken>
9
- </wsse:Security>
10
- <ser:Profile>
11
- <ser:Name><%= @name %></ser:Name>
12
- <ser:Client><%= @clientname %></ser:Client>
13
- <ser:Adapter><%= @adapter %></ser:Adapter>
14
- <ser:Machine><%= @machine %></ser:Machine>
15
- </ser:Profile>
16
- </soapenv:Header>
17
- <soapenv:Body>
18
- <ser:Ping>
19
- <ser:Message><%= @ping_message %></ser:Message>
20
- </ser:Ping>
21
- </soapenv:Body>
1
+ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://avatax.avalara.com/services">
2
+ <soapenv:Header>
3
+ <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
4
+ <wsse:UsernameToken wsu:Id="UsernameToken-7" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
5
+ <wsse:Username><%= @username %></wsse:Username>
6
+ <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"><%= @password %></wsse:Password>
7
+ <wsu:Created>2012-03-02T23:41:44.511Z</wsu:Created>
8
+ </wsse:UsernameToken>
9
+ </wsse:Security>
10
+ <ser:Profile>
11
+ <ser:Name><%= @name %></ser:Name>
12
+ <ser:Client><%= @clientname %></ser:Client>
13
+ <ser:Adapter><%= @adapter %></ser:Adapter>
14
+ <ser:Machine><%= @machine %></ser:Machine>
15
+ </ser:Profile>
16
+ </soapenv:Header>
17
+ <soapenv:Body>
18
+ <ser:Ping>
19
+ <ser:Message><%= @ping_message %></ser:Message>
20
+ </ser:Ping>
21
+ </soapenv:Body>
22
22
  </soapenv:Envelope>