Avatax_AddressService 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "Avatax_AddressService"
3
- s.version = "1.0.4"
4
- s.date = "2012-10-28"
3
+ s.version = "1.0.5"
4
+ s.date = "2012-12-16"
5
5
  s.author = "Graham S Wilson"
6
6
  s.email = "support@Avalara.com"
7
7
  s.summary = "Ruby SDK for Avatax Address Web Services"
@@ -3,178 +3,187 @@ require 'erb'
3
3
  require 'nokogiri'
4
4
 
5
5
  module AvaTax
6
-
7
6
  #Avalara address class
8
- class AddressService
9
-
10
- def initialize(username,password,name,clientname,adapter,machine)
11
-
12
- #Set credentials and Profile information
13
- @username = username == nil ? "" : username
14
- @password = password == nil ? "" : password
15
- @name = name == nil ? "" : name
16
- @clientname = clientname == nil ? "" : clientname
17
- @adapter = adapter == nil ? "" : adapter
18
- @machine = machine == nil ? "" : machine
19
-
20
- #Set @def_locn to the Avatax-x.x.x gem install library. This enables the ruby programs to
21
- #find other objects that it needs.
22
- spec = Gem::Specification.find_by_name("Avatax_AddressService")
23
- gem_root = spec.gem_dir
24
- @def_locn = gem_root + "/lib"
25
-
26
- #Open Avatax Error Log
27
- @log = File.new(@def_locn + '\address_log.txt', "w")
28
- @log.puts "#{Time.now}: Address service started"
29
-
30
- #log :false turns off HTTP logging
31
- @client = Savon.client(wsdl: @def_locn + '/addressservice_dev.wsdl', log: false)
32
-
33
- begin
34
- #Read in the SOAP template for Ping
35
- @template_ping = ERB.new(File.read(@def_locn + '\template_ping.erb'))
36
- rescue
37
- @log.puts "#{Time.now}: Error loading the Ping template"
38
- end
39
-
40
- begin
41
- #Read in the SOAP template for Validate
42
- @template_validate = ERB.new(File.read(@def_locn + '\template_validate.erb'))
43
- rescue
44
- @log.puts "#{Time.now}: Error loading the Validate template"
45
- end
46
-
47
- begin
48
- #Read in the SOAP template for IsAuthorized
49
- @template_isauthorized = ERB.new(File.read(@def_locn + '\template_isauthorized.erb'))
50
- rescue
51
- @log.puts "#{Time.now}: Error loading the IsAuthorized template"
52
- end
53
- # Create hash for validate result
54
- @return_data = Hash.new
7
+ class AddressService
8
+ def initialize(credentials)
9
+
10
+ #Extract data from hash
11
+ username = credentials[:username]
12
+ password = credentials[:password]
13
+ name = credentials[:name]
14
+ clientname = credentials[:clientname]
15
+ adapter = credentials[:adapter]
16
+ machine = credentials[:machine]
17
+
18
+ #Set credentials and Profile information
19
+ @username = username == nil ? "" : username
20
+ @password = password == nil ? "" : password
21
+ @name = name == nil ? "" : name
22
+ @clientname = clientname == nil ? "" : clientname
23
+ @adapter = adapter == nil ? "" : adapter
24
+ @machine = machine == nil ? "" : machine
25
+
26
+ #Set @def_locn to the Avatax-x.x.x gem install library. This enables the ruby programs to
27
+ #find other objects that it needs.
28
+ spec = Gem::Specification.find_by_name("Avatax_AddressService")
29
+ gem_root = spec.gem_dir
30
+ @def_locn = gem_root + "/lib"
31
+
32
+ #Open Avatax Error Log
33
+ @log = File.new(@def_locn + '\address_log.txt', "w")
34
+ @log.puts "#{Time.now}: Address service started"
35
+
36
+ #log :false turns off HTTP logging
37
+ @client = Savon.client(wsdl: @def_locn + '/addressservice_dev.wsdl', log: false)
38
+
39
+ begin
40
+ #Read in the SOAP template for Ping
41
+ @template_ping = ERB.new(File.read(@def_locn + '\template_ping.erb'))
42
+ rescue
43
+ @log.puts "#{Time.now}: Error loading the Ping template"
44
+ end
45
+
46
+ begin
47
+ #Read in the SOAP template for Validate
48
+ @template_validate = ERB.new(File.read(@def_locn + '\template_validate.erb'))
49
+ rescue
50
+ @log.puts "#{Time.now}: Error loading the Validate template"
51
+ end
52
+
53
+ begin
54
+ #Read in the SOAP template for IsAuthorized
55
+ @template_isauthorized = ERB.new(File.read(@def_locn + '\template_isauthorized.erb'))
56
+ rescue
57
+ @log.puts "#{Time.now}: Error loading the IsAuthorized template"
58
+ end
59
+ # Create hash for validate result
60
+ @return_data = Hash.new
55
61
  end
