Class: ZipTax::SalesTax

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_tax/sales_tax.rb

Constant Summary collapse

BASE_URL =
"http://api.zip-tax.com/request/v20"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postalcode: nil, state: nil, city: nil) ⇒ SalesTax

Returns a new instance of SalesTax.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zip_tax/sales_tax.rb', line 11

def initialize(postalcode: nil,
               state: nil,
               city: nil)

  fail ArgumentError, 'ZipTax.api_key must be set' if ZipTax.api_key.nil?
  fail ArgumentError, "postalcode is a required argument" if !postalcode
  @key = key
  @postalcode = postalcode.to_s
  @state = state
  @city = city
  @format = 'json'
end

Instance Attribute Details

#cityObject (readonly)

Returns the value of attribute city.



7
8
9
# File 'lib/zip_tax/sales_tax.rb', line 7

def city
  @city
end

#formatObject (readonly)

Returns the value of attribute format.



7
8
9
# File 'lib/zip_tax/sales_tax.rb', line 7

def format
  @format
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/zip_tax/sales_tax.rb', line 7

def key
  @key
end

#postalcodeObject (readonly)

Returns the value of attribute postalcode.



7
8
9
# File 'lib/zip_tax/sales_tax.rb', line 7

def postalcode
  @postalcode
end

#stateObject (readonly)

Returns the value of attribute state.



7
8
9
# File 'lib/zip_tax/sales_tax.rb', line 7

def state
  @state
end

Class Method Details

.request_for(postalcode: nil, state: nil, city: nil) ⇒ Object



36
37
38
39
40
# File 'lib/zip_tax/sales_tax.rb', line 36

def self.request_for(postalcode: nil, state: nil, city: nil)

  rt = self.new(postalcode: postalcode, state: state, city: city)
  rt.request_tax
end

Instance Method Details

#request_taxObject



30
31
32
33
34
# File 'lib/zip_tax/sales_tax.rb', line 30

def request_tax
  url = URI(request_url)
  resp = Net::HTTP.get(url)
  ZipTax::Response.new_from_http_request(resp)
end

#request_urlObject



24
25
26
27
28
# File 'lib/zip_tax/sales_tax.rb', line 24

def request_url
  query = {postalcode: @postalcode, state: @state, city: @city, format: @format}
  query.merge!(key: ZipTax.api_key)
  "#{BASE_URL}?#{Rack::Utils.build_query(query)}"
end