crypto_arbitrer 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/crypto_arbitrer/version.rb +1 -1
- data/lib/crypto_arbitrer.rb +24 -9
- data/spec/base_spec.rb +10 -0
- data/spec/mocks/btce_btc_usd.html +519 -505
- metadata +2 -2
data/lib/crypto_arbitrer.rb
CHANGED
@@ -5,6 +5,14 @@ require "open-uri"
|
|
5
5
|
|
6
6
|
module CryptoArbitrer
|
7
7
|
class Base
|
8
|
+
def self.btc_usd_strategy
|
9
|
+
@btc_usd_strategy || :mtgox
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.btc_usd_strategy=(strategy)
|
13
|
+
@btc_usd_strategy = strategy
|
14
|
+
end
|
15
|
+
|
8
16
|
# Quasi constant, all supported fiat currencies.
|
9
17
|
def self.supported_fiat
|
10
18
|
%w(usd ars uyu brl clp sgd eur vef)
|
@@ -68,9 +76,13 @@ module CryptoArbitrer
|
|
68
76
|
# The returned hash has 'sell' and 'buy' keys for the different prices,
|
69
77
|
# the prices mean at which price you could buy and sell from them, respectively.
|
70
78
|
def fetch_btc_usd
|
71
|
-
|
72
|
-
|
73
|
-
|
79
|
+
if self.class.btc_usd_strategy == :mtgox
|
80
|
+
response = open('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast').read
|
81
|
+
json = JSON.parse(response)['data']
|
82
|
+
{'sell' => json['sell']['value'].to_f, 'buy' => json['buy']['value'].to_f}
|
83
|
+
else
|
84
|
+
parse_btce_prices open("https://btc-e.com/exchange/btc_usd").read
|
85
|
+
end
|
74
86
|
end
|
75
87
|
derive_reversal(:btc, :usd)
|
76
88
|
|
@@ -93,15 +105,18 @@ module CryptoArbitrer
|
|
93
105
|
non_btc_cryptos = supported_cryptos - ['btc']
|
94
106
|
non_btc_cryptos.each do |currency|
|
95
107
|
define_method("fetch_#{currency}_btc") do
|
96
|
-
|
97
|
-
response =~ /<span id=.max_price..(.*?)..span./
|
98
|
-
sell = $1.to_f
|
99
|
-
response =~ /<span id=.min_price..(.*?)..span./
|
100
|
-
buy = $1.to_f
|
101
|
-
{'sell' => sell, 'buy' => buy}
|
108
|
+
parse_btce_prices open("https://btc-e.com/exchange/#{currency}_btc").read
|
102
109
|
end
|
103
110
|
derive_reversal(currency, :btc)
|
104
111
|
end
|
112
|
+
|
113
|
+
def parse_btce_prices(response)
|
114
|
+
response =~ /<span id=.max_price..(.*?)..span./
|
115
|
+
sell = $1.to_f
|
116
|
+
response =~ /<span id=.min_price..(.*?)..span./
|
117
|
+
buy = $1.to_f
|
118
|
+
{'sell' => sell, 'buy' => buy}
|
119
|
+
end
|
105
120
|
|
106
121
|
# Fiat prices are fetch from rate-exchange.appspot.com but there are no buy/sell prices.
|
107
122
|
# We use USD as the common denominator for all fiat currencies.
|
data/spec/base_spec.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require_relative '../lib/crypto_arbitrer.rb'
|
2
2
|
require 'webmock/rspec'
|
3
3
|
require 'csv'
|
4
|
+
require 'ruby-debug'
|
4
5
|
RSpec.configure do |config|
|
5
6
|
config.include CryptoArbitrer::SpecHelper
|
6
7
|
end
|
@@ -51,6 +52,15 @@ describe CryptoArbitrer::Base do
|
|
51
52
|
subject.fetch_btc_usd
|
52
53
|
stub.should have_been_requested
|
53
54
|
end
|
55
|
+
|
56
|
+
it 'hits btce for the btc to usd rate when configured that way' do
|
57
|
+
CryptoArbitrer::Base.btc_usd_strategy = :btce
|
58
|
+
stub = stub_crypto_arbitrer_get("https://btc-e.com/exchange/btc_usd", "btce_btc_usd.html")
|
59
|
+
rate = subject.fetch_btc_usd
|
60
|
+
rate['buy'].should == 99.999
|
61
|
+
rate['sell'].should == 99.502
|
62
|
+
stub.should have_been_requested
|
63
|
+
end
|
54
64
|
|
55
65
|
it 'hits dolarparalelo for venezuelan bolivar' do
|
56
66
|
stub = stub_crypto_arbitrer_get('http://www.dolarparalelo.org/', 'dolarparalelo.html')
|