Skip to content

Commit

Permalink
Bump to version 0.5.8.
Browse files Browse the repository at this point in the history
  • Loading branch information
phlegx committed Feb 15, 2022
1 parent 236f246 commit 02b65cc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Metrics/ClassLength:
Exclude:
- 'lib/money/bank/currencylayer_bank.rb'

Metrics/MethodLength:
Max: 12

Style/RedundantFreeze:
Exclude:
- 'lib/money/bank/currencylayer_bank.rb'
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
[![Gem Version](https://badge.fury.io/rb/money-currencylayer-bank.svg)](https://rubygems.org/gems/money-currencylayer-bank)
[![Gem](https://img.shields.io/gem/dt/money-currencylayer-bank.svg?maxAge=2592000)](https://rubygems.org/gems/money-currencylayer-bank)
[![Build Status](https://secure.travis-ci.org/phlegx/money-currencylayer-bank.svg?branch=master)](https://travis-ci.org/phlegx/money-currencylayer-bank)
[![Code Climate](http://img.shields.io/codeclimate/github/phlegx/money-currencylayer-bank.svg)](https://codeclimate.com/github/phlegx/money-currencylayer-bank)
[![Code Climate](https://codeclimate.com/github/phlegx/money-currencylayer-bank.svg)](https://codeclimate.com/github/phlegx/money-currencylayer-bank)
[![Inline Docs](http://inch-ci.org/github/phlegx/money-currencylayer-bank.svg?branch=master)](http://inch-ci.org/github/phlegx/money-currencylayer-bank)
[![Dependency Status](https://gemnasium.com/phlegx/money-currencylayer-bank.svg)](https://gemnasium.com/phlegx/money-currencylayer-bank)
[![License](https://img.shields.io/github/license/phlegx/money-currencylayer-bank.svg)](http://opensource.org/licenses/MIT)

A gem that calculates the exchange rate using published rates from
Expand Down Expand Up @@ -92,6 +91,9 @@ mclb.update_rates
# Be sure to define the cache first before updating the rates.
# mclb.update_rates(true)

# Set money rounding mode.
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN

# Set money default bank to Currencylayer bank.
Money.default_bank = mclb
~~~
Expand Down
8 changes: 4 additions & 4 deletions lib/money/bank/currencylayer_bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def read_from_url
# Opens an url and reads the content
# @return [String] unparsed JSON content
def open_url
::OpenURI.open_uri(source_url).read
URI.open(source_url).read
rescue OpenURI::HTTPError
''
end
Expand Down Expand Up @@ -274,11 +274,11 @@ def exchange_rates(straight = false)
raw_rates_careful
end
if rates.key?('quotes')
@rates = rates['quotes']
@rates = rates['quotes']
elsif rates.key?('error')
raise Error.new(rates.dig('error', 'info'))
raise Error, rates.dig('error', 'info')
else
raise Error.new('Unknown situation')
raise Error, 'Unknown rates situation!'
end
end

Expand Down
2 changes: 1 addition & 1 deletion money-currencylayer-bank.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'money-currencylayer-bank'
s.version = '0.5.7'
s.version = '0.5.8'
s.date = Time.now.utc.strftime('%Y-%m-%d')
s.homepage = "http://github.com/phlegx/#{s.name}"
s.authors = ['Egon Zemmer']
Expand Down
53 changes: 27 additions & 26 deletions test/currencylayer_bank_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))

describe Money::Bank::CurrencylayerBank do
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
subject { Money::Bank::CurrencylayerBank.new }
let(:url) { Money::Bank::CurrencylayerBank::CL_URL }
let(:secure_url) { Money::Bank::CurrencylayerBank::CL_SECURE_URL }
Expand All @@ -30,12 +31,12 @@
describe 'without rates' do
it 'able to exchange a money to its own currency even without rates' do
money = Money.new(0, 'USD')
subject.exchange_with(money, 'USD').must_equal money
_(subject.exchange_with(money, 'USD')).must_equal money
end

it "raise if it can't find an exchange rate" do
money = Money.new(0, 'USD')
proc { subject.exchange_with(money, 'SSP') }
_(proc { subject.exchange_with(money, 'SSP') })
.must_raise Money::Bank::UnknownRate
end
end
Expand All @@ -47,17 +48,17 @@

it 'should be able to exchange money from USD to a known exchange rate' do
money = Money.new(100, 'USD')
subject.exchange_with(money, 'BBD').must_equal Money.new(200, 'BBD')
_(subject.exchange_with(money, 'BBD')).must_equal Money.new(200, 'BBD')
end

it 'should be able to exchange money from a known exchange rate to USD' do
money = Money.new(200, 'BBD')
subject.exchange_with(money, 'USD').must_equal Money.new(100, 'USD')
_(subject.exchange_with(money, 'USD')).must_equal Money.new(100, 'USD')
end

it "should raise if it can't find an exchange rate" do
money = Money.new(0, 'USD')
proc { subject.exchange_with(money, 'SSP') }
_(proc { subject.exchange_with(money, 'SSP') })
.must_raise Money::Bank::UnknownRate
end
end
Expand Down Expand Up @@ -87,21 +88,21 @@
initial_size = File.read(temp_cache_path).size
stub(subject).open_url { '' }
subject.update_rates
File.read(temp_cache_path).size.must_equal initial_size
_(File.read(temp_cache_path).size).must_equal initial_size
end

it 'should not break an existing file if save returns json without rates' do
initial_size = File.read(temp_cache_path).size
stub(subject).open_url { '{ "error": "An error" }' }
subject.update_rates
File.read(temp_cache_path).size.must_equal initial_size
_(File.read(temp_cache_path).size).must_equal initial_size
end

it 'should not break an existing file if save returns a invalid json' do
initial_size = File.read(temp_cache_path).size
stub(subject).open_url { '{ invalid_json: "An error" }' }
subject.update_rates
File.read(temp_cache_path).size.must_equal initial_size
_(File.read(temp_cache_path).size).must_equal initial_size
end
end

Expand All @@ -114,7 +115,7 @@

it 'should get from url' do
subject.update_rates
subject.rates.wont_be_empty
_(subject.rates).wont_be_empty
end
end

Expand All @@ -126,7 +127,7 @@
end

it 'should raise an error if invalid path is given' do
proc { subject.update_rates }.must_raise Money::Bank::InvalidCache
_(proc { subject.update_rates }).must_raise Money::Bank::InvalidCache
end
end

Expand All @@ -146,40 +147,40 @@
it 'should get from url normally' do
stub(subject).source_url { data_path }
subject.update_rates
subject.rates.wont_be_empty
_(subject.rates).wont_be_empty
end

it 'should save from url and get from cache' do
stub(subject).source_url { data_path }
subject.update_rates
@global_rates.wont_be_empty
_(@global_rates).wont_be_empty
dont_allow(subject).source_url
subject.update_rates
subject.rates.wont_be_empty
_(subject.rates).wont_be_empty
end
end

describe '#secure_connection' do
it "should use the non-secure http url if secure_connection isn't set" do
subject.secure_connection = nil
subject.access_key = TEST_ACCESS_KEY
subject.source_url.must_equal "#{url}?source=#{source}&"\
_(subject.source_url).must_equal "#{url}?source=#{source}&"\
"access_key=#{TEST_ACCESS_KEY}"
end

it 'should use the non-secure http url if secure_connection is false' do
subject.secure_connection = false
subject.access_key = TEST_ACCESS_KEY
subject.source_url.must_equal "#{url}?source=#{source}&"\
_(subject.source_url).must_equal "#{url}?source=#{source}&"\
"access_key=#{TEST_ACCESS_KEY}"
end

it 'should use the secure https url if secure_connection is set to true' do
subject.secure_connection = true
subject.access_key = TEST_ACCESS_KEY
subject.source_url.must_equal "#{secure_url}?source=#{source}&"\
_(subject.source_url).must_equal "#{secure_url}?source=#{source}&"\
"access_key=#{TEST_ACCESS_KEY}"
subject.source_url.must_include 'https://'
_(subject.source_url).must_include 'https://'
end
end

Expand Down Expand Up @@ -214,7 +215,7 @@
subject.add_rate('USD', 'WTF', 2)
subject.add_rate('WTF', 'USD', 2)
subject.exchange_with(5000.to_money('WTF'), 'USD').cents
subject.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
_(subject.exchange_with(5000.to_money('WTF'), 'USD').cents).wont_equal 0
end
end
end
Expand All @@ -226,7 +227,7 @@
end

it 'should raise an error if no access key is set' do
proc { subject.update_rates }.must_raise Money::Bank::NoAccessKey
_(proc { subject.update_rates }).must_raise Money::Bank::NoAccessKey
end
end

Expand All @@ -250,19 +251,19 @@
describe 'when the ttl has expired' do
it 'should update the rates' do
Timecop.freeze(subject.rates_timestamp + 1000) do
subject.get_rate('USD', 'EUR').must_equal @old_usd_eur_rate
_(subject.get_rate('USD', 'EUR')).must_equal @old_usd_eur_rate
end
Timecop.freeze(subject.rates_timestamp + 1001) do
subject.get_rate('USD', 'EUR').wont_equal @old_usd_eur_rate
subject.get_rate('USD', 'EUR').must_equal @new_usd_eur_rate
_(subject.get_rate('USD', 'EUR')).wont_equal @old_usd_eur_rate
_(subject.get_rate('USD', 'EUR')).must_equal @new_usd_eur_rate
end
end

it 'updates the next expiration time' do
Timecop.freeze(subject.rates_timestamp + 1001) do
exp_time = subject.rates_timestamp + 1000
subject.expire_rates!
subject.rates_expiration.must_equal exp_time
_(subject.rates_expiration).must_equal exp_time
end
end
end
Expand All @@ -272,7 +273,7 @@
subject.update_rates
exp_time = subject.rates_expiration
subject.expire_rates!
subject.rates_expiration.must_equal exp_time
_(subject.rates_expiration).must_equal exp_time
end
end
end
Expand All @@ -291,12 +292,12 @@
it 'should return 1970-01-01 datetime if no rates' do
stub(subject).open_url { '' }
subject.update_rates
subject.rates_timestamp.must_equal Time.at(0)
_(subject.rates_timestamp).must_equal Time.at(0)
end

it 'should return a Time object' do
subject.update_rates
subject.rates_timestamp.class.must_equal Time
_(subject.rates_timestamp.class).must_equal Time
end
end
end

0 comments on commit 02b65cc

Please sign in to comment.