56
62
 
57
- ############################################################################################################
58
- # ping - Verifies connectivity to the web service and returns version information
59
- ############################################################################################################
63
+ ############################################################################################################
64
+ # ping - Verifies connectivity to the web service and returns version information
65
+ ############################################################################################################
60
66
  def ping(message = nil)
61
67
  #Read in the SOAP template
62
- @message = message == nil ? "?" : message
68
+ @message = message == nil ? "?" : message
63
69
 
64
- # Subsitute real vales for template place holders
70
+ # Subsitute real vales for template place holders
65
71
  @soap = @template_ping.result(binding)
66
-
72
+
67
73
  #Clear return hash
68
74
  @return_data.clear
69
-
75
+
70
76
  # Make the call to the Avalara Ping service
71
77
  begin
72
78
  @response = @client.call(:ping, xml: @soap).to_s
73
- rescue
74
- @log.puts "#{Time.now}: Error calling Ping service ... check username and password"
79
+ rescue
80
+ @log.puts "#{Time.now}: Error calling Ping service ... check username and password"
75
81
  end
76
82
  # Load the response into a Nokogiri object and remove namespaces
77
- @doc = Nokogiri::XML(@response).remove_namespaces!
78
-
79
- #Read in an array of XPATH pointers
83
+ @doc = Nokogiri::XML(@response).remove_namespaces!
84
+
85
+ #Read in an array of XPATH pointers
80
86
  @ping_xpath = File.readlines(@def_locn + '\xpath_ping.txt')
81
-
87
+
82
88
  #Read each array element, extract the result returned by the service and place in a the @return_data hash
83
89
  @ping_xpath.each{|xpath| @return_data[xpath[2...xpath.length].chomp.to_sym] = @doc.xpath(xpath).text}
84
-
90
+
85
91
  return @return_data
86
92
  end
87
93
 
88
94
  ############################################################################################################
89
95
  # validate - call the adddress validation service
90
96
  ############################################################################################################
