Class: Betfair::API

Inherits:
Object
  • Object
show all
Defined in:
lib/betfair/api.rb

Defined Under Namespace

Modules: ErrorPresenter, Failure, Success Classes: SOAPClient

Constant Summary collapse

EXCHANGE_IDS =

Some handy constants…

{
  :aus => 2,
  :uk  => 1
}
PRODUCT_ID_FREE =
82
BET_TYPE_LAY =
'L'
BET_TYPE_BACK =
'B'

Instance Method Summary collapse

Constructor Details

#initialize(proxy = nil, logging = nil) ⇒ API

Returns a new instance of API.



287
288
289
290
291
292
293
294
295
# File 'lib/betfair/api.rb', line 287

def initialize(proxy = nil, logging = nil)

  SOAPClient.log = logging

  @global_service = SOAPClient.global( proxy )
  @uk_service     = SOAPClient.uk( proxy )
  @aus_service    = SOAPClient.aus( proxy )

end

Instance Method Details

#cancel_bet(session_token, exchange_id, bet_id) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/betfair/api.rb', line 125

def cancel_bet(session_token, exchange_id, bet_id)
  bf_bet = { :betId => bet_id }

  response = exchange(exchange_id).
    session_request( session_token,
                     :cancelBets, 
                     :cancel_bets_response,
                     :bets => { 'CancelBets' => [bf_bet] } ) # "CancelBets" has to be a string, not a symbol!
  
  return response.maybe_result( :bet_results, :cancel_bets_result )
end

#cancel_bet_by_market(session_token, exchange_id, market_id) ⇒ Object



150
151
152
# File 'lib/betfair/api.rb', line 150

def cancel_bet_by_market(session_token, exchange_id, market_id)
  raise 'Service not available in product id of 82'
end

#cancel_multiple_bets(session_token, exchange_id, bets) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/betfair/api.rb', line 137

def cancel_multiple_bets(session_token, exchange_id, bets)
  bf_bets = []
  bets.each { |bet_id| bf_bets << { :betId => bet_id } }

  response = exchange(exchange_id).
    session_request( session_token,
                     :cancelBets, 
                     :cancel_bets_response,
                     :bets => { 'CancelBets' => bf_bets } ) # "CancelBets" has to be a string, not a symbol!
  
  return response.maybe_result( :bet_results, :cancel_bets_result )
end

#exchange(exchange_id) ⇒ Object



278
279
280
# File 'lib/betfair/api.rb', line 278

def exchange(exchange_id)   
  exchange_id == EXCHANGE_IDS[:aus] ? @aus_service : @uk_service
end

#get_account_funds(session_token, exchange_id) ⇒ Object



227
228
229
230
231
232
233
234
# File 'lib/betfair/api.rb', line 227

def ( session_token, exchange_id )
  response = exchange(exchange_id).
    session_request( session_token, 
                     :getAccountFunds, 
                     :get_account_funds_response )

  return response.maybe_result
end

#get_active_event_types(session_token, locale = nil) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/betfair/api.rb', line 201

def get_active_event_types(session_token, locale = nil)
  response = @global_service.
    session_request( session_token,
                     :getActiveEventTypes, 
                     :get_active_event_types_response,
                     :locale => locale )

  return response.maybe_result( :event_type_items, :event_type )
end

#get_all_markets(session_token, exchange_id, event_type_ids = nil, locale = nil, countries = nil, from_date = nil, to_date = nil) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/betfair/api.rb', line 212

def get_all_markets(session_token, exchange_id, event_type_ids = nil, locale = nil, countries = nil, from_date = nil, to_date = nil)
  response = exchange(exchange_id).
    session_request( session_token, 
                     :getAllMarkets, 
                     :get_all_markets_response,
                     :eventTypeIds => { 'int' => event_type_ids }, 
                     :locale       => locale, 
                     :countries    => { 'country' => countries }, 
                     :fromDate     => from_date, 
                     :toDate       => to_date )
  
  return response.maybe_result( :market_data )
end

#get_market(session_token, exchange_id, market_id, locale = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/betfair/api.rb', line 177

def get_market(session_token, exchange_id, market_id, locale = nil) 
  response = exchange(exchange_id).
    session_request( session_token, 
                     :getMarket, 
                     :get_market_response,
                     :marketId => market_id, 
                     :locale   => locale )

  return response.maybe_result( :market )
end

#get_market_prices_compressed(session_token, exchange_id, market_id, currency_code = nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/betfair/api.rb', line 189

def get_market_prices_compressed(session_token, exchange_id, market_id, currency_code = nil)
  response = exchange(exchange_id).
    session_request( session_token,
                     :getMarketPricesCompressed, 
                     :get_market_prices_compressed_response,
                     :marketId => market_id,
                     :currencyCode => currency_code )
  
  return response.maybe_result( :market_prices )
end

#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') ⇒ Object

Read-Only Betting API METHODS



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/betfair/api.rb', line 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
  response = exchange(exchange_id).
    session_request( session_token, 
                     :getMUBets, 
                     :get_mu_bets_response,
                     #:betIds => bet_ids,
                     :betStatus => bet_status,
                     #:excludeLastSecond => exclude_last_second,
                     :marketId => market_id,
                     #:matchedSince => matched_since,
                     :orderBy => order_by,
                     :recordCount => record_count,
                     :sortOrder => sort_order,
                     :startRecord => start_record
                     )

  return response.maybe_result( :bets, :mu_bet )
