active_currency 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c07e2a76dae93079049c2140345f1e38a64d81ac
4
- data.tar.gz: 179ad081203f6de02d2b44a74f1449d9eead1d52
3
+ metadata.gz: e714ea208f9bd8ebded58b96bde8497a9f9c47c2
4
+ data.tar.gz: fdd79e4b7ed274966cc932f7f4ba8536040ba02c
5
5
  SHA512:
6
- metadata.gz: e31ee097e49beaaad38f1c714d1893ca80a6b09b8ef1211bc37545d0470a000470b99e1b3583ee62f4be269e9e23d4eb7413a9ca8d2e31cc0d5c8d44e41ff9cf
7
- data.tar.gz: 1258f0d28a6be18644b1d19452ede084f439257b50b35bab6138bef3c9b0fc5bfa9b8019c9ee53b85f83bf34c46a204be315f36b63209576558dfb05f2d7e5ac
6
+ metadata.gz: ef833fcd22cf76b2511ff68a4930d1026e3687c129674abb6f96a79b2fdd64899f5c962d9fe582c911f10835c6d40363199a3876e0669e48d17c69982a734864
7
+ data.tar.gz: f2d8a04600e685729c538a47323c43504a7ad939ade1b425c1e940e5acdd9d09d9ad639dbbfa0b7dcd16e3a71945a0799314f1e02cdc6c4ed9951cfba6b74a4f
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # ActiveCurrency
2
2
 
