justexchangerates 0.2.1 → 0.3.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
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/justexchangerates.rb +45 -44
- metadata +22 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1571659794639751cd6da74bff7940ec151fdf79
|
4
|
+
data.tar.gz: c676d5c2d029f620a15e667e1f8bbd3edca31e46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5eca2a98653e5cda26eac351eb253dd4bf359aeffb1c4b89c139bbe1b41c1b906f6eb4e37571edfb6a32febe51a9ece5c2ff57c8ad1b3da866dd2cc152139386
|
7
|
+
data.tar.gz: 5ceb1dd8e8cbaa0b2bccdd3bc7f3a09bcf5d03d7da670263d74a74f45f845ffeae9359ebfcdf307b11c746ea75d2d681e4458e29dfb51509f2dd148d4f7efa5e
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/justexchangerates.rb
CHANGED
@@ -5,16 +5,20 @@
|
|
5
5
|
require 'json'
|
6
6
|
require 'open-uri'
|
7
7
|
require 'chronic_duration'
|
8
|
+
require 'open_exchange_rates'
|
8
9
|
|
9
10
|
|
11
|
+
# Note: To use this gem you need an **app id** which you can get when
|
12
|
+
# registering for free from https://openexchangerates.org/signup/free
|
13
|
+
|
10
14
|
class JustExchangeRates
|
11
15
|
|
12
16
|
attr_reader *%i(base date rates)
|
13
17
|
|
14
|
-
def initialize(
|
15
|
-
|
18
|
+
def initialize(base: 'USD', cache_refresh: '1 week', cache_path: '.', \
|
19
|
+
debug: false, app_id: nil)
|
16
20
|
|
17
|
-
@
|
21
|
+
@base, @debug, @app_id, @cache_refresh = base, debug, app_id, cache_refresh
|
18
22
|
|
19
23
|
filename = 'justexchangerates_' + base.downcase + '.json'
|
20
24
|
@cache_filepath = File.join(cache_path, filename)
|
@@ -23,60 +27,57 @@ class JustExchangeRates
|
|
23
27
|
|
24
28
|
if File.exists? @cache_filepath then
|
25
29
|
|
26
|
-
|
27
|
-
seconds = ChronicDuration.parse(cache_refresh)
|
28
|
-
|
29
|
-
if (Time.parse(h2['date']) + seconds) < Time.now then
|
30
|
-
fetch_rates
|
31
|
-
else
|
32
|
-
puts 'fetching rates from local cache ...' if @debug
|
33
|
-
h2
|
34
|
-
end
|
30
|
+
JSON.parse(File.read(@cache_filepath), {:symbolize_names => true})
|
35
31
|
|
36
32
|
else
|
37
|
-
|
33
|
+
{base: @base, date: Time.now, rates: {}}
|
38
34
|
end
|
39
|
-
|
40
|
-
else
|
41
|
-
|
42
|
-
fetch_rates
|
43
|
-
|
35
|
+
|
44
36
|
end
|
45
37
|
|
46
|
-
|
47
|
-
|
38
|
+
puts 'h: ' + h.inspect if debug
|
39
|
+
|
40
|
+
@base, @date, @rates = h[:base], h[:date], h[:rates]
|
48
41
|
|
49
42
|
end
|
50
43
|
|
51
44
|
def rate(currency)
|
52
|
-
|
45
|
+
|
46
|
+
if @rates.has_key? currency.upcase.to_sym then
|
47
|
+
|
48
|
+
seconds = ChronicDuration.parse(@cache_refresh)
|
49
|
+
puts 'seconds: ' + seconds.inspect if @debug
|
50
|
+
t = (Time.parse(rates[currency.upcase.to_sym].last) \
|
51
|
+
+ seconds)
|
52
|
+
puts 't: ' + t.inspect if @debug
|
53
|
+
if t > Time.now then
|
54
|
+
rates[currency.upcase.to_sym].first
|
55
|
+
else
|
56
|
+
fetch_rate currency
|
57
|
+
end
|
58
|
+
|
59
|
+
else
|
60
|
+
fetch_rate currency
|
61
|
+
end
|
62
|
+
|
53
63
|
end
|
54
64
|
|
65
|
+
|
55
66
|
private
|
56
67
|
|
57
|
-
def
|
58
|
-
|
59
|
-
response = open(@url + @base.upcase)
|
60
|
-
|
61
|
-
if response.status.last == "OK" then
|
62
|
-
|
63
|
-
h = JSON.parse(response.read)
|
64
|
-
h['date'] = Time.now
|
65
|
-
File.write @cache_filepath, h.to_json
|
66
|
-
h
|
67
|
-
|
68
|
-
else
|
69
|
-
puts "JustExchangeRates API error %s : %s" % response.status
|
70
|
-
{}
|
71
|
-
end
|
68
|
+
def fetch_rate(currency)
|
72
69
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
70
|
+
puts 'inside fetch_rate' if @debug
|
71
|
+
fx = OpenExchangeRates::Rates.new(:app_id => @app_id)
|
72
|
+
|
73
|
+
rate = fx.convert(1, :from => "USD", :to => currency)
|
74
|
+
puts "currency: %s rate: %s" % [currency, rate] if @debug
|
75
|
+
|
76
|
+
@rates[currency.upcase.to_sym] = [rate, Time.now.to_s]
|
77
|
+
File.write @cache_filepath, \
|
78
|
+
{base: @base, date: @date, rates: @rates}.to_json
|
79
|
+
rate
|
80
|
+
|
81
|
+
end
|
81
82
|
|
82
83
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: justexchangerates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
wxj+BoAMVbJfP/AfQ46F5erI2X10cQzKt41kZwYasgu98+v6KOKzR+J/d7nKaVJ/
|
31
31
|
BL0=
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2018-
|
33
|
+
date: 2018-06-08 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: chronic_duration
|
@@ -52,6 +52,26 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 0.10.6
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: open_exchange_rates
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.5'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 0.5.1
|
65
|
+
type: :runtime
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0.5'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.5.1
|
55
75
|
description:
|
56
76
|
email: james@jamesrobertson.eu
|
57
77
|
executables: []
|
metadata.gz.sig
CHANGED
Binary file
|