end

#keep_alive(session_token) ⇒ Object

General API METHODS



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/betfair/api.rb', line 252

def keep_alive(session_token)
  response = @global_service.
    session_request( session_token,
                     :keep_alive, 
                     :keep_alive_response )
  
  # Need to do the old school way of checking as the keep_alive
  # response doesn't return a minorErrorCode, so fails
  error_code = response[:header][:error_code]     
  return error_code == 'OK' ? 
    response[:header][:session_token].extend( Success ) : 
    error_code.extend( Failure )
end

#login(username, password, product_id, vendor_software_id, location_id, ip_address) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/betfair/api.rb', line 236

def (username, password, product_id, vendor_software_id, location_id, ip_address)
  response = @global_service.request( :login, 
                                      :login_response, 
                                      :username         => username, 
                                      :password         => password, 
                                      :productId        => product_id, 
                                      :vendorSoftwareId => vendor_software_id, 
                                      :locationId       => location_id, 
                                      :ipAddress        => ip_address )

  return response.maybe_result( :header, :session_token )
end

#logout(session_token) ⇒ Object



266
267
268
269
270
271
272
# File 'lib/betfair/api.rb', line 266

def logout(session_token)
  response = @global_service.
    session_request( session_token,
                     :logout, 
                     :logout_response )
  return response.maybe_result( :header, :session_token )
end

#place_bet(session_token, exchange_id, market_id, selection_id, bet_type, price, size) ⇒ Object

Bet Placement API METHODS



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/betfair/api.rb', line 34

def place_bet(session_token, exchange_id, market_id, selection_id, bet_type, price, size)		
  bf_bet = { 
    :marketId           => market_id, 
    :selectionId        => selection_id, 
    :betType            => bet_type, 
    :price              => price, 
    :size               => size, 
    :asianLineId        => 0, 
    :betCategoryType    => 'E', 
    :betPersistenceType => 'NONE', 
    :bspLiability       => 0 
  }

  response = exchange(exchange_id).
    session_request( session_token,
                     :placeBets, 
                     :place_bets_response,
                     :bets => { 'PlaceBets' => [bf_bet] } )

  return response.maybe_result( :bet_results, :place_bets_result )
end

#place_multiple_bets(session_token, exchange_id, bets) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/betfair/api.rb', line 57

def place_multiple_bets(session_token, exchange_id, bets)		
  bf_bets = []
  bets.each do |bet|
    bf_bets << { 
      :marketId           => bet[:market_id], 
      :selectionId        => bet[:selection_id], 
      :betType            => bet[:bet_type], 
      :price              => bet[:price], 
      :size               => bet[:size], 
      :asianLineId        => bet[:asian_line_id], 
      :betCategoryType    => bet[:bet_category_type], 
      :betPersistenceType => bet[:bet_peristence_type], 
      :bspLiability       => bet[:bsp_liability] 
    }
  end

  response = exchange(exchange_id).
    session_request( session_token,
                     :placeBets, 
                     :place_bets_response,
                     :bets => { 'PlaceBets' => bf_bets } )

  return response.maybe_result( :bet_results, :place_bets_result )
end

#session_token(response_header) ⇒ Object



282
283
284
# File 'lib/betfair/api.rb', line 282

def session_token(response_header)      
  response_header[:error_code] == 'OK' ? response_header[:session_token] : response_header[:error_code]
end

#update_bet(session_token, exchange_id, bet_id, new_bet_persitence_type, new_price, new_size, old_bet_persitance_type, old_price, old_size) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/betfair/api.rb', line 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)
   bf_bet = { 
      :betId                  => bet_id, 
      :newBetPersistenceType  => new_bet_persitence_type, 
      :newPrice               => new_price, 
      :newSize                => new_size, 
      :oldBetPersistenceType  => old_bet_persitance_type, 
      :oldPrice               => old_price, 
      :oldSize                => old_size
    }

    response = exchange(exchange_id).
      session_request( session_token,
                       :updateBets, 
                       :update_bets_response,
                       :bets => { 'UpdateBets' => [bf_bet] } )

    return response.maybe_result( :bet_results, :update_bets_result )
end

#update_multiple_bets(session_token, exchange_id, bets) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/betfair/api.rb', line 102

def update_multiple_bets(session_token, exchange_id, bets)
  bf_bets = []
  bets.each do |bet|
    bf_bets << { 
      :betId                  => bet[:bet_id], 
      :newBetPersistenceType  => bet[:new_bet_persitence_type], 
      :newPrice               => bet[:new_price], 
      :newSize                => bet[:new_size], 
      :oldBetPersistenceType  => bet[:old_bet_persitance_type], 
      :oldPrice               => bet[:old_price], 
      :oldSize                => bet[:old_size] 
    }
  end

  response = exchange(exchange_id).
    session_request( session_token,
                     :updateBets, 
                     :update_bets_response,
                     :bets => { 'UpdateBets' => bf_bets } )

  return response.maybe_result( :bet_results, :update_bets_result )     
end