diff --git a/.rubocop.yml b/.rubocop.yml index a8a824c..88fa651 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,8 @@ +AllCops: + TargetRubyVersion: 2.0 + SuggestExtensions: false + NewCops: 'disable' + Metrics/BlockLength: Exclude: - '*.gemspec' @@ -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 diff --git a/Gemfile b/Gemfile index b0c87b1..7f4f5e9 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/lib/money/bank/currencylayer_bank.rb b/lib/money/bank/currencylayer_bank.rb index 191ea1b..a40615a 100644 --- a/lib/money/bank/currencylayer_bank.rb +++ b/lib/money/bank/currencylayer_bank.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 # frozen_string_literal: true require 'open-uri' @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/money-currencylayer-bank.gemspec b/money-currencylayer-bank.gemspec index 26ed488..49874ca 100644 --- a/money-currencylayer-bank.gemspec +++ b/money-currencylayer-bank.gemspec @@ -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'] @@ -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 diff --git a/test/currencylayer_bank_test.rb b/test/currencylayer_bank_test.rb index ea8770f..8bcb362 100644 --- a/test/currencylayer_bank_test.rb +++ b/test/currencylayer_bank_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 # frozen_string_literal: true require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) @@ -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 @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 708a378..c7d91e8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 # frozen_string_literal: true require 'minitest/autorun' @@ -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