91
- def validate(addresscode = nil,
92
- line1 = nil,
93
- line2 = nil,
94
- line3 = nil,
95
- city = nil,
96
- region = nil,
97
- postalcode = nil,
98
- country = nil,
99
- taxregionid = nil,
100
- latitude = nil,
101
- longitude = nil,
102
- textcase = nil,
103
- coordinates = nil,
104
- taxability = nil)
105
-
106
- #Set parms passed by user - If Nil then default else use passed value
107
- @addresscode = addresscode == nil ? "123" : addresscode
108
- @line1 = line1 == nil ? "" : line1
109
- @line2 = line2 == nil ? "" : line2
110
- @line3 = line3 == nil ? "" : line3
111
- @city = city == nil ? "" : city
112
- @region = region == nil ? "" : region
113
- @postalcode = postalcode == nil ? "" : postalcode
114
- @country = country == nil ? "" : country
115
- @taxregionid = taxregionid == nil ? "0" : taxregionid
116
- @latitude = latitude == nil ? "" : latitude
117
- @longitude = longitude == nil ? "" : longitude
118
- @textcase = textcase == nil ? "Default" : textcase
119
- @coordinates = coordinates == nil ? "true" : coordinates
120
- @taxability = taxability == nil ? "false" : taxability
121
-
122
- # Subsitute real vales for template place holders
123
- @soap = @template_validate.result(binding)
124
-
125
- #Clear return hash
126
- @return_data.clear
127
-
128
- # Make the call to the Avalara Validate service
129
- begin
130
- @response = @client.call(:validate, xml: @soap).to_s
131
-
132
- rescue
133
- @log.puts "#{Time.now}: Error calling Validate service ... check username and password"
134
- end
135
-
136
- # Load the response into a Nokogiri object and remove namespaces
137
- @doc = Nokogiri::XML(@response).remove_namespaces!
138
-
139
- #Read in an array of XPATH pointers
140
- @validate_xpath = File.readlines(@def_locn + '\xpath_validate.txt')
141
-
142
- #Read each array element, extract the result returned by the service and place in a the @return_data hash
143
- @validate_xpath.each{|xpath| @return_data[xpath[2...xpath.length].chomp.to_sym] = @doc.xpath(xpath).text}
144
-
145
- return @return_data
97
+ def validate(address)
98
+
99
+ #Extract data from hash
100
+ addresscode = address[:addresscode]
101
+ line1 = address[:line1]
102
+ line2 = address[:line2]
103
+ line3 = address[:line3]
104
+ city = address[:city]
105
+ region = address[:region]
106
+ postalcode = address[:postalcode]
107
+ country = address[:country]
108
+ taxregionid = address[:taxregionid]
109
+ latitude = address[:latitude]
110
+ longitude = address[:longitude]
111
+ textcase = address[:textcase]
112
+ coordinates = address[:coordinates]
113
+ taxability = address[:taxability]
114
+
115
+ #Set parms passed by user - If Nil then default else use passed value
116
+ @addresscode = addresscode == nil ? "123" : addresscode
117
+ @line1 = line1 == nil ? "" : line1
118
+ @line2 = line2 == nil ? "" : line2
119
+ @line3 = line3 == nil ? "" : line3
120
+ @city = city == nil ? "" : city
121
+ @region = region == nil ? "" : region
122
+ @postalcode = postalcode == nil ? "" : postalcode
123
+ @country = country == nil ? "" : country
124
+ @taxregionid = taxregionid == nil ? "0" : taxregionid
125
+ @latitude = latitude == nil ? "" : latitude
126
+ @longitude = longitude == nil ? "" : longitude
127
+ @textcase = textcase == nil ? "Default" : textcase
128
+ @coordinates = coordinates == nil ? "true" : coordinates
129
+ @taxability = taxability == nil ? "false" : taxability
130
+
131
+ # Subsitute real vales for template place holders
132
+ @soap = @template_validate.result(binding)
133
+
134
+ #Clear return hash
135
+ @return_data.clear
136
+
137
+ # Make the call to the Avalara Validate service
138
+ begin
139
+ @response = @client.call(:validate, xml: @soap).to_s
140
+
141
+ rescue
142
+ @log.puts "#{Time.now}: Error calling Validate service ... check username and password"
143
+ end
144
+
145
+ # Load the response into a Nokogiri object and remove namespaces
146
+ @doc = Nokogiri::XML(@response).remove_namespaces!
147
+
148
+ #Read in an array of XPATH pointers
149
+ @validate_xpath = File.readlines(@def_locn + '\xpath_validate.txt')
150
+
151
+ #Read each array element, extract the result returned by the service and place in a the @return_data hash
152
+ @validate_xpath.each{|xpath| @return_data[xpath[2...xpath.length].chomp.to_sym] = @doc.xpath(xpath).text}
153
+
154
+ return @return_data
146
155
  end
147
-
156
+
148
157
  ############################################################################################################
149
158
  #Verifies connectivity to the web service and returns version information about the service.
150
159
  ############################################################################################################
151
160
  def isauthorized(operation = nil)
152
161
  #Read in the SOAP template
153
- @operation = operation == nil ? "?" : operation
162
+ @operation = operation == nil ? "?" : operation
154
163
 
155
- # Subsitute real vales for template place holders
164
+ # Subsitute real vales for template place holders
156
165
  @soap = @template_isauthorized.result(binding)
157
-
158
- #Clear return hash
166
+
167
+ #Clear return hash
159
168
  @return_data.clear
160
-
169
+
161
170
  # Make the call to the Avalara Ping service
162
171
  begin
163
172
  @response = @client.call(:is_authorized, xml: @soap).to_s
