module JSONVAT
Attributes
cache_backend[W]
host[W]
perform_caching[W]
Public Class Methods
[](country)
click to toggle source
# File lib/json_vat.rb, line 73 def [](country) country(country) end
cache()
click to toggle source
# File lib/json_vat.rb, line 31 def cache content = self.download self.cache_backend.write(content) content end
cache_backend()
click to toggle source
# File lib/json_vat.rb, line 15 def cache_backend @cache_backend ||= FileCacheBackend.new end
country(country)
click to toggle source
# File lib/json_vat.rb, line 63 def country(country) code = country.to_s.upcase # Fix ISO-3166-1-alpha2 exceptions if code == 'UK' then code = 'GB' end if code == 'EL' then code = 'GR' end self.rates.find { |r| r.country_code == code } end
download()
click to toggle source
# File lib/json_vat.rb, line 27 def download Net::HTTP.get_response(URI.parse(self.host)).body end
host()
click to toggle source
# File lib/json_vat.rb, line 19 def host @host ||= 'http://jsonvat.com' end
load_rates()
click to toggle source
# File lib/json_vat.rb, line 45 def load_rates JSON.parse(self.rates_through_cache)['rates'].map do |country| JSONVAT::Country.new(country) end end
perform_caching?()
click to toggle source
# File lib/json_vat.rb, line 11 def perform_caching? @perform_caching != false end
rates()
click to toggle source
# File lib/json_vat.rb, line 55 def rates if reload_rates? @rates = load_rates else @rates end end
rates_through_cache()
click to toggle source
# File lib/json_vat.rb, line 37 def rates_through_cache if self.perform_caching? self.cache_backend.read || self.cache else self.download end end
reload_rates?()
click to toggle source
# File lib/json_vat.rb, line 51 def reload_rates? !self.perform_caching? || (self.perform_caching? && self.cache_backend.invalid?) end