Class: ExchangeRate
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ExchangeRate
- Includes:
- Models::Auditable
- Defined in:
- app/models/exchange_rate.rb
Overview
== Schema Information
Table name: exchange_rates
Database name: primary
id :integer not null, primary key
effective_date :date
from_currency :string(255)
rate :decimal(8, 6)
to_currency :string(255)
created_at :datetime
updated_at :datetime
Indexes
idx_unique_exchange_rate (from_currency,to_currency,effective_date) UNIQUE
Constant Summary
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Class Method Summary collapse
-
.add_rate(from_currency, to_currency, rate) ⇒ Object
For the money interface so this store can beused by Money::Bank::VariableExchange.
- .convert_currency(from_currency, to_currency, date, amount) ⇒ Object
- .download_rate(from_currency, to_currency, effective_date = nil) ⇒ Object
-
.each_rate ⇒ Object
For the money interface so this store can beused by Money::Bank::VariableExchange.
- .get_all_exchange_rates(logger = Rails.logger) ⇒ Object
- .get_exchange_rate(from_currency, to_currency, date = nil) ⇒ Object
- .get_exchange_rates_for_today(_logger = Rails.logger) ⇒ Object
-
.get_rate(from_currency, to_currency) ⇒ Object
For the money interface so this store can beused by Money::Bank::VariableExchange.
Methods included from Models::Auditable
#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Models::EventPublishable
Class Method Details
.add_rate(from_currency, to_currency, rate) ⇒ Object
For the money interface so this store can beused by Money::Bank::VariableExchange
28 29 30 |
# File 'app/models/exchange_rate.rb', line 28 def self.add_rate(from_currency, to_currency, rate) create_with(rate: rate).find_or_create_by(from_currency: from_currency, to_currency: to_currency, effective_date: Date.current) end |
.convert_currency(from_currency, to_currency, date, amount) ⇒ Object
41 42 43 44 |
# File 'app/models/exchange_rate.rb', line 41 def self.convert_currency(from_currency, to_currency, date, amount) conversion_rate = download_rate(from_currency, to_currency, date) (amount * conversion_rate.rate).round(2) end |
.download_rate(from_currency, to_currency, effective_date = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/exchange_rate.rb', line 50 def self.download_rate(from_currency, to_currency, effective_date = nil) effective_date = Date.current if effective_date.nil? || effective_date > Date.current rate = find_by(from_currency: from_currency, to_currency: to_currency, effective_date: effective_date) if rate.nil? # We try to retrieve this from historical bank require 'money/bank/historical_bank' mh = Money::Bank::HistoricalBank.new ex_rate = mh.get_rate(effective_date, from_currency, to_currency) return nil if ex_rate.nil? logger.info "Getting rate for #{effective_date} (#{from_currency} to #{to_currency})" rate = ExchangeRate.create!(from_currency: from_currency, to_currency: to_currency, rate: ex_rate, effective_date: effective_date) inverse_rate = 1.0/ex_rate opp_rate = create_with(rate: inverse_rate).find_or_create_by(from_currency: to_currency, to_currency: from_currency, effective_date: effective_date) end rate end |
.each_rate ⇒ Object
For the money interface so this store can beused by Money::Bank::VariableExchange
33 34 35 36 37 38 39 |
# File 'app/models/exchange_rate.rb', line 33 def self.each_rate return find_each unless block_given? find_each do |rate| yield rate.from_currency, rate.to_currency, rate.rate end end |
.get_all_exchange_rates(logger = Rails.logger) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/models/exchange_rate.rb', line 68 def self.get_all_exchange_rates(logger = Rails.logger) days = (1..Date.current.day) days.each do |day| date = Date.new(Date.current.year, Date.current.month, day) logger.info "Getting day rates for #{date}" companies = Company.where.not(id: 3) companies.each do |company| ExchangeRate.download_rate('USD', company.currency, date) ExchangeRate.download_rate(company.currency, 'USD', date) end end unless Date.current.month == 1 months = (1..Date.current.month - 1) months.each do |month| days = (1..Time.days_in_month(month, Date.current.year)) days.each do |day| date = Date.new(Date.current.year, month, day) logger.info "Getting month rates for #{date}" companies = Company.where.not(id: [1,3]) companies.each do |company| ExchangeRate.download_rate('USD', company.currency, date) ExchangeRate.download_rate(company.currency, 'USD', date) end end end end end |
.get_exchange_rate(from_currency, to_currency, date = nil) ⇒ Object
46 47 48 |
# File 'app/models/exchange_rate.rb', line 46 def self.get_exchange_rate(from_currency, to_currency, date = nil) download_rate(from_currency, to_currency, date)&.rate end |
.get_exchange_rates_for_today(_logger = Rails.logger) ⇒ Object
98 99 100 101 102 103 104 |
# File 'app/models/exchange_rate.rb', line 98 def self.get_exchange_rates_for_today(_logger = Rails.logger) companies = Company.where.not(id: [1,3]) companies.each do |company| ExchangeRate.download_rate('USD', company.currency, Date.current) ExchangeRate.download_rate(company.currency, 'USD', Date.current) end end |
.get_rate(from_currency, to_currency) ⇒ Object
For the money interface so this store can beused by Money::Bank::VariableExchange
23 24 25 |
# File 'app/models/exchange_rate.rb', line 23 def self.get_rate(from_currency, to_currency) get_exchange_rate(from_currency, to_currency) end |