Module: JSONVAT

Defined in:
lib/json_vat.rb,
lib/json_vat/period.rb,
lib/json_vat/country.rb

Defined Under Namespace

Classes: Country, Period

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_pathObject



10
11
12
# File 'lib/json_vat.rb', line 10

def cache_path
  @cache_file ||= '/tmp/jsonvat.json'
end

Class Method Details

.[](country) ⇒ Object



44
45
46
# File 'lib/json_vat.rb', line 44

def [](country)
  country(country)
end

.cacheObject



20
21
22
23
24
# File 'lib/json_vat.rb', line 20

def cache
  content = self.download
  File.open(self.cache_path, 'w') { |f| f.write(content) }
  content
end

.country(country) ⇒ Object



40
41
42
# File 'lib/json_vat.rb', line 40

def country(country)
  self.rates.select { |r| r.country_code == country.to_s.upcase }.first
end

.downloadObject



16
17
18
# File 'lib/json_vat.rb', line 16

def download
  Net::HTTP.get_response(URI.parse('http://jsonvat.com')).body
end

.ratesObject



34
35
36
37
38
# File 'lib/json_vat.rb', line 34

def rates
  @rates ||= JSON.parse(self.rates_through_cache)['rates'].map do |country|
    JSONVAT::Country.new(country)
  end
end

.rates_through_cacheObject



26
27
28
29
30
31
32
# File 'lib/json_vat.rb', line 26

def rates_through_cache
  if self.cache_path.is_a?(String)
    File.exist?(self.cache_path) ? File.read(self.cache_path) : self.cache
  else
    self.download
  end
end