Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated currencylayer api #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rvm:
- 1.9.3
- 2.0.0
- rbx-19mode
- jruby-19mode
- 2.3.0
- 3.1.2
- rbx-3
- jruby-head
4 changes: 2 additions & 2 deletions currencylayer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "currencylayer"
spec.version = '0.0.2'
spec.version = '0.0.3'
spec.authors = ["Andrey Skuratovsky"]
spec.email = ["[email protected]"]
spec.summary = "Access to the currencylayer.com online exchange rates"
Expand All @@ -20,6 +20,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec", ">= 3.0.0"
spec.add_development_dependency "timecop"
spec.add_development_dependency "webmock"
spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "bundler", "~> 2.3"
spec.add_development_dependency "rake", "~> 10.0"
end
15 changes: 8 additions & 7 deletions lib/money/bank/currencylayer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def message
class Currencylayer < Money::Bank::VariableExchange
include OnTimePatch

# Host of service jsonrates
SERVICE_HOST = "apilayer.net"
# new host for currencylayer.com API
SERVICE_HOST = "api.apilayer.com"

# Relative path of jsonrates api
SERVICE_PATH = "/api/live"
# Relative path of Exchange api
SERVICE_PATH = "/currency_data/live"

# accessor of access_key of jsonrates.com service
attr_accessor :access_key
Expand Down Expand Up @@ -283,7 +283,7 @@ def add_rate_with_time(from, to, rate)
#
# @return [Numeric]
def set_rate_with_time(from, to, rate)
rate_d = BigDecimal.new(rate.to_s)
rate_d = BigDecimal(rate.to_s)

self.store.transaction {
self.store.add_rate(from, to, { rate: rate_d, created_at: Time.now })
Expand Down Expand Up @@ -320,10 +320,11 @@ def fetch_rate(from, to)
# Performs request on uri or raise exception message with RequestError
#
# @param [String] uri Requested uri
# @param [Hash] apikey Access key
#
# @return [String]
def perform_request(uri)
uri.read
uri.read('apikey' => access_key)
rescue Exception => e
raise RequestError, e.message
end
Expand Down Expand Up @@ -354,7 +355,7 @@ def extract_rate(data, from, to)
request_hash = JSON.parse(data)
success = request_hash['success']
raise RequestError, request_hash['error']['info'] unless success
BigDecimal.new(request_hash['quotes']["#{from}#{to}"].to_s)
BigDecimal(request_hash['quotes']["#{from}#{to}"].to_s)
end
end
end
Expand Down
31 changes: 25 additions & 6 deletions spec/currencylayer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
include StubHelper

describe "Currencylayer" do
let(:bank) { Money::Bank::Currencylayer.new }
Expand All @@ -18,14 +19,13 @@
describe '.get_rate' do
it 'returns rate' do
uri = bank.send(:build_uri, 'USD', 'EUR').to_s
stub_request(:get, uri).to_return( :status => 200,
:body => '{"success":true,"terms":"https:\/\/currencylayer.com\/terms","privacy":"https:\/\/currencylayer.com\/privacy","timestamp":1434443053,"source":"USD","quotes":{"USDEUR":0.887701}}')
stub_request(:get, uri).to_return(get_rate_USDEUR_success)

bank.flush_rates

rate = bank.get_rate('USD', 'EUR')

expect(rate).to eq(BigDecimal.new("0.887701"))
expect(rate).to eq(BigDecimal("0.947553e0"))
end

context "in careful mode" do
Expand All @@ -40,11 +40,11 @@

uri = bank.send(:build_uri, 'USD', 'EUR').to_s

stub_request(:get, uri).to_return(:status => 200, :body => '{"success":false,"error":{"code":202,"info":"You have provided one or more invalid Currency Codes. [Required format: currencies=EUR,USD,GBP,...]"}}')
stub_request(:get, uri).to_return(get_rate_USDEUR_failure)

rate = bank.get_rate('USD', 'EUR')

expect(rate).to eq(BigDecimal.new("1.011"))
expect(rate).to eq(BigDecimal("1.011"))

Money::Bank::Currencylayer.rates_careful = false
end
Expand Down Expand Up @@ -72,7 +72,7 @@
it 'returns cached value if exception raised' do
bank.flush_rates
bank.add_rate("USD", "CAD", 32.231)
expect(bank.get_rate("USD", "CAD")).to eq (BigDecimal.new('32.231'))
expect(bank.get_rate("USD", "CAD")).to eq (BigDecimal('32.231'))
end
end

Expand Down Expand Up @@ -119,4 +119,23 @@
end
end
end

describe '.build_uri' do
it 'includes new host and path' do
uri = bank.send(:build_uri, 'USD', 'EUR')
expect(uri.host).to eq("api.apilayer.com")
expect(uri.path).to eq("/currency_data/live")
end
end

describe '.perform_request' do
it 'includes auth header' do
uri = bank.send(:build_uri, 'USD', 'EUR')
stub_request(:get, uri.to_s).to_return(get_rate_USDEUR_success)

bank.flush_rates
expect(uri).to receive(:read).with('apikey' => bank.access_key)
bank.send(:perform_request, uri)
end
end
end
37 changes: 37 additions & 0 deletions spec/helpers/stub_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module StubHelper
def get_rate_USDEUR_success
{
:status => 200,
:body => '{
"success": true,
"timestamp": 1670518143,
"source": "USD",
"quotes": {
"USDEUR": 0.947553
}
}'
}
end

def get_rate_USDEUR_failure
{
:status => 200,
:body => '{
"success": false,
"error": {
"code": 202,
"info": "You have provided one or more invalid Currency Codes. [Required format: currencies=EUR,USD,GBP,...]"
}
}'
}
end

def get_rate_unauthorized
{
:status => 401,
:body => '{
"message": "No API key found in request"
}'
}
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'timecop'
require 'money'
require 'money/bank/currencylayer'
require 'webmock/rspec'
require 'webmock/rspec'
require 'helpers/stub_helper'