xrate 0.1.2 → 0.1.3
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/Gemfile.lock +1 -1
- data/README.md +5 -5
- data/lib/xrate/exchange_rate.rb +14 -9
- data/lib/xrate/rate.rb +2 -0
- data/lib/xrate/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42283c21f607751e2038cf4f3cca867aaa48b1aaaa98af19ca6fefba7bcf0493
|
4
|
+
data.tar.gz: 740e9e2c3a233068a3b67755d22c6ee0060b687b2f0f6dd92b5bdf7fb59ef783
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 866853d5403916db473e36a2c1bf45eec894bb262790eaa16a3ee83f3ed528cba2b09a5edb9c39aec45e0722e96db4790b92ded3511c0dfddb2d9d38b98c7f33
|
7
|
+
data.tar.gz: 2746022c870330e728145087b7fc773320c6468871b319bdc9789830277a217bc4c3f39aaf82538ed4b3ee0e0f7072e40f706f6225a35f72fbb27a50889dc721
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -19,18 +19,18 @@ And then execute:
|
|
19
19
|
|
20
20
|
$ bundle
|
21
21
|
|
22
|
-
|
22
|
+
### Environment variables
|
23
|
+
Xrate assumes that all rates will be against a base currency defined in the environment variable `XRATE_BASE_CURRENCY`, for example "EUR".
|
23
24
|
|
24
|
-
|
25
|
+
Exchange rate datasets will often have missing data, due to market closures (e.g. weekends and religious/national holidays). To handle this, Xrate will fall back to the last available datapoint, within a given number of days. This fallback range *must* be provided in the environment variable `XRATE_FALLBACK_DAYS`. A sensible default for this is 4, as this will cover Easter and any Christmas that spans a weekend. You can increase/decrease this depending on the dataset and the accuracy your application requires.
|
25
26
|
|
27
|
+
### Local database table
|
26
28
|
Xrate stores a cache of the current exhange rates in the database and assumes `ActiveRecord` is present. To generate the migration for this table, run `rails generate xrate:install` and `rails db:migrate` to run the migration.
|
27
29
|
|
28
|
-
Xrate assumes that all rates will be against a base currency defined in the environment variable "XRATE_BASE_CURRENCY".
|
29
|
-
|
30
30
|
## Usage
|
31
31
|
To get the last 90 days of exhange rate data from the [European Central Bank](http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml), run `rake xrate:import`. This can be run on a regular basis to keep your rate cache up to date. Note, Xrate will always take the last import as correct, overwriting any previous stored rates.
|
32
32
|
|
33
|
-
Lookups are made by running `Xrate::ExchangeRate.at({date}, {
|
33
|
+
Lookups are made by running `Xrate::ExchangeRate.at({date}, {source_currency}, {target_currency})`
|
34
34
|
|
35
35
|
### Example
|
36
36
|
To find out the how much £1 was worth in USD the day before brexit, you would run:
|
data/lib/xrate/exchange_rate.rb
CHANGED
@@ -4,28 +4,33 @@ require "unconvertable_currency_exception"
|
|
4
4
|
|
5
5
|
module Xrate
|
6
6
|
class ExchangeRate
|
7
|
-
def self.at(date,
|
8
|
-
|
7
|
+
def self.at(date, from_currency, target_currency)
|
8
|
+
date_range = (date - fallback_days)..date
|
9
|
+
get_latest(date_range, from_currency, target_currency) or
|
9
10
|
raise Xrate::UnconvertableCurrencyException
|
10
11
|
end
|
11
12
|
|
12
|
-
def self.
|
13
|
-
if
|
14
|
-
Xrate::Rate.where(date:
|
13
|
+
def self.get_latest(date_range, from_currency, target_currency)
|
14
|
+
if from_currency == ENV.fetch("XRATE_BASE_CURRENCY")
|
15
|
+
Xrate::Rate.where(date: date_range, currency: target_currency).first&.rate
|
15
16
|
else
|
16
|
-
calculate_rate(
|
17
|
+
calculate_rate(date_range, from_currency, target_currency)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
20
|
-
def self.calculate_rate(
|
21
|
+
def self.calculate_rate(date_range, from_currency, target_currency)
|
21
22
|
begin
|
22
|
-
base_to_from = Xrate::Rate.where(date:
|
23
|
-
base_to_target = Xrate::Rate.where(date:
|
23
|
+
base_to_from = Xrate::Rate.where(date: date_range, currency: from_currency).first&.rate
|
24
|
+
base_to_target = Xrate::Rate.where(date: date_range, currency: target_currency).first&.rate
|
24
25
|
|
25
26
|
base_to_target / base_to_from
|
26
27
|
rescue NoMethodError # handle divide by zero
|
27
28
|
nil
|
28
29
|
end
|
29
30
|
end
|
31
|
+
|
32
|
+
def self.fallback_days
|
33
|
+
ENV.fetch("XRATE_FALLBACK_DAYS").to_i
|
34
|
+
end
|
30
35
|
end
|
31
36
|
end
|
data/lib/xrate/rate.rb
CHANGED
data/lib/xrate/version.rb
CHANGED