Class: ActiveTax::States::WA
- Inherits:
-
Object
- Object
- ActiveTax::States::WA
- Defined in:
- lib/active_tax/states/wa.rb
Constant Summary collapse
- API_URI =
"http://rateservice.dor.wa.gov/text"
Class Method Summary collapse
- .normalize(tax_data) ⇒ Object
- .parse_result(text) ⇒ Object
- .rate(address = {}) ⇒ Object
- .tax(address = {}) ⇒ Object
Class Method Details
.normalize(tax_data) ⇒ Object
45 46 47 48 |
# File 'lib/active_tax/states/wa.rb', line 45 def self.normalize(tax_data) Struct.new(:rate, :location_code, :result_code) .new(tax_data["Rate"].to_f, tax_data["LocationCode"], tax_data["ResultCode"]) end |
.parse_result(text) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/active_tax/states/wa.rb', line 35 def self.parse_result(text) r = {} items = text.split(" ") items.each do |item| k, v = item.split("=") r[k] = v end r end |
.rate(address = {}) ⇒ Object
30 31 32 33 |
# File 'lib/active_tax/states/wa.rb', line 30 def self.rate(address={}) tax_data = tax(address) tax_data && tax_data.rate end |
.tax(address = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/active_tax/states/wa.rb', line 6 def self.tax(address={}) # http://rateservice.dor.wa.gov/text?output=text&addr=6500%20Linderson%20way&city=&zip=98501 params = { output: "text", addr: "#{address[:address]}", city: "#{address[:city]}", zip: "#{address[:zip]}" } require 'net/http' # Needed for HTTP requests uri = URI("#{API_URI}") uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri) # LocationCode=3406 Rate=0.087 ResultCode=0 if res.is_a?(Net::HTTPSuccess) normalize(self.parse_result(res.body)) else false end end |