json_vat 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 20f4120b0c9be6f1807760ae008dc9effb797cc8
4
- data.tar.gz: c19b02a2eb56bfbe0a71a72c714edfa8ccd61ffe
3
+ metadata.gz: a6e91c0929c693ba81cdd950d28c34648671d3dd
4
+ data.tar.gz: 51d688b813adea233b3534415cf3826fa83f0f2a
5
5
  SHA512:
6
- metadata.gz: a2170edf60a89e9ef87e9acc82cdfc9b586a84a6bc7c04c990ea20a4da3e0838ad0fedf0223a57ed608397522fd7b4632b76e5ce891c840dd5c1935c44251970
7
- data.tar.gz: a5af4c65a44df5631deb1c59a7b0658128928263d5b6bbc52a20eefc8db17a9c28bc2376b761ebf9abd8a847fa4a22a3cf1cbca7bf643337a34fd9471f22687a
6
+ metadata.gz: 6bf4023b4c57fe9cbaf87f1424f775dc7514e81dd6f3c293b66d0581952fdc277e211f7a5361398d2ea751e23be88e1a5570a1efc6b987aef1138dde2bdaee48
7
+ data.tar.gz: 68be36f8498fa19967176ec0decb514f43fe95a9151cedf03cf19e77d93d38a3c3454929ec9094c298e42796fa647acea5797e85a36ed6e9186a0c723028895d
@@ -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 cache_path
11
- @cache_file ||= '/tmp/jsonvat.json'
11
+ def perform_caching?
12
+ @perform_caching != false
12
13
  end
13
14
 
14
- attr_writer :cache_path
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('http://jsonvat.com')).body
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
- File.open(self.cache_path, 'w') { |f| f.write(content) }
33
+ self.cache_backend.write(content)
23
34
  content
24
35
  end
25
36
 
26
37
  def rates_through_cache
27
- if self.cache_path.is_a?(String)
28
- File.exist?(self.cache_path) ? File.read(self.cache_path) : self.cache
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
- self.rates.select { |r| r.country_code == country.to_s.upcase }.first
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.0.0
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: 2014-12-16 00:00:00.000000000 Z
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.2.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: