xrate 0.1.2
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.ruby_version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +61 -0
- data/LICENSE.txt +21 -0
- data/README.md +52 -0
- data/Rakefile +6 -0
- data/lib/Rakefile +4 -0
- data/lib/generators/xrate/install/templates/create_xrate_exchange_rates.rb +11 -0
- data/lib/generators/xrate/install_generator.rb +22 -0
- data/lib/xrate.rb +6 -0
- data/lib/xrate/exchange_rate.rb +31 -0
- data/lib/xrate/railtie.rb +13 -0
- data/lib/xrate/rate.rb +9 -0
- data/lib/xrate/tasks/import.rake +28 -0
- data/lib/xrate/unconvertable_currency_exception.rb +4 -0
- data/lib/xrate/version.rb +3 -0
- data/xrate.gemspec +39 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 38091bf237eb0b4f2a4547bd7e1252ad2b94c1bc0d34afe53e2339ebdffa7aac
|
4
|
+
data.tar.gz: f04b189d4f24013c8aa7b50d7afe32cbed09e615b404dbd9b2dcb63885801207
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a5bbc4e89ac04305364dea713ad77253030abc067988783f1d4a75de7fc89feb0aed0c3d0626767c80fd8dab63e57ee9395f8069cb5c97d9dab155ebae27c469
|
7
|
+
data.tar.gz: c06f2f5bc45fff34ffe411026223ad0c3c4526bed433d8d633d5ce988ac13b351ba5ebf2edddb0ee62b06a131d4eccce93eb543a4d2618cf63427767cbb701d6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby_version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.2
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
xrate (0.1.0)
|
5
|
+
nokogiri (~> 1.8)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (5.2.0)
|
11
|
+
activesupport (= 5.2.0)
|
12
|
+
activerecord (5.2.0)
|
13
|
+
activemodel (= 5.2.0)
|
14
|
+
activesupport (= 5.2.0)
|
15
|
+
arel (>= 9.0)
|
16
|
+
activesupport (5.2.0)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
arel (9.0.0)
|
22
|
+
concurrent-ruby (1.0.5)
|
23
|
+
diff-lcs (1.3)
|
24
|
+
i18n (1.0.1)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
mini_portile2 (2.3.0)
|
27
|
+
minitest (5.11.3)
|
28
|
+
nokogiri (1.8.2)
|
29
|
+
mini_portile2 (~> 2.3.0)
|
30
|
+
rake (10.5.0)
|
31
|
+
rspec (3.7.0)
|
32
|
+
rspec-core (~> 3.7.0)
|
33
|
+
rspec-expectations (~> 3.7.0)
|
34
|
+
rspec-mocks (~> 3.7.0)
|
35
|
+
rspec-core (3.7.1)
|
36
|
+
rspec-support (~> 3.7.0)
|
37
|
+
rspec-expectations (3.7.0)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.7.0)
|
40
|
+
rspec-mocks (3.7.0)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.7.0)
|
43
|
+
rspec-support (3.7.1)
|
44
|
+
sqlite3 (1.3.13)
|
45
|
+
thread_safe (0.3.6)
|
46
|
+
tzinfo (1.2.5)
|
47
|
+
thread_safe (~> 0.1)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
ruby
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
activerecord (~> 5)
|
54
|
+
bundler (~> 1.16)
|
55
|
+
rake (~> 10.0)
|
56
|
+
rspec (~> 3.0)
|
57
|
+
sqlite3 (~> 1.3)
|
58
|
+
xrate!
|
59
|
+
|
60
|
+
BUNDLED WITH
|
61
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Sam Ward
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Xrate
|
2
|
+
Xrate is a gem for Rails which provides historic exchange rate lookups against a base currency.
|
3
|
+
|
4
|
+
This gem uses the European Central Bank 90 day historic rate feed as a source, but other sources can be used by storing the rates by creating Rate objects:
|
5
|
+
|
6
|
+
`Xrate:Rate.create(date: Date.today, currency: "GBP", rate: 0.555)`
|
7
|
+
|
8
|
+
Xrate will raise an exception if the conversion cannot be carried out due to lack of data. You should plan to gracefully handle UnconvertableCurrencyExceptions in your app.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem "xrate"
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install xrate
|
25
|
+
|
26
|
+
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
|
+
|
28
|
+
Xrate assumes that all rates will be against a base currency defined in the environment variable "XRATE_BASE_CURRENCY".
|
29
|
+
|
30
|
+
## Usage
|
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
|
+
|
33
|
+
Lookups are made by running `Xrate::ExchangeRate.at({date}, {source}, {target})`
|
34
|
+
|
35
|
+
### Example
|
36
|
+
To find out the how much £1 was worth in USD the day before brexit, you would run:
|
37
|
+
|
38
|
+
`Xrate::ExchangeRate.at("2016-06-22", "GBP", "USD")`
|
39
|
+
|
40
|
+
Xrate returns rates in [BigDecimal](https://ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/BigDecimal.html).
|
41
|
+
|
42
|
+
## Roadmap
|
43
|
+
- Add caching layer to prevent the need to hit the database for each lookup
|
44
|
+
- Make ORM and framework-agnostic
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/samward/xrate.
|
49
|
+
|
50
|
+
## License
|
51
|
+
|
52
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class CreateXrateExchangeRates < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :xrate_exchange_rates do |t|
|
4
|
+
t.string :currency, null: false
|
5
|
+
t.decimal :rate, null: false
|
6
|
+
t.date :date, null: false
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
add_index :xrate_exchange_rates, [:date, :currency]
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
require "rails/generators/migration"
|
3
|
+
|
4
|
+
module Xrate
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < ::Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
source_root File.expand_path("../install/templates", __FILE__)
|
9
|
+
desc "Add the rate storage migration for Xrate"
|
10
|
+
|
11
|
+
def self.next_migration_number(path)
|
12
|
+
next_migration_number = current_migration_number(path) + 1
|
13
|
+
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
14
|
+
end
|
15
|
+
|
16
|
+
def copy_migrations
|
17
|
+
migration_template "create_xrate_exchange_rates.rb",
|
18
|
+
"db/migrate/create_xrate_exchange_rates.rb"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/xrate.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "xrate/version"
|
2
|
+
require "bigdecimal"
|
3
|
+
require "unconvertable_currency_exception"
|
4
|
+
|
5
|
+
module Xrate
|
6
|
+
class ExchangeRate
|
7
|
+
def self.at(date, from, target)
|
8
|
+
get_rate_at(date, from, target) or
|
9
|
+
raise Xrate::UnconvertableCurrencyException
|
10
|
+
end
|
11
|
+
|
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
|
15
|
+
else
|
16
|
+
calculate_rate(date, from, target)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.calculate_rate(date, from, target)
|
21
|
+
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
|
24
|
+
|
25
|
+
base_to_target / base_to_from
|
26
|
+
rescue NoMethodError # handle divide by zero
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/xrate/rate.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
require "open-uri"
|
3
|
+
require "xrate"
|
4
|
+
|
5
|
+
|
6
|
+
namespace :xrate do
|
7
|
+
desc "Fetch latest rates from ECB"
|
8
|
+
task import: :environment do
|
9
|
+
xml = open("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml")
|
10
|
+
doc = Nokogiri::XML.parse(xml)
|
11
|
+
doc.remove_namespaces!
|
12
|
+
doc.xpath("//Cube[@time]").each do |day|
|
13
|
+
date = Date.parse(day.attributes["time"].value)
|
14
|
+
day.children.each do |entry|
|
15
|
+
currency = entry.attributes["currency"].value
|
16
|
+
exchange_rate = entry.attributes["rate"].value
|
17
|
+
rate = Xrate::Rate.find_or_create_by(date: date, currency: currency)
|
18
|
+
rate.rate = exchange_rate
|
19
|
+
|
20
|
+
if rate.save
|
21
|
+
puts "#{date} #{currency} #{exchange_rate} stored"
|
22
|
+
else
|
23
|
+
puts "Error: #{date} #{currency} #{exchange_rate} could not be stored"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/xrate.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "xrate/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "xrate"
|
8
|
+
spec.version = Xrate::VERSION
|
9
|
+
spec.authors = ["Sam Ward"]
|
10
|
+
spec.email = ["sam@s-ward.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Enables historic currency conversion}
|
13
|
+
spec.description = %q{Provides methods to fetch, cache and utilise historic exchange rates in your app.}
|
14
|
+
spec.homepage = "https://github.com/samward/xrate"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib", "lib/xrate"]
|
32
|
+
|
33
|
+
spec.add_dependency "nokogiri", "~> 1.8"
|
34
|
+
spec.add_development_dependency "activerecord", "~> 5"
|
35
|
+
spec.add_development_dependency "sqlite3", "~> 1.3"
|
36
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xrate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sam Ward
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.16'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.16'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description: Provides methods to fetch, cache and utilise historic exchange rates
|
98
|
+
in your app.
|
99
|
+
email:
|
100
|
+
- sam@s-ward.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".ruby_version"
|
108
|
+
- ".travis.yml"
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- lib/Rakefile
|
115
|
+
- lib/generators/xrate/install/templates/create_xrate_exchange_rates.rb
|
116
|
+
- lib/generators/xrate/install_generator.rb
|
117
|
+
- lib/xrate.rb
|
118
|
+
- lib/xrate/exchange_rate.rb
|
119
|
+
- lib/xrate/railtie.rb
|
120
|
+
- lib/xrate/rate.rb
|
121
|
+
- lib/xrate/tasks/import.rake
|
122
|
+
- lib/xrate/unconvertable_currency_exception.rb
|
123
|
+
- lib/xrate/version.rb
|
124
|
+
- xrate.gemspec
|
125
|
+
homepage: https://github.com/samward/xrate
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata:
|
129
|
+
allowed_push_host: https://rubygems.org
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
- lib/xrate
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.7.6
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Enables historic currency conversion
|
151
|
+
test_files: []
|