164
- rescue
165
- @log.puts "#{Time.now}: Error calling IsAuthorized service ... check username and password"
173
+ rescue
174
+ @log.puts "#{Time.now}: Error calling IsAuthorized service ... check username and password"
166
175
  end
167
-
176
+
168
177
  # Load the response into a Nokogiri object and remove namespaces
169
- @doc = Nokogiri::XML(@response).remove_namespaces!
170
-
171
- #Read in an array of XPATH pointers
178
+ @doc = Nokogiri::XML(@response).remove_namespaces!
179
+
180
+ #Read in an array of XPATH pointers
172
181
  @isauthorized_xpath = File.readlines(@def_locn + '\xpath_isauthorized.txt')
173
-
182
+
174
183
  #Read each array element, extract the result returned by the service and place in a the @return_data hash
175
184
  @isauthorized_xpath.each{|xpath| @return_data[xpath[2...xpath.length].chomp.to_sym] = @doc.xpath(xpath).text}
176
-
185
+
177
186
  return @return_data
178
187
  end
179
- end
188
+ end
180
189
  end
data/test/test_address.rb CHANGED
@@ -3,6 +3,12 @@ require 'avatax_addressservice'
3
3
  #require green_shoes for the GUI
4
4
  require 'green_shoes'
5
5
 
6
+ #Create new Credentials hash object
7
+ credentials = Hash.new
8
+
9
+ #Create new Address hash object
10
+ address = Hash.new
11
+
6
12
  Shoes.app :width => 400, :height => 400 do
7
13
  background orange
8
14
  border("#BE8",
@@ -11,19 +17,21 @@ Shoes.app :width => 400, :height => 400 do
11
17
  para
12
18
  para "Enter Avalara credentials"
13
19
  para "Username:"
14
- @username = edit_line
20
+ @username = edit_line text: "USERNAME"
15
21
  para "Password:"
16
- @password = edit_line :secret => true
22
+ @password = edit_line text: "PASSWORD", :secret => true
17
23
  para
18
24
  @confirm = button "Confirm"
19
25
  para
20
26
  @confirm.click {
21
- name = 'Avalara Inc.'
22
- clientname = 'MyShoppingCart'
23
- adapter = 'Avatax SDK for Ruby 1.0.1'
24
- machine = 'Lenovo W520 Windows 7'
27
+ credentials[:username] = @username.text
28
+ credentials[:password] = @password.text
29
+ credentials[:name] = 'Avalara Inc.'
30
+ credentials[:clientname] = 'MyShoppingCart'
31
+ credentials[:adapter] = 'Avatax SDK for Ruby 1.0.5'
32
+ credentials[:machine] = 'Lenovo W520 Windows 7'
25
33
  #Create an address service instance
26
- AddrService = AvaTax::AddressService.new(@username.text,@password.text,name,clientname,adapter,machine)
34
+ AddrService = AvaTax::AddressService.new(credentials)
27
35
 
28
36
  #Open a window to get the address to validate
29
37
  Shoes.app :width => 400, :height => 700, :title => "Avalara - Address Validation Tester" do
@@ -59,10 +67,19 @@ Shoes.app :width => 400, :height => 400 do
59
67
 
60
68
  #When the user clicks the Validate button then call the Validate service
61
69
  @validate.click {
70
+ address[:line1] = @line1.text
71
+ address[:line2] = @line2.text
72
+ address[:line3] = @line3.text
73
+ address[:city] = @city.text
74
+ address[:region] = @state.text
75
+ address[:postalcode] = @zip.text
76
+ address[:country]= @country.text
77
+ address[:textcase] = @textcase
78
+ address[:addresscode] = "123"
79
+
80
+ #Call the validate service - passing the address as a Hash
62
81
 
63
-
64
- #Call the validate service
65
- val_addr = AddrService.validate(nil, @line1.text, nil, nil, @city.text, @state.text, @zip.text, 'US', nil, nil, nil, @textcase, nil, nil)
82
+ val_addr = AddrService.validate(address)
66
83
 
67
84
 
68
85
  if val_addr[:ResultCode] == "Success"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Avatax_AddressService
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-28 00:00:00.000000000 Z
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri