json_vat 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/json_vat.rb +25 -9
- data/lib/json_vat/file_cache_backend.rb +21 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6e91c0929c693ba81cdd950d28c34648671d3dd
|
4
|
+
data.tar.gz: 51d688b813adea233b3534415cf3826fa83f0f2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6bf4023b4c57fe9cbaf87f1424f775dc7514e81dd6f3c293b66d0581952fdc277e211f7a5361398d2ea751e23be88e1a5570a1efc6b987aef1138dde2bdaee48
|
7
|
+
data.tar.gz: 68be36f8498fa19967176ec0decb514f43fe95a9151cedf03cf19e77d93d38a3c3454929ec9094c298e42796fa647acea5797e85a36ed6e9186a0c723028895d
|
data/lib/json_vat.rb
CHANGED
@@ -2,30 +2,41 @@ require 'net/http'
|
|
2
2
|
require 'uri'
|
3
3
|
require 'json'
|
4
4
|
require 'json_vat/country'
|
5
|
+
require 'json_vat/file_cache_backend'
|
5
6
|
|
6
7
|
module JSONVAT
|
7
8
|
|
8
9
|
class << self
|
9
10
|
|
10
|
-
def
|
11
|
-
@
|
11
|
+
def perform_caching?
|
12
|
+
@perform_caching != false
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
+
def cache_backend
|
16
|
+
@cache_backend ||= FileCacheBackend.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def host
|
20
|
+
@host ||= 'http://jsonvat.com'
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_writer :cache_backend
|
24
|
+
attr_writer :perform_caching
|
25
|
+
attr_writer :host
|
15
26
|
|
16
27
|
def download
|
17
|
-
Net::HTTP.get_response(URI.parse(
|
28
|
+
Net::HTTP.get_response(URI.parse(self.host)).body
|
18
29
|
end
|
19
30
|
|
20
31
|
def cache
|
21
32
|
content = self.download
|
22
|
-
|
33
|
+
self.cache_backend.write(content)
|
23
34
|
content
|
24
35
|
end
|
25
36
|
|
26
37
|
def rates_through_cache
|
27
|
-
if self.
|
28
|
-
|
38
|
+
if self.perform_caching?
|
39
|
+
self.cache_backend.read || self.cache
|
29
40
|
else
|
30
41
|
self.download
|
31
42
|
end
|
@@ -38,7 +49,13 @@ module JSONVAT
|
|
38
49
|
end
|
39
50
|
|
40
51
|
def country(country)
|
41
|
-
|
52
|
+
code = country.to_s.upcase
|
53
|
+
|
54
|
+
# Fix ISO-3166-1-alpha2 exceptions
|
55
|
+
if code == 'UK' then code = 'GB' end
|
56
|
+
if code == 'EL' then code = 'GR' end
|
57
|
+
|
58
|
+
self.rates.find { |r| r.country_code == code }
|
42
59
|
end
|
43
60
|
|
44
61
|
def [](country)
|
@@ -47,5 +64,4 @@ module JSONVAT
|
|
47
64
|
|
48
65
|
end
|
49
66
|
|
50
|
-
|
51
67
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module JSONVAT
|
2
|
+
class FileCacheBackend
|
3
|
+
|
4
|
+
attr_accessor :path
|
5
|
+
|
6
|
+
def initialize(path = "/tmp/jsonvat.json")
|
7
|
+
@path = path
|
8
|
+
end
|
9
|
+
|
10
|
+
def read
|
11
|
+
File.read(self.path)
|
12
|
+
rescue Errno::ENOENT
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def write(data)
|
17
|
+
File.open(self.path, 'w') { |f| f.write(data) }
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_vat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Cooke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -34,6 +34,7 @@ extra_rdoc_files: []
|
|
34
34
|
files:
|
35
35
|
- lib/json_vat.rb
|
36
36
|
- lib/json_vat/country.rb
|
37
|
+
- lib/json_vat/file_cache_backend.rb
|
37
38
|
- lib/json_vat/period.rb
|
38
39
|
homepage: http://github.com/adamcooke/json-vat
|
39
40
|
licenses:
|
@@ -55,9 +56,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
56
|
version: '0'
|
56
57
|
requirements: []
|
57
58
|
rubyforge_project:
|
58
|
-
rubygems_version: 2.
|
59
|
+
rubygems_version: 2.4.5
|
59
60
|
signing_key:
|
60
61
|
specification_version: 4
|
61
62
|
summary: A client library for jsonvat.com
|
62
63
|
test_files: []
|
63
|
-
has_rdoc:
|