3
+ [![CircleCI](https://circleci.com/gh/sunny/active_currency.svg?style=svg)](https://circleci.com/gh/sunny/active_currency)
4
+
3
5
  Rails plugin to retrieve and store the currency rates daily to integrate
4
6
  with the `money-rails` gem.
5
7
 
@@ -15,10 +17,9 @@ provides the following advantages:
15
17
  - Choose how often you want to check for a currency (daily for example).
16
18
  - Your users do not suffer the cost of making calls to the bank rates API.
17
19
  - Your app does not go down when the bank rates API does.
18
- - When fetching the current rate, it uses your application cache in order not
19
- to have to do a database query.
20
20
 
21
- To fetch the rates, it uses the [eu_central_bank] gem.
21
+ To fetch the rates, it uses the [eu_central_bank] gem and uses your application
22
+ cache when getting the current rate in order to save on database queries.
22
23
 
23
24
  ## Usage
24
25
 
@@ -29,13 +30,13 @@ like `sidekiq-scheduler` or `whenever`) with the currencies you want to store:
29
30
  ActiveCurrency::AddRates.call(%w[EUR USD])
30
31
  ```
31
32
 
32
- You can then exchange money by using the Money gem:
33
+ You can then exchange money by using the Money gem helpers:
33
34
 
34
35
  ```rb
35
36
  10.to_money('EUR').exchange_to('USD').cents
36
37
  ```
37
38
 
38
- Or look up the currency rate:
39
+ If you need to look up the previous currency rates:
39
40
 
40
41
  ```rb
41
42
  ActiveCurrency::Rate.value_for('EUR', 'USD', 1.month.ago)
@@ -69,13 +70,13 @@ the currency rates and fill it for the first time.
69
70
  In your app test suite you may not want to have to fill your database to be
70
71
  able to exchange currencies.
71
72
 
72
- For that, you should use a fake rate store in `config/initializers/money.rb`:
73
+ For that, you can use a fake rate store in your `rails_helper.rb`:
73
74
 
74
75
  ```rb
75
- if Rails.env.test?
76
+ MoneyRails.configure do |config|
76
77
  rate_store = Money::RatesStore::Memory.new.tap do |store|
77
- store.add_rate('USD', 'EUR', 0.5)
78
- store.add_rate('EUR', 'USD', 1.5)
78
+ store.add_rate('USD', 'EUR', 1.5)
79
+ store.add_rate('EUR', 'USD', 1.4)
79
80
  end
80
81
  config.default_bank = Money::Bank::VariableExchange.new(rate_store)
81
82
  end
@@ -86,10 +87,18 @@ end
86
87
  Please file issues and pull requests
87
88
  [on GitHub](https://github.com/sunny/active_currency).
88
89
 
89
- In developemnt, launch specs and code linter by calling:
90
+ ## Development
91
+
92
+ Install:
93
+
94
+ ```sh
95
+ BUNDLE_GEMFILE=Gemfile-rails4.2 bundle install
96
+ ```
97
+
98
+ Launch specs and linters:
90
99
 
91
100
  ```sh
92
- BUNDLE_GEMFILE=Gemfile-rails3.2 bundle exec rspec && bundle exec rake
101
+ BUNDLE_GEMFILE=Gemfile-rails4.2 bundle exec rake
93
102
  ```
94
103
 
95
104
  ## License
@@ -2,12 +2,13 @@
2
2
 
3
3
  module ActiveCurrency
4
4
  class Rate < ActiveRecord::Base
5
- validates :from,
6
- :to,
7
- presence: true
5
+ validates :from, presence: true
6
+ validates :to, presence: true
8
7
  validates :value, numericality: { greater_than: 0 }
9
8
 
10
- scope :before, ->(date) { where(arel_table[:created_at].lt(date)) }
9
+ scope :before, ->(date) {
10
+ where(arel_table[:created_at].lt(date))
11
+ }
11
12
 
12
13
  def self.value_for(from, to, date = nil)
13
14
  scope = date ? before(date) : all_scope
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_currency/migration'
4
+
3
5
  class CreateActiveCurrencyRates < ActiveCurrency::Migration
4
6
  def change
5
7
  create_table :active_currency_rates do |t|
@@ -9,6 +11,8 @@ class CreateActiveCurrencyRates < ActiveCurrency::Migration
9
11
  t.datetime :created_at
10
12
  end
11
13
 
12
- add_index :active_currency_rates, %i[from to created_at]
14
+ add_index :active_currency_rates,
15
+ %i[from to created_at],
16
+ name: 'index_active_currency_rates'
13
17
  end
14
18
  end
@@ -3,7 +3,6 @@
3
3
  require 'money-rails'
4
4
  require 'eu_central_bank'
5
5
 
6
- require 'active_currency/migration'
7
6
  require 'active_currency/engine'
8
7
  require 'active_currency/database_store'
9
8
  require 'active_currency/cacheable_store'
@@ -8,16 +8,8 @@ module ActiveCurrency
8
8
  end
9
9
 
10
10
  def call
11
- bank = EuCentralBank.new
12
- bank.update_rates
13
-
14
- from = 'EUR'
15
- currencies.map(&:to_s).each do |to|
16
- next if to == from
17
-
18
- rate = bank.get_rate(from, to)
19
- store.add_rate(from, to, rate)
20
- store.add_rate(to, from, 1 / rate)
11
+ other_currencies.each do |to|
12
+ store_rate(from, to)
21
13
  end
22
14
  end
23
15
 
@@ -31,8 +23,31 @@ module ActiveCurrency
31
23
  @currencies.map(&:to_s).map(&:upcase)
32
24
  end
33
25
 
26
+ def other_currencies
27
+ currencies - [from]
28
+ end
29
+
30
+ def from
31
+ 'EUR'
32
+ end
33
+
34
34
  def store
35
35
  @store ||= ActiveCurrency::RateStore.new
36
36
  end
37
+
38
+ def bank
39
+ @bank ||= EuCentralBank.new.tap(&:update_rates)
40
+ end
41
+
42
+ def store_rate(from, to)
43
+ rate = bank.get_rate(from, to)
44
+
45
+ if rate.nil? || rate.zero?
46
+ raise "Bank rate must be set but bank returned #{rate.inpsect}"
47
+ end
48
+
49
+ store.add_rate(from, to, rate)
50
+ store.add_rate(to, from, 1.fdiv(rate))
51
+ end
37
52
  end
38
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCurrency
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_currency
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sunny Ripert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-02 00:00:00.000000000 Z
11
+ date: 2019-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -54,6 +54,20 @@ dependencies:
54
54
  version: 1.3.1
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.6
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.6
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - ">="
@@ -67,7 +81,7 @@ dependencies:
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
- name: rspec-rails
84
+ name: rspec_junit_formatter
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="