Skip to content

Commit

Permalink
Closes #39 - Make all requests over https and deprecate `secure_conne…
Browse files Browse the repository at this point in the history
…ction`
  • Loading branch information
jaredmoody committed Mar 12, 2018
1 parent be8b4dd commit 444ce0d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 75 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ oxr.update_rates
# by default, they never expire, in this example 1 day.
oxr.ttl_in_seconds = 86400
# (optional)
# use https to fetch rates from Open Exchange Rates
# disabled by default to support free-tier users
# see https://openexchangerates.org/documentation#https
oxr.secure_connection = true
# (optional)
# set historical date of the rate
# see https://openexchangerates.org/documentation#historical-data
oxr.date = '2015-01-01'
Expand Down
28 changes: 8 additions & 20 deletions lib/money/bank/open_exchange_rates_bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,19 @@ class NoAppId < StandardError; end
# OpenExchangeRatesBank base class
class OpenExchangeRatesBank < Money::Bank::VariableExchange
VERSION = ::OpenExchangeRatesBank::VERSION
BASE_URL = 'http://openexchangerates.org/api/'.freeze
BASE_URL = 'https://openexchangerates.org/api/'.freeze

# OpenExchangeRates urls
OER_URL = URI.join(BASE_URL, 'latest.json')
OER_HISTORICAL_URL = URI.join(BASE_URL, 'historical/')
# OpenExchangeRates secure url
SECURE_OER_URL = OER_URL.clone
SECURE_OER_URL.scheme = 'https'
SECURE_OER_HISTORICAL_URL = OER_HISTORICAL_URL.clone
SECURE_OER_HISTORICAL_URL.scheme = 'https'

# Default base currency "base": "USD"
OE_SOURCE = 'USD'.freeze

# use https to fetch rates from Open Exchange Rates
# disabled by default to support free-tier users
#
# @example
# oxr.secure_connection = true
#
# @param [Boolean] true for https, false for http
# @return [Boolean] true for https, false for http
attr_accessor :secure_connection
# @deprecated secure_connection is deprecated and has no effect
def secure_connection=(*)
'secure_connection is deprecated and has no effect'
end

# As of the end of August 2012 all requests to the Open Exchange Rates
# API must have a valid app_id
Expand Down Expand Up @@ -178,7 +169,7 @@ def expire_rates
end

# Source url of openexchangerates
# defined with app_id and secure_connection
# defined with app_id
#
# @return [String] URL
def source_url
Expand Down Expand Up @@ -206,16 +197,13 @@ def oer_url
#
# @return [String] URL
def historical_url
url = OER_HISTORICAL_URL
url = SECURE_OER_HISTORICAL_URL if secure_connection
URI.join(url, "#{date}.json")
URI.join(OER_HISTORICAL_URL, "#{date}.json")
end

# Latest url
#
# @return [String] URL
def latest_url
return SECURE_OER_URL if secure_connection
OER_URL
end

Expand Down
54 changes: 4 additions & 50 deletions test/open_exchange_rates_bank_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
let(:oer_historical_url) do
Money::Bank::OpenExchangeRatesBank::OER_HISTORICAL_URL
end
let(:oer_secure_url) { Money::Bank::OpenExchangeRatesBank::SECURE_OER_URL }
let(:oer_historical_secure_url) do
Money::Bank::OpenExchangeRatesBank::SECURE_OER_HISTORICAL_URL
end

let(:temp_cache_path) do
data_file('tmp.json')
Expand Down Expand Up @@ -165,62 +161,20 @@
subject.date = '2015-01-01'
end

def historical_url
let(:historical_url) do
"#{oer_historical_url}#{subject.date}.json?app_id=#{TEST_APP_ID}"
end

def historical_secure_url
"#{oer_historical_secure_url}#{subject.date}.json?app_id=#{TEST_APP_ID}"
end

it 'should use the non-secure http url if secure_connection is nil' do
subject.secure_connection = nil
subject.source_url.must_equal historical_url
subject.source_url.must_include 'http://'
subject.source_url.must_include "/api/historical/#{subject.date}.json"
end

it 'should use the non-secure http url if secure_connection is false' do
subject.secure_connection = false
it 'should use the secure https url' do
subject.source_url.must_equal historical_url
subject.source_url.must_include 'http://'
subject.source_url.must_include "/api/historical/#{subject.date}.json"
end

it 'should use the secure https url if secure_connection is true' do
subject.secure_connection = true
subject.source_url.must_equal historical_secure_url
subject.source_url.must_include 'https://'
subject.source_url.must_include "/api/historical/#{subject.date}.json"
end
end

describe 'latest' do
def source_url
"#{oer_url}?app_id=#{TEST_APP_ID}"
end

def source_secure_url
"#{oer_secure_url}?app_id=#{TEST_APP_ID}"
end

it 'should use the non-secure http url if secure_connection is nil' do
subject.secure_connection = nil
subject.source_url.must_equal source_url
subject.source_url.must_include 'http://'
subject.source_url.must_include '/api/latest.json'
end

it 'should use the non-secure http url if secure_connection is false' do
subject.secure_connection = false
subject.source_url.must_equal source_url
subject.source_url.must_include 'http://'
subject.source_url.must_include '/api/latest.json'
end

it 'should use the secure https url if secure_connection is true' do
subject.secure_connection = true
subject.source_url.must_equal source_secure_url
it 'should use the secure https url' do
subject.source_url.must_equal "#{oer_url}?app_id=#{TEST_APP_ID}"
subject.source_url.must_include 'https://'
subject.source_url.must_include '/api/latest.json'
end
Expand Down

0 comments on commit 444ce0d

Please sign in to comment.