Module: JSONVAT
- Defined in:
- lib/json_vat.rb,
lib/json_vat/period.rb,
lib/json_vat/country.rb,
lib/json_vat/file_cache_backend.rb
Defined Under Namespace
Classes: Country, FileCacheBackend, Period
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.cache_backend ⇒ Object
15
16
17
|
# File 'lib/json_vat.rb', line 15
def cache_backend
@cache_backend ||= FileCacheBackend.new
end
|
.host ⇒ Object
19
20
21
|
# File 'lib/json_vat.rb', line 19
def host
@host ||= 'http://jsonvat.com'
end
|
Sets the attribute perform_caching
24
25
26
|
# File 'lib/json_vat.rb', line 24
def perform_caching=(value)
@perform_caching = value
end
|
Class Method Details
.[](country) ⇒ Object
73
74
75
|
# File 'lib/json_vat.rb', line 73
def [](country)
country(country)
end
|
.cache ⇒ Object
31
32
33
34
35
|
# File 'lib/json_vat.rb', line 31
def cache
content = self.download
self.cache_backend.write(content)
content
end
|
.country(country) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/json_vat.rb', line 63
def country(country)
code = country.to_s.upcase
if code == 'UK' then code = 'GB' end
if code == 'EL' then code = 'GR' end
self.rates.find { |r| r.country_code == code }
end
|
.download ⇒ Object
27
28
29
|
# File 'lib/json_vat.rb', line 27
def download
Net::HTTP.get_response(URI.parse(self.host)).body
end
|
.load_rates ⇒ Object
45
46
47
48
49
|
# 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
|
11
12
13
|
# File 'lib/json_vat.rb', line 11
def perform_caching?
@perform_caching != false
end
|
.rates ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/json_vat.rb', line 55
def rates
if reload_rates?
@rates = load_rates
else
@rates
end
end
|
.rates_through_cache ⇒ Object
37
38
39
40
41
42
43
|
# 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? ⇒ Boolean
51
52
53
|
# File 'lib/json_vat.rb', line 51
def reload_rates?
!self.perform_caching? || (self.perform_caching? && self.cache_backend.invalid?)
end
|