money-api-bank 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/misc.xml +4 -0
- data/.idea/modules.xml +8 -0
- data/.idea/money-api-bank.iml +15 -0
- data/.idea/workspace.xml +20 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.markdown +43 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/money/bank/api_bank.rb +268 -0
- data/lib/money/bank/api_exchange_rates_loader.rb +21 -0
- data/lib/money/bank/version.rb +5 -0
- data/money-api-bank.gemspec +37 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 28e7b9fff6938db9c1c772756daddb532e89dec8
|
4
|
+
data.tar.gz: 64f5d28440a9be07e77b2801c07adb1dc0dfe6ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c125a2526df1f9d481e06a308a83d85d55ee98d67395498a423562c97f5f0f528dcb081b91d3dea7f9b73c40b7517129b6a3d07007f70fd2fcb1677a3d340b62
|
7
|
+
data.tar.gz: f8bf8bc51d241ad0b655e7f1d66f5152f5de00b8c162efc5d2fcf09de5339053ea15f455a7eaaf5cbed309cd2561394693e41a030decfed0dd3f6b82ef1aa60a
|
data/.gitignore
ADDED
data/.idea/.rakeTasks
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<Settings><!--This file was automatically generated by Ruby plugin.
|
3
|
+
You are allowed to:
|
4
|
+
1. Remove rake task
|
5
|
+
2. Add existing rake tasks
|
6
|
+
To add existing rake tasks automatically delete this file and reload the project.
|
7
|
+
--><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Run tests" fullCmd="test" taksId="test" /><RakeTask description="" fullCmd="default" taksId="default" /></RakeGroup></Settings>
|
data/.idea/misc.xml
ADDED
data/.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectModuleManager">
|
4
|
+
<modules>
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/money-api-bank.iml" filepath="$PROJECT_DIR$/.idea/money-api-bank.iml" />
|
6
|
+
</modules>
|
7
|
+
</component>
|
8
|
+
</project>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
3
|
+
<component name="NewModuleRootManager">
|
4
|
+
<content url="file://$MODULE_DIR$" />
|
5
|
+
<orderEntry type="inheritedJdk" />
|
6
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
7
|
+
<orderEntry type="library" scope="PROVIDED" name="i18n (v0.8.1, RVM: ruby-2.4.0) [gem]" level="application" />
|
8
|
+
<orderEntry type="library" scope="PROVIDED" name="minitest (v5.10.1, RVM: ruby-2.4.0) [gem]" level="application" />
|
9
|
+
<orderEntry type="library" scope="PROVIDED" name="money (v6.8.2, RVM: ruby-2.4.0) [gem]" level="application" />
|
10
|
+
<orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, RVM: ruby-2.4.0) [gem]" level="application" />
|
11
|
+
<orderEntry type="library" scope="PROVIDED" name="rr (v1.2.0, RVM: ruby-2.4.0) [gem]" level="application" />
|
12
|
+
<orderEntry type="library" scope="PROVIDED" name="sixarm_ruby_unaccent (v1.1.1, RVM: ruby-2.4.0) [gem]" level="application" />
|
13
|
+
<orderEntry type="library" scope="PROVIDED" name="yajl-ruby (v1.3.0, RVM: ruby-2.4.0) [gem]" level="application" />
|
14
|
+
</component>
|
15
|
+
</module>
|
data/.idea/workspace.xml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ChangeListManager">
|
4
|
+
<option name="TRACKING_ENABLED" value="true" />
|
5
|
+
<option name="SHOW_DIALOG" value="false" />
|
6
|
+
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
7
|
+
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
8
|
+
<option name="LAST_RESOLUTION" value="IGNORE" />
|
9
|
+
</component>
|
10
|
+
<component name="ShelveChangesManager" show_recycled="false">
|
11
|
+
<option name="remove_strategy" value="false" />
|
12
|
+
</component>
|
13
|
+
<component name="VcsContentAnnotationSettings">
|
14
|
+
<option name="myLimit" value="2678400000" />
|
15
|
+
</component>
|
16
|
+
<component name="XDebuggerManager">
|
17
|
+
<breakpoint-manager />
|
18
|
+
<watches-manager />
|
19
|
+
</component>
|
20
|
+
</project>
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at mjmaix@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2011 Laurent Arnoud <laurent@spkdev.net>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Money API Bank
|
2
|
+
[![Build Status](https://secure.travis-ci.org/coutud/money-historical-bank.png)](http://travis-ci.org/coutud/money-historical-bank)
|
3
|
+
|
4
|
+
A gem that add a `Money::Bank` able to handle API rates, and infer rates from limited rates.
|
5
|
+
|
6
|
+
* You can add rates for any currency pair, at a special date. Use `home_run` gem if you need fast date handling in ruby.
|
7
|
+
* The gem is able to guess inverse rates (EURUSD rate when only USDEUR is present), and go through USD when using other pairs. For example, GBPEUR will be calculated using USDGBP and USDEUR. This strategy isn't used if said rate (GBPEUR) is already set.
|
8
|
+
* No caching exists, but you can use `import_data` and `export_data` the same way `Money::Bank::VariableExchange` works.
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
require 'money/bank/api_bank'
|
14
|
+
mh = Money::Bank::ApiBank.new
|
15
|
+
|
16
|
+
# Exchanges 1000 EUR to USD using Date.today (default if no date has been entered).
|
17
|
+
mh.exchange_with(1000.to_money('EUR'), 'USD')
|
18
|
+
|
19
|
+
# Exchanges 1000 EUR to USD using historical rates
|
20
|
+
date = Date.new(2009,9,9)
|
21
|
+
mh.set_rate(date, 'USD', 'EUR', 0.7634)
|
22
|
+
mh.exchange_with(date, 1000.to_money('USD'), 'EUR') # => 763.4 EUR
|
23
|
+
|
24
|
+
Money.default_bank = mh
|
25
|
+
```
|
26
|
+
|
27
|
+
## Refs
|
28
|
+
Created using mainly the base `VariableExchange` implementation, OpenExchangeRates implementation and idea based on `money-historical-bank` gem.
|
29
|
+
|
30
|
+
* https://github.com/RubyMoney/money
|
31
|
+
* https://github.com/atwam/money-historical-bank
|
32
|
+
|
33
|
+
## License
|
34
|
+
This gem is available under the *MIT license*
|
35
|
+
|
36
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
37
|
+
|
38
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
39
|
+
|
40
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
41
|
+
|
42
|
+
|
43
|
+
Copyright © 2017 MJAbadilla <mjmaix@gmail.com>
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Money::Api::Bank
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/money/api/bank`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'money-api-bank'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install money-api-bank
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/money-api-bank. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "money/api/bank"
|
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,268 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'money'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
require File.expand_path(File.dirname(__FILE__)) + '/api_exchange_rates_loader'
|
6
|
+
|
7
|
+
require 'money/bank/version'
|
8
|
+
|
9
|
+
class Money
|
10
|
+
module Bank
|
11
|
+
class InvalidCache < StandardError ; end
|
12
|
+
|
13
|
+
class ApiBank < Base
|
14
|
+
include Money::Bank::ExchangeRatesLoader
|
15
|
+
|
16
|
+
attr_reader :rates
|
17
|
+
# Available formats for importing/exporting rates.
|
18
|
+
RATE_FORMATS = [:json, :ruby, :yaml]
|
19
|
+
|
20
|
+
def setup
|
21
|
+
@rates = {}
|
22
|
+
@mutex = Mutex.new
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
# Set the rate for the given currency pair at a given date.
|
27
|
+
# Uses +Mutex+ to synchronize data access.
|
28
|
+
#
|
29
|
+
# @param [Date] date Date for which the rate is valid.
|
30
|
+
# @param [Currency, String, Symbol] from Currency to exchange from.
|
31
|
+
# @param [Currency, String, Symbol] to Currency to exchange to.
|
32
|
+
# @param [Numeric] rate Rate to use when exchanging currencies.
|
33
|
+
#
|
34
|
+
# @return [Numeric]
|
35
|
+
# @example
|
36
|
+
# bank = Money::Bank::ApiBank.new
|
37
|
+
# bank.set_rate(Date.new(2001, 1, 1), "USD", "CAD", 1.24514)
|
38
|
+
def set_rate(date, from, to, rate)
|
39
|
+
@mutex.synchronize do
|
40
|
+
internal_set_rate(date, from, to, rate)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Retrieve the rate for the given currencies. Uses +Mutex+ to synchronize
|
45
|
+
# data access. If no rates have been set for +date+, will try to load them
|
46
|
+
# using #load_data.
|
47
|
+
#
|
48
|
+
# @param [Date] date Date to retrieve the exchange rate at.
|
49
|
+
# @param [Currency, String, Symbol] from Currency to exchange from.
|
50
|
+
# @param [Currency, String, Symbol] to Currency to exchange to.
|
51
|
+
#
|
52
|
+
# @return [Numeric]
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# bank = Money::Bank::ApiBank.new
|
56
|
+
# d1 = Date.new(2001, 1, 1)
|
57
|
+
# d2 = Date.new(2002, 1, 1)
|
58
|
+
# bank.set_rate(d1, "USD", "CAD", 1.24515)
|
59
|
+
# bank.set_rate(d2, "CAD", "USD", 0.803115)
|
60
|
+
#
|
61
|
+
# bank.get_rate(d1, "USD", "CAD") #=> 1.24515
|
62
|
+
# bank.get_rate(d2, "CAD", "USD") #=> 0.803115
|
63
|
+
def get_rate(date, from, to)
|
64
|
+
@mutex.synchronize do
|
65
|
+
unless existing_rates = @rates[date.to_s]
|
66
|
+
load_data(date, from, to)
|
67
|
+
existing_rates = @rates[date.to_s]
|
68
|
+
end
|
69
|
+
rate = nil
|
70
|
+
if existing_rates
|
71
|
+
rate = existing_rates[rate_key_for(from, to)]
|
72
|
+
unless rate
|
73
|
+
# Tries to calculate an inverse rate
|
74
|
+
inverse_rate = existing_rates[rate_key_for(to, from)]
|
75
|
+
rate = 1.0 / inverse_rate if inverse_rate
|
76
|
+
end
|
77
|
+
unless rate
|
78
|
+
# Tries to calculate a pair rate using USD rate
|
79
|
+
unless from_base_rate = existing_rates[rate_key_for("USD", from)]
|
80
|
+
from_inverse_rate = existing_rates[rate_key_for(from, "USD")]
|
81
|
+
from_base_rate = 1.0 / from_inverse_rate if from_inverse_rate
|
82
|
+
end
|
83
|
+
unless to_base_rate = existing_rates[rate_key_for("USD", to)]
|
84
|
+
to_inverse_rate = existing_rates[rate_key_for(to, "USD")]
|
85
|
+
to_base_rate = 1.0 / to_inverse_rate if to_inverse_rate
|
86
|
+
end
|
87
|
+
if to_base_rate && from_base_rate
|
88
|
+
rate = to_base_rate / from_base_rate
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
rate
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# @overload exchange_with(from, to_currency)
|
98
|
+
# Exchanges the given +Money+ object to a new +Money+ object in
|
99
|
+
# +to_currency+. The exchange rate used will be for Date.today.
|
100
|
+
# If no rates are here for Date.today, it will try to load them.
|
101
|
+
# @param [Money] from
|
102
|
+
# The +Money+ object to exchange.
|
103
|
+
# @param [Currency, String, Symbol] to_currency
|
104
|
+
# The currency to exchange to.
|
105
|
+
#
|
106
|
+
# @overload exchange_with(date, from, to_currency)
|
107
|
+
# Exchanges the +Money+ object +from+ to a new +Money+ object in +to_currency+, using
|
108
|
+
# the exchange rate available on +date+.
|
109
|
+
# @param [Date] date The +Date+ at which you want to calculate the rate.
|
110
|
+
# @param [Money] from
|
111
|
+
# The +Money+ object to exchange.
|
112
|
+
# @param [Currency, String, Symbol] to_currency
|
113
|
+
# The currency to exchange to.
|
114
|
+
#
|
115
|
+
# @yield [n] Optional block to use when rounding after exchanging one
|
116
|
+
# currency for another.
|
117
|
+
# @yieldparam [Float] n The resulting float after exchanging one currency
|
118
|
+
# for another.
|
119
|
+
# @yieldreturn [Integer]
|
120
|
+
#
|
121
|
+
# @return [Money]
|
122
|
+
#
|
123
|
+
# @raise +Money::Bank::UnknownRate+ if the conversion rate is unknown.
|
124
|
+
#
|
125
|
+
# @example
|
126
|
+
# bank = Money::Bank::VariableExchange.new
|
127
|
+
# bank.add_rate(Date.today, "USD", "CAD", 1.24515)
|
128
|
+
# bank.add_rate(Date.new(2011,1,1), "CAD", "USD", 0.803115)
|
129
|
+
#
|
130
|
+
# c1 = 100_00.to_money("USD")
|
131
|
+
# c2 = 100_00.to_money("CAD")
|
132
|
+
#
|
133
|
+
# # Exchange 100 USD to CAD:
|
134
|
+
# bank.exchange_with(c1, "CAD") #=> #<Money @cents=1245150>
|
135
|
+
#
|
136
|
+
# # Exchange 100 CAD to USD:
|
137
|
+
# bank.exchange_with(Date.new(2011,1,1), c2, "USD") #=> #<Money @cents=803115>
|
138
|
+
def exchange_with(*args)
|
139
|
+
date, from, to_currency = args.length == 2 ? [Date.today] + args : args
|
140
|
+
|
141
|
+
return from if same_currency?(from.currency, to_currency)
|
142
|
+
|
143
|
+
rate = get_rate(date, from.currency, to_currency)
|
144
|
+
unless rate
|
145
|
+
raise UnknownRate, "No conversion rate available for #{date} '#{from.currency.iso_code}' -> '#{to_currency}'"
|
146
|
+
end
|
147
|
+
_to_currency_ = Currency.wrap(to_currency)
|
148
|
+
|
149
|
+
cents = BigDecimal.new(from.cents.to_s) / (BigDecimal.new(from.currency.subunit_to_unit.to_s) / BigDecimal.new(_to_currency_.subunit_to_unit.to_s))
|
150
|
+
|
151
|
+
ex = cents * BigDecimal.new(rate.to_s)
|
152
|
+
ex = ex.to_f
|
153
|
+
ex = if block_given?
|
154
|
+
yield ex
|
155
|
+
elsif @rounding_method
|
156
|
+
@rounding_method.call(ex)
|
157
|
+
else
|
158
|
+
ex.to_s.to_i
|
159
|
+
end
|
160
|
+
Money.new(ex, _to_currency_)
|
161
|
+
end
|
162
|
+
|
163
|
+
# Return the known rates as a string in the format specified. If +file+
|
164
|
+
# is given will also write the string out to the file specified.
|
165
|
+
# Available formats are +:json+, +:ruby+ and +:yaml+.
|
166
|
+
#
|
167
|
+
# @param [Symbol] format Request format for the resulting string.
|
168
|
+
# @param [String] file Optional file location to write the rates to.
|
169
|
+
#
|
170
|
+
# @return [String]
|
171
|
+
#
|
172
|
+
# @raise +Money::Bank::UnknownRateFormat+ if format is unknown.
|
173
|
+
#
|
174
|
+
# @example
|
175
|
+
# bank = Money::Bank::VariableExchange.new
|
176
|
+
# bank.set_rate("USD", "CAD", 1.24515)
|
177
|
+
# bank.set_rate("CAD", "USD", 0.803115)
|
178
|
+
#
|
179
|
+
# s = bank.export_rates(:json)
|
180
|
+
# s #=> "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
|
181
|
+
def export_rates(format, file=nil)
|
182
|
+
raise Money::Bank::UnknownRateFormat unless
|
183
|
+
RATE_FORMATS.include? format
|
184
|
+
|
185
|
+
s = ""
|
186
|
+
@mutex.synchronize {
|
187
|
+
s = case format
|
188
|
+
when :json
|
189
|
+
JSON.dump(@rates)
|
190
|
+
when :ruby
|
191
|
+
Marshal.dump(@rates)
|
192
|
+
when :yaml
|
193
|
+
YAML.dump(@rates)
|
194
|
+
end
|
195
|
+
|
196
|
+
unless file.nil?
|
197
|
+
File.open(file, "w").write(s)
|
198
|
+
end
|
199
|
+
}
|
200
|
+
s
|
201
|
+
end
|
202
|
+
|
203
|
+
# Loads rates provided in +s+ given the specified format. Available
|
204
|
+
# formats are +:json+, +:ruby+ and +:yaml+.
|
205
|
+
#
|
206
|
+
# @param [Symbol] format The format of +s+.
|
207
|
+
# @param [String] s The rates string.
|
208
|
+
#
|
209
|
+
# @return [self]
|
210
|
+
#
|
211
|
+
# @raise +Money::Bank::UnknownRateFormat+ if format is unknown.
|
212
|
+
#
|
213
|
+
# @example
|
214
|
+
# s = "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
|
215
|
+
# bank = Money::Bank::VariableExchange.new
|
216
|
+
# bank.import_rates(:json, s)
|
217
|
+
#
|
218
|
+
# bank.get_rate("USD", "CAD") #=> 1.24515
|
219
|
+
# bank.get_rate("CAD", "USD") #=> 0.803115
|
220
|
+
def import_rates(format, s)
|
221
|
+
raise Money::Bank::UnknownRateFormat unless
|
222
|
+
RATE_FORMATS.include? format
|
223
|
+
|
224
|
+
@mutex.synchronize {
|
225
|
+
@rates = case format
|
226
|
+
when :json
|
227
|
+
JSON.load(s)
|
228
|
+
when :ruby
|
229
|
+
Marshal.load(s)
|
230
|
+
when :yaml
|
231
|
+
YAML.load(s)
|
232
|
+
end
|
233
|
+
}
|
234
|
+
self
|
235
|
+
end
|
236
|
+
|
237
|
+
private
|
238
|
+
# Return the rate hashkey for the given currencies.
|
239
|
+
#
|
240
|
+
# @param [Currency, String, Symbol] from The currency to exchange from.
|
241
|
+
# @param [Currency, String, Symbol] to The currency to exchange to.
|
242
|
+
#
|
243
|
+
# @return [String]
|
244
|
+
#
|
245
|
+
# @example
|
246
|
+
# rate_key_for("USD", "CAD") #=> "USD_TO_CAD"
|
247
|
+
def rate_key_for(from, to)
|
248
|
+
"#{Currency.wrap(from).iso_code}_TO_#{Currency.wrap(to).iso_code}".upcase
|
249
|
+
end
|
250
|
+
|
251
|
+
# Set the rate for the given currency pair at a given date.
|
252
|
+
# Doesn't use any mutex.
|
253
|
+
#
|
254
|
+
# @param [Date] date Date for which the rate is valid.
|
255
|
+
# @param [Currency, String, Symbol] from Currency to exchange from.
|
256
|
+
# @param [Currency, String, Symbol] to Currency to exchange to.
|
257
|
+
# @param [Numeric] rate Rate to use when exchanging currencies.
|
258
|
+
#
|
259
|
+
# @return [Numeric]
|
260
|
+
def internal_set_rate(date, from, to, rate)
|
261
|
+
if Money::Currency.find(from) && Money::Currency.find(to)
|
262
|
+
date_rates = @rates[date.to_s] ||= {}
|
263
|
+
date_rates[rate_key_for(from, to)] = rate
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'money'
|
3
|
+
require 'date'
|
4
|
+
require 'yajl'
|
5
|
+
require 'open-uri'
|
6
|
+
|
7
|
+
class Money
|
8
|
+
module Bank
|
9
|
+
# will store returned exchange rate
|
10
|
+
module ExchangeRatesLoader
|
11
|
+
API_URL = ENV['URL']
|
12
|
+
|
13
|
+
def load_data(date, from, to)
|
14
|
+
rates_source = API_URL + "?on=#{date.strftime('%Y-%m-%d')}&from=#{from}&to=#{to}"
|
15
|
+
doc = Yajl::Parser.parse(open(rates_source).read)
|
16
|
+
|
17
|
+
internal_set_rate(date, doc.from, doc.to, doc.rate) if doc.blank?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'money/bank/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "money-api-bank"
|
8
|
+
spec.version = Money::Bank::VERSION
|
9
|
+
spec.authors = ["MJ Abadilla"]
|
10
|
+
spec.email = ["mjmaix@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "A gem that can be used with custom Exchange Rate API. Ideal for own apps using money gem"
|
13
|
+
spec.description = "A gem that provides rates for the money gem that will use provided URL. Modified clone of money-historical-bank gem."
|
14
|
+
spec.homepage = "https://github.com/mjmaix/#{spec.name}"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
f.match(%r{^(test|spec|features)/})
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_dependency "yajl-ruby", ">=0.8.3"
|
34
|
+
spec.add_dependency "money", ">=3.7.1"
|
35
|
+
spec.add_development_dependency "minitest", ">=2.0"
|
36
|
+
spec.add_development_dependency "rr", ">=1.0.4"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: money-api-bank
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- MJ Abadilla
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yajl-ruby
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.3
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: money
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.7.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.7.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rr
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.0.4
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.0.4
|
97
|
+
description: A gem that provides rates for the money gem that will use provided URL.
|
98
|
+
Modified clone of money-historical-bank gem.
|
99
|
+
email:
|
100
|
+
- mjmaix@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".idea/.rakeTasks"
|
107
|
+
- ".idea/misc.xml"
|
108
|
+
- ".idea/modules.xml"
|
109
|
+
- ".idea/money-api-bank.iml"
|
110
|
+
- ".idea/workspace.xml"
|
111
|
+
- CODE_OF_CONDUCT.md
|
112
|
+
- Gemfile
|
113
|
+
- LICENSE
|
114
|
+
- README.markdown
|
115
|
+
- README.md
|
116
|
+
- Rakefile
|
117
|
+
- bin/console
|
118
|
+
- bin/setup
|
119
|
+
- lib/money/bank/api_bank.rb
|
120
|
+
- lib/money/bank/api_exchange_rates_loader.rb
|
121
|
+
- lib/money/bank/version.rb
|
122
|
+
- money-api-bank.gemspec
|
123
|
+
homepage: https://github.com/mjmaix/money-api-bank
|
124
|
+
licenses: []
|
125
|
+
metadata:
|
126
|
+
allowed_push_host: https://rubygems.org
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
requirements: []
|
142
|
+
rubyforge_project:
|
143
|
+
rubygems_version: 2.6.8
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: A gem that can be used with custom Exchange Rate API. Ideal for own apps
|
147
|
+
using money gem
|
148
|
+
test_files: []
|