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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38091bf237eb0b4f2a4547bd7e1252ad2b94c1bc0d34afe53e2339ebdffa7aac
4
- data.tar.gz: f04b189d4f24013c8aa7b50d7afe32cbed09e615b404dbd9b2dcb63885801207
3
+ metadata.gz: 42283c21f607751e2038cf4f3cca867aaa48b1aaaa98af19ca6fefba7bcf0493
4
+ data.tar.gz: 740e9e2c3a233068a3b67755d22c6ee0060b687b2f0f6dd92b5bdf7fb59ef783
5
5
  SHA512:
6
- metadata.gz: a5bbc4e89ac04305364dea713ad77253030abc067988783f1d4a75de7fc89feb0aed0c3d0626767c80fd8dab63e57ee9395f8069cb5c97d9dab155ebae27c469
7
- data.tar.gz: c06f2f5bc45fff34ffe411026223ad0c3c4526bed433d8d633d5ce988ac13b351ba5ebf2edddb0ee62b06a131d4eccce93eb543a4d2618cf63427767cbb701d6
6
+ metadata.gz: 866853d5403916db473e36a2c1bf45eec894bb262790eaa16a3ee83f3ed528cba2b09a5edb9c39aec45e0722e96db4790b92ded3511c0dfddb2d9d38b98c7f33
7
+ data.tar.gz: 2746022c870330e728145087b7fc773320c6468871b319bdc9789830277a217bc4c3f39aaf82538ed4b3ee0e0f7072e40f706f6225a35f72fbb27a50889dc721
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xrate (0.1.0)
4
+ xrate (0.1.3)
5
5
  nokogiri (~> 1.8)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -19,18 +19,18 @@ And then execute:
19
19
 
20
20
  $ bundle
21
21
 
22
- Or install it yourself as:
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
- $ gem install xrate
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}, {source}, {target})`
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:
@@ -4,28 +4,33 @@ require "unconvertable_currency_exception"
4
4
 
5
5
  module Xrate
6
6
  class ExchangeRate
7
- def self.at(date, from, target)
8
- get_rate_at(date, from, target) or
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.get_rate_at(date, from, target)
13
- if from == ENV.fetch("XRATE_BASE_CURRENCY")
14
- Xrate::Rate.where(date: date, currency: target).first&.rate
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(date, from, target)
17
+ calculate_rate(date_range, from_currency, target_currency)
17
18
  end
18
19
  end
19
20
 
20
- def self.calculate_rate(date, from, target)
21
+ def self.calculate_rate(date_range, from_currency, target_currency)
21
22
  begin
22
- base_to_from = Xrate::Rate.where(date: date, currency: from).first&.rate
23
- base_to_target = Xrate::Rate.where(date: date, currency: target).first&.rate
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
@@ -3,6 +3,8 @@ require "active_record"
3
3
  module Xrate
4
4
  class Rate < ActiveRecord::Base
5
5
  self.table_name = "xrate_exchange_rates"
6
+ default_scope { order(date: :desc) }
7
+
6
8
  validates :currency, uniqueness: { scope: :date }
7
9
  validates :currency, :rate, :date, presence: true
8
10
  end
@@ -1,3 +1,3 @@
1
1
  module Xrate
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ward