simple_forex 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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +102 -0
- data/Rakefile +36 -0
- data/lib/easy_money.rb +109 -0
- data/lib/generators/simple_forex/simple_forex_generator.rb +28 -0
- data/lib/simple_forex/.DS_Store +0 -0
- data/lib/simple_forex/initializer.rb +1 -0
- data/lib/simple_forex/migration.rb +15 -0
- data/lib/simple_forex/railtie.rb +13 -0
- data/lib/simple_forex/tasks/simple_forex.rake +79 -0
- data/lib/simple_forex/version.rb +5 -0
- data/lib/simple_forex.rb +109 -0
- data/sig/simple_forex.rbs +4 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 15bbf36ebf4a841feed8f60243b2b0d06abff9b144d60a372ab4df8a23195330
|
4
|
+
data.tar.gz: 2b9837d7cb44e58381d023950a19e627f53a2a845d448e906c56363dc3c87a61
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9beb9be479dce8b19b36feb829f51838ce93bbf32478afb23284f9f7f905c369ba528730dd7a48c6560c0b488fa304cf88c5a87880f190b38029f92e4719b2a8
|
7
|
+
data.tar.gz: 8e6e97c5374f5c493b7e951c6da47a123efa2e4fa070981261984e0f6d9c909ceff94804e9c87fd569cac435572d0020a6bdd77c58863960458b1d2f4851e6ec
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Steve Condylios
|
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,102 @@
|
|
1
|
+
# SimpleForex
|
2
|
+
|
3
|
+
The goal of the simple_forex gem is to help you quickly:
|
4
|
+
|
5
|
+
- Add a currencies table to your rails app
|
6
|
+
- Schedule the retrieval of up to date foreign exchange data (hourly from a free API)
|
7
|
+
- Convert between currencies
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
```bash
|
14
|
+
bundle add simple_forex
|
15
|
+
```
|
16
|
+
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
gem install simple_forex
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
### Add a currencies table to your rails app
|
28
|
+
|
29
|
+
After installing the gem, run this to add a currencies table to your rails app
|
30
|
+
|
31
|
+
```bash
|
32
|
+
rails generate simple_forex
|
33
|
+
```
|
34
|
+
|
35
|
+
followed by
|
36
|
+
|
37
|
+
```
|
38
|
+
rake db:migrate
|
39
|
+
```
|
40
|
+
|
41
|
+
### Run a rake task to update currencies
|
42
|
+
|
43
|
+
|
44
|
+
Get a [free API key](https://openexchangerates.org/signup/free), open credentials.yml (`EDITOR="vim" rails credentials:edit`) and add the API key to credentials.yml like so:
|
45
|
+
|
46
|
+
```
|
47
|
+
simple_forex:
|
48
|
+
openexchangerates_key: 1234AB
|
49
|
+
```
|
50
|
+
|
51
|
+
Then, run this to retrieve currencies in the currencies table:
|
52
|
+
|
53
|
+
```bash
|
54
|
+
rake simple_forex:fetch_rates
|
55
|
+
```
|
56
|
+
|
57
|
+
Confirm it worked in the rails console with something like `Currency.last`.
|
58
|
+
|
59
|
+
|
60
|
+
### Schedule it
|
61
|
+
|
62
|
+
**Optional**
|
63
|
+
|
64
|
+
Schedule the `simple_forex:fetch_rates` rake task to run at the frequency you require.
|
65
|
+
|
66
|
+
Tip: open exchange rates's free API gives 1000 calls monthly, and there are ~700 hours in month, so you safely run it hourly while staying within the free limit.
|
67
|
+
|
68
|
+
|
69
|
+
### Convert between currencies
|
70
|
+
|
71
|
+
Convert currencies by calling
|
72
|
+
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
require 'simple_forex'
|
76
|
+
convert(amount, from_currency, to_currency)
|
77
|
+
```
|
78
|
+
|
79
|
+
|
80
|
+
### Example
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
require 'simple_forex'
|
84
|
+
convert(100, 'USD', 'EUR')
|
85
|
+
# => 89.0
|
86
|
+
```
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
## Development
|
91
|
+
|
92
|
+
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.
|
93
|
+
|
94
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/stevecondylios/simple_forex.
|
99
|
+
|
100
|
+
## License
|
101
|
+
|
102
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
require "rubocop/rake_task"
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new
|
11
|
+
|
12
|
+
task default: %i[spec rubocop]
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
require 'simple_forex'
|
19
|
+
|
20
|
+
path = File.expand_path(__dir__)
|
21
|
+
Dir.glob("#{path}/tasks/**/*.rake").each { |f| import f }
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
#Sc: nice example here: https://github.com/norman/friendly_id/blob/81442a69df30a604e63fbbd789ba22464de5c758/Rakefile
|
26
|
+
|
27
|
+
desc "Test task"
|
28
|
+
task :testtask do
|
29
|
+
puts "This is a test task"
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
data/lib/easy_money.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "simple_forex/version"
|
4
|
+
|
5
|
+
module SimpleForex
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
# From: https://stackoverflow.com/a/31701809/5783745
|
9
|
+
# Otherwise after the migration you get Currency.count # uninitialized constant Currency (NameError)
|
10
|
+
class Currency < ActiveRecord::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
def currency_exchange_rate(from, to)
|
16
|
+
|
17
|
+
currencies = Currency.last.blob
|
18
|
+
|
19
|
+
# Tests:
|
20
|
+
# currency_exchange_rate("aud", "gbp") # 0.54
|
21
|
+
# currency_exchange_rate("GBP", "AUD") # 1.832
|
22
|
+
# currency_exchange_rate("USD", "AUD") # 1.29
|
23
|
+
# currency_exchange_rate("AUD", "usd") # 0.77
|
24
|
+
|
25
|
+
# The open exchange rate API uses UPPER case (unlike stripe and some other platforms)
|
26
|
+
from = from.upcase
|
27
|
+
to = to.upcase
|
28
|
+
|
29
|
+
error_message = """
|
30
|
+
The currency %{currency_code} is not a valid currency code.
|
31
|
+
Run `Currency.last.blob[\"rates\"].keys` to view
|
32
|
+
available currencies.
|
33
|
+
"""
|
34
|
+
# from = "AUDz"
|
35
|
+
# error_message % {currency_code: from}
|
36
|
+
|
37
|
+
raise error_message % {currency_code: from} unless currencies["rates"].keys.include? from
|
38
|
+
raise error_message % {currency_code: to} unless currencies["rates"].keys.include? to
|
39
|
+
|
40
|
+
|
41
|
+
usd_to_from = currencies['rates'][from].to_d
|
42
|
+
usd_to_to = currencies['rates'][to].to_d
|
43
|
+
rate = usd_to_to / usd_to_from
|
44
|
+
# This converts 1 AUD to GPD (gives 0.54585)
|
45
|
+
# R example: rates$rates$GBP / rates$rates$AUD
|
46
|
+
# Ruby: currency['rates']['GBP'].to_d / currency['rates']['AUD'].to_d
|
47
|
+
|
48
|
+
return rate
|
49
|
+
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def convert(amount, from_currency, to_currency)
|
56
|
+
# Examples:
|
57
|
+
# convert(100, "aud", "gbp") # 54.585
|
58
|
+
# convert(100, "gbp", "aud") # 183.2
|
59
|
+
# convert(100, "usd", "aud") # 129.0
|
60
|
+
# convert(100, "aud", "usd") # 77.0
|
61
|
+
begin
|
62
|
+
rate = currency_exchange_rate(from_currency, to_currency)
|
63
|
+
rescue => exception
|
64
|
+
$stderr.puts "Error: #{exception} - \n\nYou could be seeing this error if you forgot to setup the database and fetch currency data.
|
65
|
+
\nDid you forget to run `rails g simple_forex:install`, `rake db:migrate` or `rake simple_forex:fetch_rates`?
|
66
|
+
\nSee documentation for more information: https://github.com/stevecondylios/SimpleForex\n\n"
|
67
|
+
end
|
68
|
+
|
69
|
+
converted_amount = amount * rate
|
70
|
+
return converted_amount
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def all_currencies
|
76
|
+
# Acquired via Currency.last.blob['rates'].keys
|
77
|
+
["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNH", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "SSP", "STD", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VES", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XDR", "XOF", "XPD", "XPF", "XPT", "YER", "ZAR", "ZMW", "ZWL"]
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
def some_currencies
|
84
|
+
["AUD", "CAD", "EUR", "GBP", "HKD", "NZD", "SGD", "USD"]
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
# Required for rake files
|
91
|
+
# From here: https://gist.github.com/ntamvl/7a6658b4cd82d6fbd15434f0a9953411#integrate-our-gem-with-rails-apps
|
92
|
+
# require 'railtie' if defined?(Rails)
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
end # This is the end for the module
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
# sc: this goes at the end so the module gets loaded when require 'simple_forex' is called
|
104
|
+
include SimpleForex
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
require "simple_forex/railtie" if defined?(Rails::Railtie)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
require "rails/generators/active_record"
|
3
|
+
|
4
|
+
# This generator adds a migration for the {FriendlyId::History
|
5
|
+
# FriendlyId::History} addon.
|
6
|
+
class SimpleForexGenerator < ActiveRecord::Generators::Base
|
7
|
+
# ActiveRecord::Generators::Base inherits from Rails::Generators::NamedBase which requires a NAME parameter for the
|
8
|
+
# new table name. Our generator always uses 'friendly_id_slugs', so we just set a random name here.
|
9
|
+
argument :name, type: :string, default: "a_temporary_name"
|
10
|
+
|
11
|
+
class_option :'skip-migration', type: :boolean, desc: "Don't generate a migration for the currencies table"
|
12
|
+
class_option :'skip-initializer', type: :boolean, desc: "Don't generate an initializer"
|
13
|
+
|
14
|
+
source_root File.expand_path("../../../simple_forex", __FILE__)
|
15
|
+
|
16
|
+
# Copies the migration template to db/migrate.
|
17
|
+
def copy_files
|
18
|
+
return if options["skip-migration"]
|
19
|
+
migration_template "migration.rb", "db/migrate/create_currencies.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
# # Uncomment to copy /lib/simple_forex/initializer.rb to config/initializers/SimpleForex.rb
|
24
|
+
# def create_initializer
|
25
|
+
# return if options["skip-initializer"]
|
26
|
+
# copy_file "initializer.rb", "config/initializers/SimpleForex.rb"
|
27
|
+
# end
|
28
|
+
end
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
# sc: intentially blank for now
|
@@ -0,0 +1,15 @@
|
|
1
|
+
MIGRATION_CLASS =
|
2
|
+
if ActiveRecord::VERSION::MAJOR >= 5
|
3
|
+
ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]
|
4
|
+
else
|
5
|
+
ActiveRecord::Migration
|
6
|
+
end
|
7
|
+
|
8
|
+
class CreateCurrencies < MIGRATION_CLASS
|
9
|
+
def change
|
10
|
+
create_table :currencies do |t|
|
11
|
+
t.jsonb :blob
|
12
|
+
t.timestamps
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'simple_forex'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module SimpleForex
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
railtie_name :simple_forex
|
7
|
+
|
8
|
+
rake_tasks do
|
9
|
+
path = File.expand_path(__dir__)
|
10
|
+
Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
# Useful example from ryanb's letter_opener gem
|
4
|
+
# namespace :tmp do
|
5
|
+
# task :zzzzzzzzzzzzzz do
|
6
|
+
# rm_rf Dir["tmp/letter_opener/[^.]*"], verbose: false
|
7
|
+
# end
|
8
|
+
|
9
|
+
# task zzzzzzzzz: :letter_opener
|
10
|
+
# end
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
# Run from terminal with: RAILS_ENV=test rake fetch_rates
|
17
|
+
|
18
|
+
desc "This retrieves currency data"
|
19
|
+
namespace :simple_forex do
|
20
|
+
task :fetch_rates => :environment do
|
21
|
+
|
22
|
+
|
23
|
+
error_message = """
|
24
|
+
Missing openexchangerates_key in credentials.
|
25
|
+
Please ensure you have added the key to your credentials.
|
26
|
+
|
27
|
+
Edit credentials.yml with
|
28
|
+
|
29
|
+
```
|
30
|
+
EDITOR=\"vim\" rails credentials:edit
|
31
|
+
```
|
32
|
+
|
33
|
+
It should contain lines like this:
|
34
|
+
|
35
|
+
```
|
36
|
+
simple_forex:
|
37
|
+
openexchangerates_key: 1234AB
|
38
|
+
```
|
39
|
+
"""
|
40
|
+
# from = "AUDz"
|
41
|
+
# error_message % {currency_code: from}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
begin
|
46
|
+
openexchangerates_key = Rails.application.credentials.simple_forex.openexchangerates_key
|
47
|
+
rescue => exception
|
48
|
+
$stderr.puts "Error: #{exception} - \n\n" + error_message + "\n\n"
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
open_exchange_rates_url = "https://openexchangerates.org/api/latest.json?app_id=" + openexchangerates_key
|
56
|
+
|
57
|
+
cur = JSON.load(URI.open(open_exchange_rates_url))
|
58
|
+
|
59
|
+
# Iterate over each of the rates and ensure it's a decimal, otherwise it could mess with multiplication later
|
60
|
+
# Here's why decimal over float;
|
61
|
+
# https://stackoverflow.com/a/8523253/5783745
|
62
|
+
# TL;DR slightly more accurate, slightly less performant
|
63
|
+
cur["rates"].each { |code, rate| cur["rates"][code] = rate.to_d }
|
64
|
+
|
65
|
+
Currency.create!(blob: cur)
|
66
|
+
|
67
|
+
success_message = """
|
68
|
+
Successfully updated %{number_of_currencies} currencies.
|
69
|
+
View them with `Currency.last.blob[\"rates\"].keys` or `Currency.last`.
|
70
|
+
"""
|
71
|
+
|
72
|
+
puts success_message % {number_of_currencies: Currency.last.blob["rates"].count.to_s }
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
data/lib/simple_forex.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "simple_forex/version"
|
4
|
+
|
5
|
+
module SimpleForex
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
# From: https://stackoverflow.com/a/31701809/5783745
|
9
|
+
# Otherwise after the migration you get Currency.count # uninitialized constant Currency (NameError)
|
10
|
+
class Currency < ActiveRecord::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
def currency_exchange_rate(from, to)
|
16
|
+
|
17
|
+
currencies = Currency.last.blob
|
18
|
+
|
19
|
+
# Tests:
|
20
|
+
# currency_exchange_rate("aud", "gbp") # 0.54
|
21
|
+
# currency_exchange_rate("GBP", "AUD") # 1.832
|
22
|
+
# currency_exchange_rate("USD", "AUD") # 1.29
|
23
|
+
# currency_exchange_rate("AUD", "usd") # 0.77
|
24
|
+
|
25
|
+
# The open exchange rate API uses UPPER case (unlike stripe and some other platforms)
|
26
|
+
from = from.upcase
|
27
|
+
to = to.upcase
|
28
|
+
|
29
|
+
error_message = """
|
30
|
+
The currency %{currency_code} is not a valid currency code.
|
31
|
+
Run `Currency.last.blob[\"rates\"].keys` to view
|
32
|
+
available currencies.
|
33
|
+
"""
|
34
|
+
# from = "AUDz"
|
35
|
+
# error_message % {currency_code: from}
|
36
|
+
|
37
|
+
raise error_message % {currency_code: from} unless currencies["rates"].keys.include? from
|
38
|
+
raise error_message % {currency_code: to} unless currencies["rates"].keys.include? to
|
39
|
+
|
40
|
+
|
41
|
+
usd_to_from = currencies['rates'][from].to_d
|
42
|
+
usd_to_to = currencies['rates'][to].to_d
|
43
|
+
rate = usd_to_to / usd_to_from
|
44
|
+
# This converts 1 AUD to GPD (gives 0.54585)
|
45
|
+
# R example: rates$rates$GBP / rates$rates$AUD
|
46
|
+
# Ruby: currency['rates']['GBP'].to_d / currency['rates']['AUD'].to_d
|
47
|
+
|
48
|
+
return rate
|
49
|
+
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def convert(amount, from_currency, to_currency)
|
56
|
+
# Examples:
|
57
|
+
# convert(100, "aud", "gbp") # 54.585
|
58
|
+
# convert(100, "gbp", "aud") # 183.2
|
59
|
+
# convert(100, "usd", "aud") # 129.0
|
60
|
+
# convert(100, "aud", "usd") # 77.0
|
61
|
+
begin
|
62
|
+
rate = currency_exchange_rate(from_currency, to_currency)
|
63
|
+
rescue => exception
|
64
|
+
$stderr.puts "Error: #{exception} - \n\nYou could be seeing this error if you forgot to setup the database and fetch currency data.
|
65
|
+
\nDid you forget to run `rails g simple_forex:install`, `rake db:migrate` or `rake simple_forex:fetch_rates`?
|
66
|
+
\nSee documentation for more information: https://github.com/stevecondylios/SimpleForex\n\n"
|
67
|
+
end
|
68
|
+
|
69
|
+
converted_amount = amount * rate
|
70
|
+
return converted_amount
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def all_currencies
|
76
|
+
# Acquired via Currency.last.blob['rates'].keys
|
77
|
+
["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNH", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "SSP", "STD", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VES", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XDR", "XOF", "XPD", "XPF", "XPT", "YER", "ZAR", "ZMW", "ZWL"]
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
def some_currencies
|
84
|
+
["AUD", "CAD", "EUR", "GBP", "HKD", "NZD", "SGD", "USD"]
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
# Required for rake files
|
91
|
+
# From here: https://gist.github.com/ntamvl/7a6658b4cd82d6fbd15434f0a9953411#integrate-our-gem-with-rails-apps
|
92
|
+
# require 'railtie' if defined?(Rails)
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
end # This is the end for the module
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
# sc: this goes at the end so the module gets loaded when require 'simple_forex' is called
|
104
|
+
include SimpleForex
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
require "simple_forex/railtie" if defined?(Rails::Railtie)
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_forex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Condylios
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: simple_forex lets you setup and use currency conversion in your Rails
|
14
|
+
application. It includes a migration for a currencies table that stores all world
|
15
|
+
currencies, including some cryptocurrencies. It also includes a rake task for retrieving
|
16
|
+
up to date foreign exchange rates, and methods to conveniently convert between currencies.
|
17
|
+
email:
|
18
|
+
- steve.condylios@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- ".rspec"
|
24
|
+
- ".rubocop.yml"
|
25
|
+
- CHANGELOG.md
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- lib/easy_money.rb
|
31
|
+
- lib/generators/simple_forex/simple_forex_generator.rb
|
32
|
+
- lib/simple_forex.rb
|
33
|
+
- lib/simple_forex/.DS_Store
|
34
|
+
- lib/simple_forex/initializer.rb
|
35
|
+
- lib/simple_forex/migration.rb
|
36
|
+
- lib/simple_forex/railtie.rb
|
37
|
+
- lib/simple_forex/tasks/simple_forex.rake
|
38
|
+
- lib/simple_forex/version.rb
|
39
|
+
- sig/simple_forex.rbs
|
40
|
+
homepage: https://github.com/stevecondylios/simple_forex
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://github.com/stevecondylios/simple_forex
|
45
|
+
source_code_uri: https://github.com/stevecondylios/simple_forex
|
46
|
+
changelog_uri: https://github.com/stevecondylios/simple_forex/CHANGELOG.md
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.6.0
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.2.32
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: simple_forex lets you quickly set up currency conversion capabilities in
|
66
|
+
your Rails application.
|
67
|
+
test_files: []
|