Class: Betfair::API

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

Defined Under Namespace

Modules: ErrorPresenter 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.



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

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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/betfair/api.rb', line 70

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_multiple_bets(session_token, exchange_id, bets) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/betfair/api.rb', line 83

def cancel_multiple_bets(session_token, exchange_id, bets)
  bf_bet = []
  bets.each { |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

#exchange(exchange_id) ⇒ Object



194
195
196
# File 'lib/betfair/api.rb', line 194

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

#get_account_funds(session_token, exchange_id) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/betfair/api.rb', line 147

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



121
122
123
124
125
126
127
128
129
# File 'lib/betfair/api.rb', line 121

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



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/betfair/api.rb', line 132

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



97
98
99
100
101
102
103
104
105
106
# File 'lib/betfair/api.rb', line 97

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



109
110
111
112
113
114
115
116
117
118
# File 'lib/betfair/api.rb', line 109

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

, bet_ids = nil, , exclude_last_second = nil, matched_since = nil



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

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



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

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 )
  
  session_token(response[:header])
end

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

API METHODS



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/betfair/api.rb', line 21

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/betfair/api.rb', line 44

def place_multiple_bets(session_token, exchange_id, bets)		
  bf_bet = []
  bets.each do |bet|
    bf_bet << { 
      :marketId           => bet[:market_id], 
      :selectionId        => bet[:runner_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_bet } )

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

#session_token(response_header) ⇒ Object



198
199
200
# File 'lib/betfair/api.rb', line 198

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