money-oxr 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4bd37837495d074315c1d36f74eda52210f8500
4
+ data.tar.gz: 974de53313238e11c02da30fb18a39c45beca679
5
+ SHA512:
6
+ metadata.gz: 59c86351dab9217c7554a81817f79591e3f227424ee70a3923ea4c50ed1e8a18ff06a8b0f6dd0f1ab61148e0b2c7d9305ca2a18413458abb7fd0840e0e054f61
7
+ data.tar.gz: 56934936045e5a15bba1d470afc0807ef48be130a15b1f08d72408324858363b1c2d4463c92f4494ece67073614a289c14f2496914721b3a12a00b03c2eee0a5
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in money-oxr.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ed Lebert
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,83 @@
1
+ # Money::Oxr
2
+
3
+ A Money-compatible rate store that uses exchange rates from openexchangerates.org.
4
+
5
+ A few improvements over the existing money-open-exchange-rates gem:
6
+
7
+ * Uses BigDecimal instead of Float for exchange rates.
8
+ * Automatically caches API results to file if a :cache_path option is provided.
9
+ * Automatically fetches new data from API if data becomes stale if a :max_age option is provided.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'money-oxr'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install money-oxr
26
+
27
+ ## Usage
28
+
29
+ With the gem, you do not need to manually add exchange rates. Calling load will
30
+ load the rates from the cache file or download the rates from the API depending
31
+ on your options. The :source option defaults to
32
+
33
+ ``` ruby
34
+ require 'money_oxr/bank'
35
+ oxr_bank = MoneyOXR::Bank.new(
36
+ app_id: 'abcd1234',
37
+ cache_path: 'tmp/oxr.json',
38
+ max_age: 86400
39
+ )
40
+ oxr_bank.store.load
41
+ Money.default_bank = oxr_bank
42
+ ```
43
+
44
+ If you only want to load data from a file without ever fetching from the API,
45
+ the :app_id and :max_age options are not necessary.
46
+
47
+ ``` ruby
48
+ require 'money_oxr/bank'
49
+ oxr_bank = MoneyOXR::Bank.new(
50
+ cache_path: 'config/oxr.json'
51
+ )
52
+ oxr_bank.store.load
53
+ Money.default_bank = oxr_bank
54
+ ```
55
+
56
+ If you are on a paid plan from openexchangerates.org and you wish to use a different
57
+ source currency, you may provide it with the :source option.
58
+
59
+ ``` ruby
60
+ require 'money_oxr/bank'
61
+ oxr_bank = MoneyOXR::Bank.new(
62
+ app_id: 'abcd1234',
63
+ cache_path: 'tmp/oxr.json',
64
+ max_age: 86400,
65
+ source: 'GBP'
66
+ )
67
+ oxr_bank.store.load
68
+ Money.default_bank = oxr_bank
69
+ ```
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/edlebert/money-oxr.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "money/oxr"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ require 'money_oxr/rates_store'
2
+ require 'money/bank/variable_exchange'
3
+
4
+ module MoneyOXR
5
+ class Bank < Money::Bank::VariableExchange
6
+
7
+ def initialize(options={}, &block)
8
+ super(MoneyOXR::RatesStore.new(options), &block)
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,106 @@
1
+ require 'money/rates_store/memory'
2
+ require 'json'
3
+ require 'bigdecimal'
4
+ require 'open-uri'
5
+
6
+ module MoneyOXR
7
+ class RatesStore < Money::RatesStore::Memory
8
+
9
+ class UnsupportedCurrency < StandardError; end
10
+
11
+ attr_reader :app_id, :source, :cache_path, :last_updated_at, :max_age
12
+
13
+ def initialize(*)
14
+ super
15
+ @app_id = options[:app_id]
16
+ @source = options[:source] || 'USD'
17
+ @cache_path = options[:cache_path]
18
+ @max_age = options[:max_age]
19
+ end
20
+
21
+ def get_rate(iso_from, iso_to)
22
+ load
23
+ super || begin
24
+ if iso_from == source
25
+ raise UnsupportedCurrency.new(iso_to)
26
+ elsif inverse_rate = super(iso_to, iso_from)
27
+ add_rate(iso_from, iso_to, 1 / inverse_rate)
28
+ elsif iso_to == source
29
+ raise UnsupportedCurrency.new(iso_from)
30
+ else
31
+ rate1 = get_rate(iso_from, source)
32
+ rate2 = get_rate(source, iso_to)
33
+ add_rate(iso_from, iso_to, rate1 * rate2)
34
+ end
35
+ end
36
+ end
37
+
38
+ def loaded?
39
+ index.any?
40
+ end
41
+
42
+ def load
43
+ # Loads data and ensures it is not stale.
44
+ if !loaded? && cache_path && File.exist?(cache_path)
45
+ load_from_cache_path
46
+ end
47
+ if app_id && (!loaded? || stale?)
48
+ load_from_api
49
+ end
50
+ end
51
+
52
+ def stale?
53
+ return false if !max_age
54
+ return true if last_updated_at.nil?
55
+ last_updated_at + max_age < Time.now
56
+ end
57
+
58
+ def load_from_api
59
+ json = get_json_from_api
60
+ if cache_path
61
+ write_cache_file(json)
62
+ load_from_cache_path
63
+ else
64
+ load_json(json)
65
+ end
66
+ end
67
+
68
+ def get_json_from_api
69
+ open(api_uri).read
70
+ end
71
+
72
+ def api_uri
73
+ "https://openexchangerates.org/api/latest.json?source=#{source}&app_id=#{app_id}"
74
+ end
75
+
76
+ def load_from_cache_path
77
+ load_json(File.read(cache_path))
78
+ end
79
+
80
+ def write_cache_file(text)
81
+ File.open(cache_path, 'w') { |file| file.write text }
82
+ end
83
+
84
+ def load_json(text)
85
+ data = parse_json(text)
86
+ transaction do
87
+ @last_updated_at = Time.at(data['timestamp'])
88
+ index.clear
89
+ data['rates'].each do |iso_to, rate|
90
+ add_rate(source, iso_to, rate)
91
+ end
92
+ end
93
+ end
94
+
95
+ def parse_json(text)
96
+ # Convert text to strings so that we can use BigDecimal instead of Float
97
+ text = text.gsub(/("[A-Z]{3}": ?)(\d+\.\d+)/, '\\1"\\2"')
98
+ data = JSON.parse(text)
99
+ data['rates'] = data['rates'].each_with_object({}) do |(key, value), rates|
100
+ rates[key] = BigDecimal.new(value)
101
+ end
102
+ data
103
+ end
104
+
105
+ end
106
+ end
data/money-oxr.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "money-oxr"
5
+ spec.version = "0.1.0"
6
+ spec.authors = ["Ed Lebert"]
7
+
8
+ spec.summary = %q{A Money-compatible rate store that uses exchange rates from openexchangerates.org.}
9
+ spec.homepage = "https://github.com/edlebert/money-oxr"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
13
+ f.match(%r{^spec/})
14
+ end
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_dependency "money", "~> 6.0"
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.15"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "rspec", "~> 3.0"
22
+ spec.add_development_dependency "webmock", "~> 3.3"
23
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: money-oxr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ed Lebert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: money
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ description:
84
+ email:
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - ".rspec"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/money_oxr/bank.rb
99
+ - lib/money_oxr/rates_store.rb
100
+ - money-oxr.gemspec
101
+ homepage: https://github.com/edlebert/money-oxr
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.6.11
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: A Money-compatible rate store that uses exchange rates from openexchangerates.org.
125
+ test_files: []