betfair 1.0.3 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -2
- data/README.md +23 -22
- data/betfair.gemspec +4 -3
- data/examples/README.md +3 -0
- data/lib/betfair/api.rb +193 -216
- data/lib/betfair/version.rb +1 -1
- data/spec/betfair/api_spec.rb +75 -68
- data/spec/spec_helper.rb +9 -1
- metadata +54 -25
- data/README_OLD +0 -170
- data/examples/bar.rb +0 -23
- data/examples/foo.rb +0 -42
- data/examples/simplebot.log +0 -4
- data/examples/simplebot/README.md +0 -51
- data/examples/simplebot/daemonize.rb +0 -8
- data/examples/simplebot/monit.conf +0 -6
- data/examples/simplebot/simplebot.rb +0 -170
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -161,9 +161,9 @@ The API PlaceBets service allows you to place multiple (1 to 60) bets on a singl
|
|
161
161
|
|
162
162
|
exchange_id = 1
|
163
163
|
bets = []
|
164
|
-
bets << { market_id: 12345,
|
164
|
+
bets << { market_id: 12345, selection_id: 58805, bet_type: 'B', price: 2.0, size: 2.0, asian_line_id: 0,
|
165
165
|
bet_category_type: 'E', bet_peristence_type: 'NONE', bsp_liability: 0 }
|
166
|
-
bets << { market_id: 12345,
|
166
|
+
bets << { market_id: 12345, selection_id: 1234, bet_type: 'L', price: 1.5, size: 2.0, asian_line_id: 0,
|
167
167
|
bet_category_type: 'E', bet_peristence_type: 'NONE', bsp_liability: 0 }
|
168
168
|
|
169
169
|
place_multiple_bets =
|
@@ -238,7 +238,7 @@ foo[100388290] returns
|
|
238
238
|
|
239
239
|
{ :market_id=>100388290, :market_name=>"Premiers 2012", :market_type=>"O", :market_status=>"ACTIVE", :event_date=>2012-03-24 16:20:00 +0800,
|
240
240
|
:menu_path=>"\\Australian Rules\\AFL 2012", :event_hierarchy=>"/61420/26759191/100388290", :bet_delay=>"0", :exchange_id=>2,
|
241
|
-
:iso3_country_code=>"AUS", :last_refresh=>2012-03-29 16:35:21 +0800, :
|
241
|
+
:iso3_country_code=>"AUS", :last_refresh=>2012-03-29 16:35:21 +0800, :number_of_selections=>18, :number_of_winners=>1,
|
242
242
|
:total_amount_matched=>193599.58, :bsp_market=>false, :turning_in_play=>false
|
243
243
|
}
|
244
244
|
|
@@ -255,7 +255,7 @@ foo.first returns
|
|
255
255
|
|
256
256
|
{ :market_id=>100388290, :market_name=>"Premiers 2012", :market_type=>"O", :market_status=>"ACTIVE", :event_date=>2012-03-24 08:20:00 UTC,
|
257
257
|
:menu_path=>"\\Australian Rules\\AFL 2012", :event_heirachy=>"/61420/26759191/100388290", :bet_delay=>0, :exchange_id=>2,
|
258
|
-
:iso3_country_code=>"AUS", :last_refresh=>2012-03-29 09:10:12 UTC, :
|
258
|
+
:iso3_country_code=>"AUS", :last_refresh=>2012-03-29 09:10:12 UTC, :number_of_selections=>18, :number_of_winners=>1,
|
259
259
|
:total_amount_matched=>193657.0, :bsp_market=>false, :turning_in_play=>false
|
260
260
|
}
|
261
261
|
|
@@ -263,7 +263,7 @@ foo.first returns
|
|
263
263
|
This helper sorts out a nice hash from the
|
264
264
|
|
265
265
|
market = bf.get_market(session_token, 2, 100388290)
|
266
|
-
foo = helpers.market_info(
|
266
|
+
foo = helpers.market_info(market)
|
267
267
|
|
268
268
|
Which returns
|
269
269
|
|
@@ -276,7 +276,7 @@ This helper cleans up the prices
|
|
276
276
|
foo = helpers.prices(prices)
|
277
277
|
|
278
278
|
## Combine ##
|
279
|
-
Use this to combine `
|
279
|
+
Use this to combine `selection_names` and prices from the #market_info and #prices helpers
|
280
280
|
|
281
281
|
market = bf.get_market(session_token, 2, 100388290)
|
282
282
|
prices = bf.get_market_prices_compressed(session_token, 2, 100388290)
|
@@ -284,14 +284,14 @@ Use this to combine `runner_names` and prices from the #market_info and #prices
|
|
284
284
|
|
285
285
|
foo.first returns
|
286
286
|
|
287
|
-
{ :
|
287
|
+
{ :selection_id=>39983, :selection_name=>"Collingwood Magpies", :market_id=>100388290, :market_type_id=>61420,
|
288
288
|
:prices_string=>"39983~0~89899.79~4.2~~~false~~~~|4.2~430.35~L~1~4.1~311.51~L~2~3.85~4.75~L~3~|4.4~155.46~B~1~4.6~230.69~B~2~5.9~100.3~B~3~",
|
289
|
-
:
|
289
|
+
:selection_matched=>89899.79, :last_back_price=>4.2, :wom=>0.6054936499440416, :b1=>4.2, :b1_available=>430.35, :b2=>4.1, :b2_available=>311.51,
|
290
290
|
:b3=>3.85, :b3_available=>4.75, :l1=>4.4, :l1_available=>155.46, :l2=>4.6, :l2_available=>230.69, :l3=>5.9, :l3_available=>100.3
|
291
291
|
}
|
292
292
|
|
293
293
|
## Details ##
|
294
|
-
Gets the `market_id`/`market_name` and the `
|
294
|
+
Gets the `market_id`/`market_name` and the `selection_id`/`selection_name`
|
295
295
|
|
296
296
|
market = bf.get_market(session_token, 2, 100388290)
|
297
297
|
foo = helpers.details(market)
|
@@ -299,15 +299,15 @@ Gets the `market_id`/`market_name` and the `runner_id`/`runner_name`
|
|
299
299
|
Which returns
|
300
300
|
|
301
301
|
{ :market_id=>100388290, :market_type_id=>61420,
|
302
|
-
:
|
303
|
-
{:
|
304
|
-
{:
|
305
|
-
{:
|
306
|
-
{:
|
307
|
-
{:
|
308
|
-
{:
|
309
|
-
{:
|
310
|
-
{:
|
302
|
+
:selections=>[{:selection_id=>39983, :selection_name=>"Collingwood Magpies"}, {:selection_id=>244664, :selection_name=>"Hawthorn Hawks"},
|
303
|
+
{:selection_id=>244663, :selection_name=>"Geelong Cats"}, {:selection_id=>244689, :selection_name=>"Carlton Blues"},
|
304
|
+
{:selection_id=>210344, :selection_name=>"West Coast Eagles"}, {:selection_id=>244666, :selection_name=>"Fremantle Dockers"},
|
305
|
+
{:selection_id=>244688, :selection_name=>"St Kilda Saints"}, {:selection_id=>244665, :selection_name=>"Sydney Swans"},
|
306
|
+
{:selection_id=>173363, :selection_name=>"Adelaide Crows"}, {:selection_id=>210343, :selection_name=>"Essendon Bombers"},
|
307
|
+
{:selection_id=>39986, :selection_name=>"Western Bulldogs"}, {:selection_id=>244667, :selection_name=>"Richmond Tigers"},
|
308
|
+
{:selection_id=>2013991, :selection_name=>"North Melbourne Kangaroos"}, {:selection_id=>39987, :selection_name=>"Melbourne Demons"},
|
309
|
+
{:selection_id=>39984, :selection_name=>"Brisbane Lions"}, {:selection_id=>217710, :selection_name=>"Port Adelaide Power"},
|
310
|
+
{:selection_id=>4997061, :selection_name=>"Gold Coast Suns"}, {:selection_id=>5149403, :selection_name=>"Greater Western Sydney Giants"}]
|
311
311
|
}
|
312
312
|
|
313
313
|
## Prices Complete ##
|
@@ -320,7 +320,7 @@ foo.first returns
|
|
320
320
|
|
321
321
|
[ 39983, {:selection_id=>39983, :order_index=>0, :total_amount_matched=>89899.79, :last_price_matched=>4.2, :handicap=>0.0,
|
322
322
|
:reduction_factor=>0.0, :vacant=>false, :far_sp_price=>0.0, :near_sp_price=>0.0, :actual_sp_price=>0.0, :prices_string=>nil,
|
323
|
-
:
|
323
|
+
:selection_matched=>0, :last_back_price=>0, :wom=>0.5516492637413943, :b1=>4.2, :b1_available=>429.06, :b2=>4.1, :b2_available=>310.58,
|
324
324
|
:b3=>3.85, :b3_available=>4.75, :l1=>4.3, :l1_available=>20.0, :l2=>4.4, :l2_available=>355.0, :l3=>4.6, :l3_available=>230.0}
|
325
325
|
]
|
326
326
|
|
@@ -329,7 +329,7 @@ foo.first returns
|
|
329
329
|
prices = bf.get_market_prices_compressed(session_token, 2, 100388290)
|
330
330
|
foo = helpers.price_string(prices, true)
|
331
331
|
|
332
|
-
{ :prices_string=>nil, :
|
332
|
+
{ :prices_string=>nil, :selection_matched=>0, :last_back_price=>0, :wom=>0.6054936499440416, :b1=>4.2, :b1_available=>430.35, :b2=>4.1, :b2_available=>311.51, :b3=>3.85,
|
333
333
|
:b3_available=>4.75, :l1=>4.4, :l1_available=>155.46, :l2=>4.6, :l2_available=>230.69, :l3=>5.9, :l3_available=>100.3
|
334
334
|
}
|
335
335
|
|
@@ -375,10 +375,11 @@ trying to set a threshold on when to place a bet.
|
|
375
375
|
* rake
|
376
376
|
|
377
377
|
## To Do ##
|
378
|
-
*
|
378
|
+
* Add gzip support option
|
379
|
+
* Add em-http support as per this once this gets pulled https://github.com/rubiii/httpi/pull/40
|
379
380
|
* The WOM of money in Helpers#price_string returns 0 if either all b1,b2,b3 or l1,l2,l3 are all 0
|
380
381
|
* Add some error checking to the Betfair::Helper methods
|
381
|
-
* Finish of the mash method, to return a nice hash of all market and
|
382
|
+
* Finish of the mash method, to return a nice hash of all market and selection info
|
382
383
|
* Write a spec for the mashed method
|
383
384
|
|
384
385
|
## Contribute ##
|
data/betfair.gemspec
CHANGED
@@ -18,9 +18,10 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_runtime_dependency 'savon'
|
21
|
+
s.add_runtime_dependency 'savon', '<= 0.8.6'
|
22
22
|
|
23
23
|
s.add_development_dependency 'rake'
|
24
24
|
s.add_development_dependency 'rspec'
|
25
|
-
s.add_development_dependency 'savon_spec'
|
26
|
-
|
25
|
+
s.add_development_dependency 'savon_spec', '= 0.1.5'
|
26
|
+
s.add_development_dependency 'mocha', '= 0.10.4'
|
27
|
+
end
|
data/examples/README.md
ADDED
data/lib/betfair/api.rb
CHANGED
@@ -4,8 +4,13 @@ module Betfair
|
|
4
4
|
|
5
5
|
## Some handy constants...
|
6
6
|
|
7
|
-
EXCHANGE_IDS = {
|
7
|
+
EXCHANGE_IDS = {
|
8
|
+
:aus => 2,
|
9
|
+
:uk => 1
|
10
|
+
}
|
11
|
+
|
8
12
|
PRODUCT_ID_FREE = 82
|
13
|
+
|
9
14
|
BET_TYPE_LAY = 'L'
|
10
15
|
BET_TYPE_BACK = 'B'
|
11
16
|
|
@@ -26,70 +31,70 @@ module Betfair
|
|
26
31
|
## Bet Placement API METHODS
|
27
32
|
#
|
28
33
|
|
29
|
-
def place_bet(session_token, exchange_id, market_id, selection_id, bet_type, price, size)
|
30
|
-
bf_bet = {
|
31
|
-
marketId
|
32
|
-
selectionId
|
33
|
-
betType
|
34
|
-
price
|
35
|
-
size
|
36
|
-
asianLineId
|
37
|
-
betCategoryType
|
38
|
-
betPersistenceType
|
39
|
-
bspLiability
|
34
|
+
def place_bet(session_token, exchange_id, market_id, selection_id, bet_type, price, size)
|
35
|
+
bf_bet = {
|
36
|
+
:marketId => market_id,
|
37
|
+
:selectionId => selection_id,
|
38
|
+
:betType => bet_type,
|
39
|
+
:price => price,
|
40
|
+
:size => size,
|
41
|
+
:asianLineId => 0,
|
42
|
+
:betCategoryType => 'E',
|
43
|
+
:betPersistenceType => 'NONE',
|
44
|
+
:bspLiability => 0
|
40
45
|
}
|
41
46
|
|
42
47
|
response = exchange(exchange_id).
|
43
48
|
session_request( session_token,
|
44
|
-
:placeBets,
|
49
|
+
:placeBets,
|
45
50
|
:place_bets_response,
|
46
|
-
bets
|
51
|
+
:bets => { 'PlaceBets' => [bf_bet] } )
|
47
52
|
|
48
53
|
return response.maybe_result( :bet_results, :place_bets_result )
|
49
54
|
end
|
50
55
|
|
51
56
|
|
52
|
-
def place_multiple_bets(session_token, exchange_id, bets)
|
57
|
+
def place_multiple_bets(session_token, exchange_id, bets)
|
53
58
|
bf_bets = []
|
54
59
|
bets.each do |bet|
|
55
|
-
bf_bets << {
|
56
|
-
marketId
|
57
|
-
selectionId
|
58
|
-
betType
|
59
|
-
price
|
60
|
-
size
|
61
|
-
asianLineId
|
62
|
-
betCategoryType
|
63
|
-
betPersistenceType
|
64
|
-
bspLiability
|
60
|
+
bf_bets << {
|
61
|
+
:marketId => bet[:market_id],
|
62
|
+
:selectionId => bet[:selection_id],
|
63
|
+
:betType => bet[:bet_type],
|
64
|
+
:price => bet[:price],
|
65
|
+
:size => bet[:size],
|
66
|
+
:asianLineId => bet[:asian_line_id],
|
67
|
+
:betCategoryType => bet[:bet_category_type],
|
68
|
+
:betPersistenceType => bet[:bet_peristence_type],
|
69
|
+
:bspLiability => bet[:bsp_liability]
|
65
70
|
}
|
66
71
|
end
|
67
72
|
|
68
73
|
response = exchange(exchange_id).
|
69
74
|
session_request( session_token,
|
70
|
-
:placeBets,
|
75
|
+
:placeBets,
|
71
76
|
:place_bets_response,
|
72
|
-
bets
|
77
|
+
:bets => { 'PlaceBets' => bf_bets } )
|
73
78
|
|
74
79
|
return response.maybe_result( :bet_results, :place_bets_result )
|
75
80
|
end
|
76
81
|
|
77
82
|
def update_bet(session_token, exchange_id, bet_id, new_bet_persitence_type, new_price, new_size, old_bet_persitance_type, old_price, old_size)
|
78
|
-
bf_bet = {
|
79
|
-
betId
|
80
|
-
newBetPersistenceType
|
81
|
-
newPrice
|
82
|
-
newSize
|
83
|
-
oldBetPersistenceType
|
84
|
-
oldPrice
|
85
|
-
oldSize
|
83
|
+
bf_bet = {
|
84
|
+
:betId => bet_id,
|
85
|
+
:newBetPersistenceType => new_bet_persitence_type,
|
86
|
+
:newPrice => new_price,
|
87
|
+
:newSize => new_size,
|
88
|
+
:oldBetPersistenceType => old_bet_persitance_type,
|
89
|
+
:oldPrice => old_price,
|
90
|
+
:oldSize => old_size
|
86
91
|
}
|
87
92
|
|
88
93
|
response = exchange(exchange_id).
|
89
94
|
session_request( session_token,
|
90
|
-
:updateBets,
|
95
|
+
:updateBets,
|
91
96
|
:update_bets_response,
|
92
|
-
bets
|
97
|
+
:bets => { 'UpdateBets' => [bf_bet] } )
|
93
98
|
|
94
99
|
return response.maybe_result( :bet_results, :update_bets_result )
|
95
100
|
end
|
@@ -97,47 +102,47 @@ module Betfair
|
|
97
102
|
def update_multiple_bets(session_token, exchange_id, bets)
|
98
103
|
bf_bets = []
|
99
104
|
bets.each do |bet|
|
100
|
-
bf_bets << {
|
101
|
-
betId
|
102
|
-
newBetPersistenceType
|
103
|
-
newPrice
|
104
|
-
newSize
|
105
|
-
oldBetPersistenceType
|
106
|
-
oldPrice
|
107
|
-
oldSize
|
105
|
+
bf_bets << {
|
106
|
+
:betId => bet[:bet_id],
|
107
|
+
:newBetPersistenceType => bet[:new_bet_persitence_type],
|
108
|
+
:newPrice => bet[:new_price],
|
109
|
+
:newSize => bet[:new_size],
|
110
|
+
:oldBetPersistenceType => bet[:old_bet_persitance_type],
|
111
|
+
:oldPrice => bet[:old_price],
|
112
|
+
:oldSize => bet[:old_size]
|
108
113
|
}
|
109
114
|
end
|
110
115
|
|
111
116
|
response = exchange(exchange_id).
|
112
117
|
session_request( session_token,
|
113
|
-
:updateBets,
|
118
|
+
:updateBets,
|
114
119
|
:update_bets_response,
|
115
|
-
bets
|
120
|
+
:bets => { 'UpdateBets' => bf_bets } )
|
116
121
|
|
117
|
-
return response.maybe_result( :bet_results, :update_bets_result )
|
122
|
+
return response.maybe_result( :bet_results, :update_bets_result )
|
118
123
|
end
|
119
124
|
|
120
125
|
def cancel_bet(session_token, exchange_id, bet_id)
|
121
|
-
bf_bet = { betId
|
126
|
+
bf_bet = { :betId => bet_id }
|
122
127
|
|
123
128
|
response = exchange(exchange_id).
|
124
129
|
session_request( session_token,
|
125
|
-
:cancelBets,
|
130
|
+
:cancelBets,
|
126
131
|
:cancel_bets_response,
|
127
|
-
bets
|
132
|
+
:bets => { 'CancelBets' => [bf_bet] } ) # "CancelBets" has to be a string, not a symbol!
|
128
133
|
|
129
134
|
return response.maybe_result( :bet_results, :cancel_bets_result )
|
130
135
|
end
|
131
136
|
|
132
137
|
def cancel_multiple_bets(session_token, exchange_id, bets)
|
133
138
|
bf_bets = []
|
134
|
-
bets.each { |bet_id| bf_bets << { betId
|
139
|
+
bets.each { |bet_id| bf_bets << { :betId => bet_id } }
|
135
140
|
|
136
141
|
response = exchange(exchange_id).
|
137
142
|
session_request( session_token,
|
138
|
-
:cancelBets,
|
143
|
+
:cancelBets,
|
139
144
|
:cancel_bets_response,
|
140
|
-
bets
|
145
|
+
:bets => { 'CancelBets' => bf_bets } ) # "CancelBets" has to be a string, not a symbol!
|
141
146
|
|
142
147
|
return response.maybe_result( :bet_results, :cancel_bets_result )
|
143
148
|
end
|
@@ -151,31 +156,31 @@ module Betfair
|
|
151
156
|
|
152
157
|
def get_mu_bets( session_token, exchange_id, market_id = 0, bet_status = 'MU', start_record = 0, record_count = 200, sort_order = 'ASC', order_by = 'PLACED_DATE') #, bet_ids = nil, , exclude_last_second = nil, matched_since = nil
|
153
158
|
response = exchange(exchange_id).
|
154
|
-
session_request( session_token,
|
155
|
-
:getMUBets,
|
159
|
+
session_request( session_token,
|
160
|
+
:getMUBets,
|
156
161
|
:get_mu_bets_response,
|
157
|
-
|
158
|
-
betStatus
|
159
|
-
|
160
|
-
marketId
|
161
|
-
|
162
|
-
orderBy
|
163
|
-
recordCount
|
164
|
-
sortOrder
|
165
|
-
startRecord
|
162
|
+
#:betIds => bet_ids,
|
163
|
+
:betStatus => bet_status,
|
164
|
+
#:excludeLastSecond => exclude_last_second,
|
165
|
+
:marketId => market_id,
|
166
|
+
#:matchedSince => matched_since,
|
167
|
+
:orderBy => order_by,
|
168
|
+
:recordCount => record_count,
|
169
|
+
:sortOrder => sort_order,
|
170
|
+
:startRecord => start_record
|
166
171
|
)
|
167
172
|
|
168
173
|
return response.maybe_result( :bets, :mu_bet )
|
169
174
|
end
|
170
175
|
|
171
176
|
|
172
|
-
def get_market(session_token, exchange_id, market_id, locale = nil)
|
177
|
+
def get_market(session_token, exchange_id, market_id, locale = nil)
|
173
178
|
response = exchange(exchange_id).
|
174
|
-
session_request( session_token,
|
175
|
-
:getMarket,
|
179
|
+
session_request( session_token,
|
180
|
+
:getMarket,
|
176
181
|
:get_market_response,
|
177
|
-
marketId
|
178
|
-
|
182
|
+
:marketId => market_id,
|
183
|
+
:locale => locale )
|
179
184
|
|
180
185
|
return response.maybe_result( :market )
|
181
186
|
end
|
@@ -184,10 +189,10 @@ module Betfair
|
|
184
189
|
def get_market_prices_compressed(session_token, exchange_id, market_id, currency_code = nil)
|
185
190
|
response = exchange(exchange_id).
|
186
191
|
session_request( session_token,
|
187
|
-
:getMarketPricesCompressed,
|
192
|
+
:getMarketPricesCompressed,
|
188
193
|
:get_market_prices_compressed_response,
|
189
|
-
marketId
|
190
|
-
currencyCode
|
194
|
+
:marketId => market_id,
|
195
|
+
:currencyCode => currency_code )
|
191
196
|
|
192
197
|
return response.maybe_result( :market_prices )
|
193
198
|
end
|
@@ -196,9 +201,9 @@ module Betfair
|
|
196
201
|
def get_active_event_types(session_token, locale = nil)
|
197
202
|
response = @global_service.
|
198
203
|
session_request( session_token,
|
199
|
-
:getActiveEventTypes,
|
204
|
+
:getActiveEventTypes,
|
200
205
|
:get_active_event_types_response,
|
201
|
-
locale
|
206
|
+
:locale => locale )
|
202
207
|
|
203
208
|
return response.maybe_result( :event_type_items, :event_type )
|
204
209
|
end
|
@@ -206,14 +211,14 @@ module Betfair
|
|
206
211
|
|
207
212
|
def get_all_markets(session_token, exchange_id, event_type_ids = nil, locale = nil, countries = nil, from_date = nil, to_date = nil)
|
208
213
|
response = exchange(exchange_id).
|
209
|
-
session_request( session_token,
|
210
|
-
:getAllMarkets,
|
214
|
+
session_request( session_token,
|
215
|
+
:getAllMarkets,
|
211
216
|
:get_all_markets_response,
|
212
|
-
eventTypeIds
|
213
|
-
locale
|
214
|
-
countries
|
215
|
-
fromDate
|
216
|
-
toDate
|
217
|
+
:eventTypeIds => { 'int' => event_type_ids },
|
218
|
+
:locale => locale,
|
219
|
+
:countries => { 'country' => countries },
|
220
|
+
:fromDate => from_date,
|
221
|
+
:toDate => to_date )
|
217
222
|
|
218
223
|
return response.maybe_result( :market_data )
|
219
224
|
end
|
@@ -221,22 +226,22 @@ module Betfair
|
|
221
226
|
|
222
227
|
def get_account_funds( session_token, exchange_id )
|
223
228
|
response = exchange(exchange_id).
|
224
|
-
session_request( session_token,
|
225
|
-
:getAccountFunds,
|
229
|
+
session_request( session_token,
|
230
|
+
:getAccountFunds,
|
226
231
|
:get_account_funds_response )
|
227
232
|
|
228
233
|
return response.maybe_result
|
229
234
|
end
|
230
235
|
|
231
236
|
def login(username, password, product_id, vendor_software_id, location_id, ip_address)
|
232
|
-
response = @global_service.request( :login,
|
233
|
-
:login_response,
|
234
|
-
username
|
235
|
-
password
|
236
|
-
productId
|
237
|
-
vendorSoftwareId
|
238
|
-
locationId
|
239
|
-
ipAddress
|
237
|
+
response = @global_service.request( :login,
|
238
|
+
:login_response,
|
239
|
+
:username => username,
|
240
|
+
:password => password,
|
241
|
+
:productId => product_id,
|
242
|
+
:vendorSoftwareId => vendor_software_id,
|
243
|
+
:locationId => location_id,
|
244
|
+
:ipAddress => ip_address )
|
240
245
|
|
241
246
|
return response.maybe_result( :header, :session_token )
|
242
247
|
end
|
@@ -247,19 +252,21 @@ module Betfair
|
|
247
252
|
def keep_alive(session_token)
|
248
253
|
response = @global_service.
|
249
254
|
session_request( session_token,
|
250
|
-
:keep_alive,
|
255
|
+
:keep_alive,
|
251
256
|
:keep_alive_response )
|
252
257
|
|
253
|
-
# Need to do the old school way of checking as the keep_alive
|
254
|
-
|
255
|
-
|
256
|
-
return error_code == 'OK' ?
|
258
|
+
# Need to do the old school way of checking as the keep_alive
|
259
|
+
# response doesn't return a minorErrorCode, so fails
|
260
|
+
error_code = response[:header][:error_code]
|
261
|
+
return error_code == 'OK' ?
|
262
|
+
response[:header][:session_token].extend( Success ) :
|
263
|
+
error_code.extend( Failure )
|
257
264
|
end
|
258
265
|
|
259
266
|
def logout(session_token)
|
260
267
|
response = @global_service.
|
261
268
|
session_request( session_token,
|
262
|
-
:logout,
|
269
|
+
:logout,
|
263
270
|
:logout_response )
|
264
271
|
return response.maybe_result( :header, :session_token )
|
265
272
|
end
|
@@ -268,11 +275,11 @@ module Betfair
|
|
268
275
|
## END OF API METHODS
|
269
276
|
|
270
277
|
|
271
|
-
def exchange(exchange_id)
|
278
|
+
def exchange(exchange_id)
|
272
279
|
exchange_id == EXCHANGE_IDS[:aus] ? @aus_service : @uk_service
|
273
280
|
end
|
274
281
|
|
275
|
-
def session_token(response_header)
|
282
|
+
def session_token(response_header)
|
276
283
|
response_header[:error_code] == 'OK' ? response_header[:session_token] : response_header[:error_code]
|
277
284
|
end
|
278
285
|
|
@@ -297,13 +304,13 @@ module Betfair
|
|
297
304
|
|
298
305
|
# Handy constants
|
299
306
|
NAMESPACES = {
|
300
|
-
aus
|
301
|
-
global
|
302
|
-
uk
|
307
|
+
:aus => 'http://www.betfair.com/exchange/v3/BFExchangeService/AUS',
|
308
|
+
:global => 'https://www.betfair.com/global/v3/BFGlobalService',
|
309
|
+
:uk => 'http://www.betfair.com/exchange/v3/BFExchangeService/UK' }
|
303
310
|
ENDPOINTS = {
|
304
|
-
aus
|
305
|
-
global
|
306
|
-
uk
|
311
|
+
:aus => 'https://api-au.betfair.com/exchange/v5/BFExchangeService',
|
312
|
+
:global => 'https://api.betfair.com/global/v3/BFGlobalService',
|
313
|
+
:uk => 'https://api.betfair.com/exchange/v5/BFExchangeService' }
|
307
314
|
|
308
315
|
|
309
316
|
# Factory methods for building clients to the different endpoints
|
@@ -345,15 +352,15 @@ module Betfair
|
|
345
352
|
# For those requests which take place in the context of a session,
|
346
353
|
# this method constructs the correct header and delegates to #request.
|
347
354
|
def session_request( session_token, method, result_field, body = {})
|
348
|
-
header_body = { header
|
355
|
+
header_body = { :header => api_request_header(session_token) }
|
349
356
|
full_body = header_body.merge( body )
|
350
357
|
|
351
358
|
request method, result_field, full_body
|
352
359
|
end
|
353
360
|
|
354
361
|
|
355
|
-
def api_request_header(session_token)
|
356
|
-
{ client_stamp
|
362
|
+
def api_request_header(session_token)
|
363
|
+
{ :client_stamp => 0, :session_token => session_token }
|
357
364
|
end
|
358
365
|
protected :api_request_header
|
359
366
|
|
@@ -366,7 +373,7 @@ module Betfair
|
|
366
373
|
module ErrorPresenter
|
367
374
|
|
368
375
|
def success?
|
369
|
-
self[:error_code] ==
|
376
|
+
self[:error_code] == "OK"
|
370
377
|
end
|
371
378
|
|
372
379
|
|
@@ -403,26 +410,26 @@ module Betfair
|
|
403
410
|
markets.each do |piece|
|
404
411
|
piece.gsub! "\0", '\:'
|
405
412
|
foo = piece.split('~')
|
406
|
-
market_hash[foo[0].to_i] = {
|
407
|
-
market_id
|
408
|
-
market_name
|
409
|
-
market_type
|
410
|
-
market_status
|
413
|
+
market_hash[foo[0].to_i] = {
|
414
|
+
:market_id => foo[0].to_i,
|
415
|
+
:market_name => foo[1].to_s,
|
416
|
+
:market_type => foo[2].to_s,
|
417
|
+
:market_status => foo[3].to_s,
|
411
418
|
# bf returns in this case time in Epoch, but in milliseconds
|
412
|
-
event_date
|
413
|
-
menu_path
|
414
|
-
event_hierarchy
|
415
|
-
bet_delay
|
416
|
-
exchange_id
|
417
|
-
iso3_country_code
|
418
|
-
# bf returns in this case time in Epoch, but in milliseconds
|
419
|
-
last_refresh
|
420
|
-
|
421
|
-
number_of_winners
|
422
|
-
total_amount_matched
|
423
|
-
bsp_market
|
424
|
-
turning_in_play
|
425
|
-
}
|
419
|
+
:event_date => Time.at(foo[4].to_i/1000),
|
420
|
+
:menu_path => foo[5].to_s,
|
421
|
+
:event_hierarchy => foo[6].to_s,
|
422
|
+
:bet_delay => foo[7].to_s,
|
423
|
+
:exchange_id => foo[8].to_i,
|
424
|
+
:iso3_country_code => foo[9].to_s,
|
425
|
+
# bf returns in this case time in Epoch, but in milliseconds
|
426
|
+
:last_refresh => Time.at(foo[10].to_i/1000),
|
427
|
+
:number_of_selections => foo[11].to_i,
|
428
|
+
:number_of_winners => foo[12].to_i,
|
429
|
+
:total_amount_matched => foo[13].to_f,
|
430
|
+
:bsp_market => foo[14] == 'Y' ? true : false,
|
431
|
+
:turning_in_play => foo[15] == 'Y' ? true : false
|
432
|
+
}
|
426
433
|
end
|
427
434
|
return market_hash
|
428
435
|
end
|
@@ -441,25 +448,9 @@ module Betfair
|
|
441
448
|
event_date = Time.at(bar[4].to_i/1000).utc
|
442
449
|
last_refresh = Time.at(bar[10].to_i/1000).utc
|
443
450
|
|
444
|
-
doh = {
|
445
|
-
|
446
|
-
|
447
|
-
market_type: bar[2],
|
448
|
-
market_status: bar[3],
|
449
|
-
event_date: event_date,
|
450
|
-
menu_path: bar[5],
|
451
|
-
event_heirachy: bar[6],
|
452
|
-
bet_delay: bar[7].to_i,
|
453
|
-
exchange_id: bar[8].to_i,
|
454
|
-
iso3_country_code: bar[9],
|
455
|
-
last_refresh: last_refresh,
|
456
|
-
number_of_runners: bar[11].to_i,
|
457
|
-
number_of_winners: bar[12].to_i,
|
458
|
-
total_amount_matched: bar[13].to_f,
|
459
|
-
bsp_market: bsp_market,
|
460
|
-
turning_in_play: turning_in_play
|
461
|
-
}
|
462
|
-
|
451
|
+
doh = { market_id: bar[0].to_i, market_name: bar[1], market_type: bar[2], market_status: bar[3], event_date: event_date, menu_path: bar[5], event_heirachy: bar[6],
|
452
|
+
bet_delay: bar[7].to_i, exchange_id: bar[8].to_i, iso3_country_code: bar[9], last_refresh: last_refresh, number_of_selections: bar[11].to_i, number_of_winners: bar[12].to_i,
|
453
|
+
total_amount_matched: bar[13].to_f, bsp_market: bsp_market, turning_in_play: turning_in_play }
|
463
454
|
foo << doh if !doh[:market_name].nil?
|
464
455
|
end
|
465
456
|
end
|
@@ -467,24 +458,24 @@ module Betfair
|
|
467
458
|
end
|
468
459
|
|
469
460
|
def market_info(details)
|
470
|
-
{ exchange_id
|
471
|
-
market_type_id
|
472
|
-
market_matched
|
473
|
-
menu_path
|
474
|
-
market_id
|
475
|
-
market_name
|
476
|
-
market_type_name
|
461
|
+
{ :exchange_id => nil,
|
462
|
+
:market_type_id => nil,
|
463
|
+
:market_matched => nil,
|
464
|
+
:menu_path => details[:menu_path],
|
465
|
+
:market_id => details[:market_id],
|
466
|
+
:market_name => details[:name],
|
467
|
+
:market_type_name => details[:menu_path].to_s.split('\\')[1]
|
477
468
|
}
|
478
469
|
end
|
479
470
|
|
480
471
|
def details(market)
|
481
|
-
|
482
|
-
market[:runners][:runner].each { |
|
483
|
-
return { market_id
|
472
|
+
selections = []
|
473
|
+
market[:runners][:runner].each { |selection| selections << { :selection_id => selection[:selection_id].to_i, :selection_name => selection[:name] } }
|
474
|
+
return { :market_id => market[:market_id].to_i, :market_type_id => market[:event_type_id].to_i, :selection => selections }
|
484
475
|
end
|
485
476
|
|
486
477
|
def prices(prices)
|
487
|
-
price_hash = {}
|
478
|
+
price_hash = {}
|
488
479
|
prices.gsub! '\:', "\0"
|
489
480
|
pieces = prices.split ":"
|
490
481
|
pieces.each do |piece|
|
@@ -495,24 +486,24 @@ module Betfair
|
|
495
486
|
end
|
496
487
|
|
497
488
|
def combine(market, prices)
|
498
|
-
market = details(market)
|
489
|
+
market = details(market)
|
499
490
|
prices = prices(prices)
|
500
|
-
market[:
|
501
|
-
|
502
|
-
|
503
|
-
|
491
|
+
market[:selection].each do |selection|
|
492
|
+
selection.merge!( { :market_id => market[:market_id] } )
|
493
|
+
selection.merge!( { :market_type_id => market[:market_type_id] } )
|
494
|
+
selection.merge!(price_string(prices[selection[:selection_id]]))
|
504
495
|
end
|
505
496
|
end
|
506
497
|
|
507
498
|
##
|
508
499
|
#
|
509
500
|
# Complete representation of market price data response,
|
510
|
-
# except "removed
|
501
|
+
# except "removed selection" which is returned as raw string.
|
511
502
|
#
|
512
503
|
##
|
513
504
|
def prices_complete(prices)
|
514
505
|
aux_hash = {}
|
515
|
-
price_hash = {}
|
506
|
+
price_hash = {}
|
516
507
|
|
517
508
|
prices.gsub! '\:', "\0"
|
518
509
|
pieces = prices.split ":"
|
@@ -521,21 +512,21 @@ module Betfair
|
|
521
512
|
aux = pieces.first
|
522
513
|
aux.gsub! "\0", '\:'
|
523
514
|
foo = aux.split('~')
|
524
|
-
aux_hash = {
|
525
|
-
market_id
|
526
|
-
currency
|
527
|
-
market_status
|
528
|
-
in_play_delay
|
529
|
-
number_of_winners
|
530
|
-
market_information
|
531
|
-
discount_allowed
|
532
|
-
market_base_rate
|
533
|
-
refresh_time_in_milliseconds
|
534
|
-
|
535
|
-
bsp_market
|
515
|
+
aux_hash = {
|
516
|
+
:market_id => foo[0].to_i,
|
517
|
+
:currency => foo[1].to_s,
|
518
|
+
:market_status => foo[2].to_s,
|
519
|
+
:in_play_delay => foo[3].to_i,
|
520
|
+
:number_of_winners => foo[4].to_i,
|
521
|
+
:market_information => foo[5].to_s,
|
522
|
+
:discount_allowed => foo[6] == 'true' ? true : false,
|
523
|
+
:market_base_rate => foo[7].to_s,
|
524
|
+
:refresh_time_in_milliseconds => foo[8].to_i,
|
525
|
+
:removed_selections => foo[9].to_s,
|
526
|
+
:bsp_market => foo[10] == 'Y' ? true : false
|
536
527
|
}
|
537
528
|
|
538
|
-
# now iterating over the prices excluding the first piece that we already parsed above
|
529
|
+
# now iterating over the prices excluding the first piece that we already parsed above
|
539
530
|
pieces[1..-1].each do |piece|
|
540
531
|
piece.gsub! "\0", '\:'
|
541
532
|
|
@@ -543,17 +534,17 @@ module Betfair
|
|
543
534
|
# using the selection_id as hash key
|
544
535
|
price_hash_key = bar[0].to_i
|
545
536
|
|
546
|
-
price_hash[price_hash_key] = {
|
547
|
-
selection_id
|
548
|
-
order_index
|
549
|
-
total_amount_matched
|
550
|
-
last_price_matched
|
551
|
-
handicap
|
552
|
-
reduction_factor
|
553
|
-
vacant
|
554
|
-
far_sp_price
|
555
|
-
near_sp_price
|
556
|
-
actual_sp_price
|
537
|
+
price_hash[price_hash_key] = {
|
538
|
+
:selection_id => bar[0].to_i,
|
539
|
+
:order_index => bar[1].to_i,
|
540
|
+
:total_amount_matched => bar[2].to_f,
|
541
|
+
:last_price_matched => bar[3].to_f,
|
542
|
+
:handicap => bar[4].to_f,
|
543
|
+
:reduction_factor => bar[5].to_f,
|
544
|
+
:vacant => bar[6] == 'true' ? true : false,
|
545
|
+
:far_sp_price => bar[7].to_f,
|
546
|
+
:near_sp_price => bar[8].to_f,
|
547
|
+
:actual_sp_price => bar[9].to_f
|
557
548
|
}
|
558
549
|
|
559
550
|
# merge lay and back prices into price_hash
|
@@ -569,43 +560,29 @@ module Betfair
|
|
569
560
|
string_raw = string
|
570
561
|
string = string.split('|')
|
571
562
|
|
572
|
-
price = {
|
573
|
-
|
574
|
-
|
575
|
-
last_back_price: 0,
|
576
|
-
wom: 0,
|
577
|
-
b1: 0,
|
578
|
-
b1_available: 0,
|
579
|
-
b2: 0,
|
580
|
-
b2_available: 0,
|
581
|
-
b3: 0,
|
582
|
-
b3_available: 0,
|
583
|
-
l1: 0,
|
584
|
-
l1_available: 0,
|
585
|
-
l2: 0,
|
586
|
-
l2_available: 0,
|
587
|
-
l3: 0,
|
588
|
-
l3_available: 0
|
563
|
+
price = { :prices_string => nil, :selection_matched => 0, :last_back_price => 0, :wom => 0,
|
564
|
+
:b1 => 0, :b1_available => 0, :b2 => 0, :b2_available => 0, :b3 => 0, :b3_available => 0,
|
565
|
+
:l1 => 0, :l1_available => 0, :l2 => 0, :l2_available => 0, :l3 => 0, :l3_available => 0
|
589
566
|
}
|
590
567
|
|
591
568
|
if !string[0].nil? and !prices_only
|
592
|
-
str = string[0].split('~')
|
569
|
+
str = string[0].split('~')
|
593
570
|
price[:prices_string] = string_raw
|
594
|
-
price[:
|
571
|
+
price[:selection_matched] = str[2].to_f
|
595
572
|
price[:last_back_price] = str[3].to_f
|
596
573
|
end
|
597
574
|
|
598
575
|
# Get the b prices (which are actually the l prices)
|
599
576
|
if !string[1].nil?
|
600
|
-
b = string[1].split('~')
|
577
|
+
b = string[1].split('~')
|
601
578
|
price[:b1] = b[0].to_f if !b[0].nil?
|
602
579
|
price[:b1_available] = b[1].to_f if !b[1].nil?
|
603
580
|
price[:b2] = b[4].to_f if !b[5].nil?
|
604
581
|
price[:b2_available] = b[5].to_f if !b[6].nil?
|
605
582
|
price[:b3] = b[8].to_f if !b[8].nil?
|
606
|
-
price[:b3_available] = b[9].to_f if !b[9].nil?
|
583
|
+
price[:b3_available] = b[9].to_f if !b[9].nil?
|
607
584
|
combined_b = price[:b1_available] + price[:b2_available] + price[:b3_available]
|
608
|
-
end
|
585
|
+
end
|
609
586
|
|
610
587
|
# Get the l prices (which are actually the l prices)
|
611
588
|
if !string[2].nil?
|
@@ -615,16 +592,16 @@ module Betfair
|
|
615
592
|
price[:l2] = l[4].to_f if !l[4].nil?
|
616
593
|
price[:l2_available] = l[5].to_f if !l[5].nil?
|
617
594
|
price[:l3] = l[8].to_f if !l[8].nil?
|
618
|
-
price[:l3_available] = l[9].to_f if !l[9].nil?
|
595
|
+
price[:l3_available] = l[9].to_f if !l[9].nil?
|
619
596
|
combined_l = price[:l1_available] + price[:l2_available] + price[:l3_available]
|
620
597
|
end
|
621
598
|
|
622
599
|
price[:wom] = combined_b / ( combined_b + combined_l ) unless combined_b.nil? or combined_l.nil?
|
623
600
|
|
624
|
-
return price
|
601
|
+
return price
|
625
602
|
end
|
626
|
-
|
627
|
-
|
603
|
+
|
604
|
+
def odds_table
|
628
605
|
odds_table = []
|
629
606
|
(1.01..1.99).step(0.01).each { |i| odds_table << i.round(2) }
|
630
607
|
(2..2.98).step(0.02).each { |i| odds_table << i.round(2) }
|