json_vat 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20f4120b0c9be6f1807760ae008dc9effb797cc8
4
+ data.tar.gz: c19b02a2eb56bfbe0a71a72c714edfa8ccd61ffe
5
+ SHA512:
6
+ metadata.gz: a2170edf60a89e9ef87e9acc82cdfc9b586a84a6bc7c04c990ea20a4da3e0838ad0fedf0223a57ed608397522fd7b4632b76e5ce891c840dd5c1935c44251970
7
+ data.tar.gz: a5af4c65a44df5631deb1c59a7b0658128928263d5b6bbc52a20eefc8db17a9c28bc2376b761ebf9abd8a847fa4a22a3cf1cbca7bf643337a34fd9471f22687a
@@ -0,0 +1,51 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'json_vat/country'
5
+
6
+ module JSONVAT
7
+
8
+ class << self
9
+
10
+ def cache_path
11
+ @cache_file ||= '/tmp/jsonvat.json'
12
+ end
13
+
14
+ attr_writer :cache_path
15
+
16
+ def download
17
+ Net::HTTP.get_response(URI.parse('http://jsonvat.com')).body
18
+ end
19
+
20
+ def cache
21
+ content = self.download
22
+ File.open(self.cache_path, 'w') { |f| f.write(content) }
23
+ content
24
+ end
25
+
26
+ 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
29
+ else
30
+ self.download
31
+ end
32
+ end
33
+
34
+ def rates
35
+ @rates ||= JSON.parse(self.rates_through_cache)['rates'].map do |country|
36
+ JSONVAT::Country.new(country)
37
+ end
38
+ end
39
+
40
+ def country(country)
41
+ self.rates.select { |r| r.country_code == country.to_s.upcase }.first
42
+ end
43
+
44
+ def [](country)
45
+ country(country)
46
+ end
47
+
48
+ end
49
+
50
+
51
+ end
@@ -0,0 +1,43 @@
1
+ require 'json_vat/period'
2
+
3
+ module JSONVAT
4
+ class Country
5
+
6
+ def initialize(attributes)
7
+ @attributes = attributes
8
+ end
9
+
10
+ def name
11
+ @attributes['name']
12
+ end
13
+
14
+ def country_code
15
+ @attributes['country_code']
16
+ end
17
+
18
+ def code
19
+ @attributes['code']
20
+ end
21
+
22
+ def periods
23
+ @periods ||= @attributes['periods'].map { |p| Period.new(self, p) }
24
+ end
25
+
26
+ def period_on(date)
27
+ periods.select { |p| p.effective_from <= date }.sort_by(&:effective_from).last
28
+ end
29
+
30
+ def rate_on(date, type = :standard)
31
+ if period = period_on(date)
32
+ period.rates[type.to_s]
33
+ else
34
+ nil
35
+ end
36
+ end
37
+
38
+ def rate(type = :standard)
39
+ rate_on(Date.today, type)
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ require 'date'
2
+
3
+ module JSONVAT
4
+ class Period
5
+
6
+ def initialize(country, attributes)
7
+ @country, @attributes = country, attributes
8
+ end
9
+
10
+ def effective_from
11
+ @effective_from ||= Date.parse(@attributes['effective_from'])
12
+ end
13
+
14
+ def rates
15
+ @attributes['rates'] || {}
16
+ end
17
+
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_vat
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ description: Allows you to easily lookup VAT rats for EU countries based on the data
28
+ from jsonvat.com.
29
+ email:
30
+ - me@adamcooke.io
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/json_vat.rb
36
+ - lib/json_vat/country.rb
37
+ - lib/json_vat/period.rb
38
+ homepage: http://github.com/adamcooke/json-vat
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.2.2
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: A client library for jsonvat.com
62
+ test_files: []
63
+ has_rdoc: