Skip to content

Commit

Permalink
Fix bug #14 reported by @stevenou.
Browse files Browse the repository at this point in the history
  • Loading branch information
phlegx committed Oct 17, 2022
1 parent b409f87 commit 408e834
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
13 changes: 12 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
AllCops:
TargetRubyVersion: 2.0
SuggestExtensions: false
NewCops: 'disable'

Metrics/BlockLength:
Exclude:
- '*.gemspec'
Expand All @@ -11,8 +16,14 @@ Metrics/MethodLength:
Max: 12

Metrics/LineLength:
Max: 100
Max: 120

Style/RedundantFreeze:
Exclude:
- 'lib/money/bank/currencylayer_bank.rb'

Style/OptionalBooleanParameter:
Enabled: false

Security/Open:
Enabled: false
6 changes: 0 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,3 @@
source 'https://rubygems.org'

gemspec

group :test, :development do
platforms :ruby_19, :jruby_19 do
gem 'tins', '~> 1.6'
end
end
18 changes: 10 additions & 8 deletions lib/money/bank/currencylayer_bank.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: UTF-8
# frozen_string_literal: true

require 'open-uri'
Expand All @@ -12,10 +11,10 @@ module RatesStore
# Memory class
class Memory
# Add method to reset the build in memory store
# @param [Hash] rt Optional initial exchange rate data.
# @param [Hash] rtd Optional initial exchange rate data.
# @return [Object] store.
def reset!(rt = {})
transaction { @index = rt }
def reset!(rtd = {})
transaction { @index = rtd }
end
end
end
Expand Down Expand Up @@ -123,6 +122,7 @@ def update_rates(straight = false)
currency = exchange_rate.first[3..-1]
rate = exchange_rate.last
next unless Money::Currency.find(currency)

add_rate(source, currency, rate)
add_rate(currency, source, 1.0 / rate)
end
Expand All @@ -135,7 +135,7 @@ def update_rates(straight = false)
# @param [String] to_currency Currency ISO code. ex. 'CAD'
# @param [Numeric] rate Rate to use when exchanging currencies.
# @return [Numeric] rate.
def add_rate(from_currency, to_currency, rate)
def add_rate(from_currency, to_currency, rate) # rubocop:disable Lint/UselessMethodDefinition
super
end

Expand Down Expand Up @@ -188,6 +188,7 @@ def stale?
# @return [String] the remote API url
def source_url
raise NoAccessKey if access_key.nil? || access_key.empty?

url = "#{currencylayer ? URL_CL : URL_AL}?source=#{source}"
url = url.sub('http:', 'https:') if secure_connection
url = "#{url}&access_key=#{access_key}" if currencylayer
Expand Down Expand Up @@ -218,9 +219,10 @@ def rates_timestamp
# @param text [String] parsed JSON content
# @return [String,Integer]
def store_in_cache(text)
if cache.is_a?(Proc)
case cache
when Proc
cache.call(text)
elsif cache.is_a?(String) || cache.is_a?(Pathname)
when String, Pathname
write_to_file(text)
end
end
Expand Down Expand Up @@ -258,7 +260,7 @@ def read_from_url
# Opens an url and reads the content
# @return [String] unparsed JSON content
def open_url
currencylayer ? URI.open(source_url).read : URI.open(source_url, apikey: access_key).read
currencylayer ? URI.open(source_url).read : URI.open(source_url, 'apikey' => access_key).read
rescue OpenURI::HTTPError
''
end
Expand Down
20 changes: 10 additions & 10 deletions 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.7.1'
s.version = '0.7.2'
s.date = Time.now.utc.strftime('%Y-%m-%d')
s.homepage = "http://github.com/phlegx/#{s.name}"
s.authors = ['Egon Zemmer']
Expand All @@ -17,16 +17,16 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.test_files = Dir.glob('test/*_test.rb')
s.require_paths = ['lib']
s.required_ruby_version = '>= 1.9.3'
s.rubygems_version = '1.3.7'
s.add_dependency 'money', '~> 6.7'
s.add_dependency 'monetize', '~> 1.4'
s.required_ruby_version = '>= 2.0'
s.required_rubygems_version = '>=1.3.7'
s.add_dependency 'json', '>= 1.8'
s.add_dependency 'monetize', '~> 1.4'
s.add_dependency 'money', '~> 6.7'
s.add_development_dependency 'inch', '~>0.8'
s.add_development_dependency 'minitest', '~> 5.8'
s.add_development_dependency 'minitest-line', '~> 0.6'
s.add_development_dependency 'rr', '~> 1.1'
s.add_development_dependency 'rake', '~>12.0'
s.add_development_dependency 'timecop', '~>0.8.1'
s.add_development_dependency 'rubocop', '~>0.49.1'
s.add_development_dependency 'inch', '~>0.7.1'
s.add_development_dependency 'rake', '~>13.0'
s.add_development_dependency 'rr', '~> 3.1'
s.add_development_dependency 'rubocop', '~>1.36.0'
s.add_development_dependency 'timecop', '~>0.9.5'
end
6 changes: 3 additions & 3 deletions test/currencylayer_bank_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: UTF-8
# frozen_string_literal: true

require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
Expand Down Expand Up @@ -81,7 +80,7 @@
it 'should allow update after save' do
begin
subject.update_rates
rescue
rescue e
assert false, 'Should allow updating after saving'
end
end
Expand Down Expand Up @@ -217,8 +216,9 @@
end

it 'should update itself with exchange rates from CurrencylayerBank' do
subject.rates.keys.each do |currency|
subject.rates.each_key do |currency|
next unless Money::Currency.find(currency)

subject.get_rate('USD', currency).must_be :>, 0
end
end
Expand Down
8 changes: 7 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: UTF-8
# frozen_string_literal: true

require 'minitest/autorun'
Expand All @@ -15,3 +14,10 @@
raise "Please add a valid access key to file #{TEST_ACCESS_KEY_PATH} or to " \
' TEST_ACCESS_KEY environment'
end

# Overwrite open-uri open method for tests with data from file instead of Url.
module URI
def self.open(name, *_rest, &_block)
Kernel.open(name)
end
end

0 comments on commit 408e834

Please sign in to comment.