spec/base_spec.rb in crypto_arbitrer-0.0.2 vs spec/base_spec.rb in crypto_arbitrer-0.0.3
- old
+ new
@@ -1,27 +1,16 @@
require_relative '../lib/crypto_arbitrer.rb'
require 'webmock/rspec'
require 'csv'
+RSpec.configure do |config|
+ config.include CryptoArbitrer::SpecHelper
+end
describe CryptoArbitrer::Base do
- def stub_get(url, mock_name)
- stub_request(:get, url)
- .to_return(body: open(File.expand_path("../mocks/#{mock_name}", __FILE__)).read)
- end
-
describe 'when matching corpus of conversion rates' do
before :each do
- stub_get("http://www.eldolarblue.net/getDolarBlue.php?as=json", 'dolarblue.json')
- stub_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
- stub_get('http://www.dolarparalelo.org/', 'dolarparalelo.html')
- %w(ltc nmc nvc trc ppc ftc cnc).each do |crypto|
- stub_get("https://btc-e.com/exchange/#{crypto}_btc", "btce_#{crypto}_btc.html")
- end
- %w(uyu brl clp sgd eur).each do |currency|
- stub_get("http://rate-exchange.appspot.com/currency?from=usd&to=#{currency}",
- "rate_exchange_usd_#{currency}.json")
- end
+ mock_crypto_arbitrer_requests!
end
CSV.read(File.expand_path('../conversions.csv', __FILE__), 'r').each do |from, to, buy, sell|
it "converts from #{from} to #{to}" do
rate = subject.fetch(from, to)
@@ -50,65 +39,65 @@
end
end
end
it 'hits dolarblue for usd to ars rate' do
- stub = stub_get("http://www.eldolarblue.net/getDolarBlue.php?as=json", 'dolarblue.json')
+ stub = stub_crypto_arbitrer_get("http://www.eldolarblue.net/getDolarBlue.php?as=json", 'dolarblue.json')
subject.fetch_usd_ars
stub.should have_been_requested
end
it 'hits mt.gox for the btc to usd rate' do
- stub = stub_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
+ stub = stub_crypto_arbitrer_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
subject.fetch_btc_usd
stub.should have_been_requested
end
it 'hits dolarparalelo for venezuelan bolivar' do
- stub = stub_get('http://www.dolarparalelo.org/', 'dolarparalelo.html')
+ stub = stub_crypto_arbitrer_get('http://www.dolarparalelo.org/', 'dolarparalelo.html')
subject.fetch_usd_vef
stub.should have_been_requested
end
it 'hits mt.gox and dolarblue for the btc to ars rate' do
- stub_ars = stub_get("http://www.eldolarblue.net/getDolarBlue.php?as=json", 'dolarblue.json')
- stub_btc = stub_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
+ stub_ars = stub_crypto_arbitrer_get("http://www.eldolarblue.net/getDolarBlue.php?as=json", 'dolarblue.json')
+ stub_btc = stub_crypto_arbitrer_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
subject.fetch_btc_ars
stub_ars.should have_been_requested
stub_btc.should have_been_requested
end
%w(ltc nmc nvc trc ppc ftc cnc).each do |currency|
it "hits btc-e for the #{currency} to btc rate" do
- stub = stub_get("https://btc-e.com/exchange/#{currency}_btc", "btce_#{currency}_btc.html")
+ stub = stub_crypto_arbitrer_get("https://btc-e.com/exchange/#{currency}_btc", "btce_#{currency}_btc.html")
subject.fetch(currency, 'btc')
subject.fetch('btc', currency)
stub.should have_been_requested.twice
end
end
%w(uyu brl clp sgd eur).each do |currency|
it "hits rate-exchange for the #{currency} to usd rate" do
- stub = stub_get("http://rate-exchange.appspot.com/currency?from=usd&to=#{currency}",
+ stub = stub_crypto_arbitrer_get("http://rate-exchange.appspot.com/currency?from=usd&to=#{currency}",
"rate_exchange_usd_#{currency}.json")
subject.fetch('usd', currency)
subject.fetch(currency, 'usd')
stub.should have_been_requested.twice
end
end
describe 'when caching remote calls' do
it 'does not cache anything by default' do
- stub = stub_get("http://www.eldolarblue.net/getDolarBlue.php?as=json", 'dolarblue.json')
+ stub = stub_crypto_arbitrer_get("http://www.eldolarblue.net/getDolarBlue.php?as=json", 'dolarblue.json')
2.times{ subject.fetch('usd', 'ars') }
stub.should have_been_requested.twice
end
it 'caches when a cache_backend has been set, stops caching afterwards' do
cache = {}
subject.class.cache_backend = lambda {|from, to, block| cache[[from,to]] ||= block.call }
- stub = stub_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
+ stub = stub_crypto_arbitrer_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
subject.fetch('btc', 'usd')
cache[['btc','usd']].should be_a(Hash)
subject.fetch('btc', 'usd')
stub.should have_been_requested.once
subject.class.cache_backend = nil
@@ -117,19 +106,19 @@
end
it 'does not cache for low level calls' do
cache = {}
subject.class.cache_backend = lambda {|from, to, block| cache[key] ||= block.call }
- stub = stub_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
+ stub = stub_crypto_arbitrer_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
subject.fetch_btc_usd
subject.fetch_btc_usd
stub.should have_been_requested.twice
end
it 'does not cache when using a force argument' do
cache = {}
subject.class.cache_backend = lambda {|from, to, block| cache[key] ||= block.call }
- stub = stub_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
+ stub = stub_crypto_arbitrer_get('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast', 'mtgox.json')
subject.fetch('btc', 'usd', true)
subject.fetch('btc', 'usd', true)
stub.should have_been_requested.twice
end
end