Class: Betfair::Helpers

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

Instance Method Summary collapse

Instance Method Details

#all_markets(markets) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/betfair/api.rb', line 315

def all_markets(markets)
  market_hash = {}
  markets.gsub! '\:', "\0"
  markets = markets.split ":"
  markets.each do |piece|
    piece.gsub! "\0", '\:'
    market_hash[piece.split('~')[0].to_i] = { :market_id            => piece.split('~')[0].to_i,
      :market_name          => piece.split('~')[1].to_s,
      :market_type          => piece.split('~')[2].to_s,
      :market_status        => piece.split('~')[3].to_s,
      # bf returns in this case time in Epoch, but in milliseconds
      :event_date           => Time.at(piece.split('~')[4].to_i/1000),
      :menu_path            => piece.split('~')[5].to_s,
      :event_hierarchy      => piece.split('~')[6].to_s,
      :bet_delay            => piece.split('~')[7].to_s,
      :exchange_id          => piece.split('~')[8].to_i,
      :iso3_country_code    => piece.split('~')[9].to_s,
      # bf returns in this case time in Epoch, but in milliseconds
      :last_refresh         => Time.at(piece.split('~')[10].to_i/1000),
      :number_of_runners    => piece.split('~')[11].to_i,
      :number_of_winners    => piece.split('~')[12].to_i,
      :total_amount_matched => piece.split('~')[13].to_f,
      :bsp_market           => piece.split('~')[14] == 'Y' ? true : false,
      :turning_in_play      => piece.split('~')[15] == 'Y' ? true : false
    } 
  end
  return market_hash
end

#combine(market, prices) ⇒ Object



355
356
357
358
359
360
361
362
363
# File 'lib/betfair/api.rb', line 355

def combine(market, prices)
  market = details(market)            
  prices = prices(prices)
  market[:runners].each do |runner|
    runner.merge!( { :market_id => market[:market_id] } )
    runner.merge!( { :market_type_id => market[:market_type_id] } )
    runner.merge!(price_string(prices[runner[:runner_id]]))
  end
end

#details(market) ⇒ Object



365
366
367
368
369
# File 'lib/betfair/api.rb', line 365

def details(market)
  runners = []
  market[:runners][:runner].each { |runner| runners << { :runner_id => runner[:selection_id].to_i, :runner_name => runner[:name] } }
  return { :market_id => market[:market_id].to_i, :market_type_id => market[:event_type_id].to_i, :runners => runners }
end

#market_info(details) ⇒ Object



344
345
346
347
348
349
350
351
352
353
# File 'lib/betfair/api.rb', line 344

def market_info(details)
  { :exchange_id => nil,
    :market_type_id => nil,
    :market_matched => nil,
    :menu_path => details[:menu_path], 
    :market_id => details[:market_id], 
    :market_name => details[:name],
    :market_type_name => details[:menu_path].to_s.split('\\')[1]
  }
end

#price_string(string, prices_only = false) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/betfair/api.rb', line 439

def price_string(string, prices_only = false)
  string_raw = string
  string = string.split('|')

  price = { :prices_string => nil, :runner_matched => 0, :last_back_price => 0, :wom => 0, 
    :b1 => 0, :b1_available => 0, :b2 => 0, :b2_available => 0, :b3 => 0, :b3_available => 0,
    :l1 => 0, :l1_available => 0, :l2 => 0, :l2_available => 0, :l3 => 0, :l3_available => 0 
  }    			

  if !string[0].nil? and !prices_only
    str = string[0].split('~')	
    price[:prices_string] = string_raw
    price[:runner_matched] = str[2].to_f
    price[:last_back_price]   = str[3].to_f
  end

  # Get the b prices (which are actually the l prices)
  if !string[1].nil?
    b = string[1].split('~')	
    price[:b1]             = b[0].to_f if !b[0].nil?
    price[:b1_available]   = b[1].to_f if !b[1].nil?
    price[:b2]             = b[4].to_f if !b[5].nil?
    price[:b2_available]   = b[5].to_f if !b[6].nil?
    price[:b3]             = b[8].to_f if !b[8].nil?
    price[:b3_available]   = b[9].to_f if !b[9].nil?  				 				
    combined_b = price[:b1_available] + price[:b2_available] + price[:b3_available]
  end				

  # Get the l prices (which are actually the l prices)
  if !string[2].nil?
    l = string[2].split('~')
    price[:l1]             = l[0].to_f if !l[0].nil?
    price[:l1_available]   = l[1].to_f if !l[1].nil?
    price[:l2]             = l[4].to_f if !l[4].nil?
    price[:l2_available]   = l[5].to_f if !l[5].nil?
    price[:l3]             = l[8].to_f if !l[8].nil?
    price[:l3_available]   = l[9].to_f if !l[9].nil?  				  				
    combined_l = price[:l1_available] + price[:l2_available] + price[:l3_available]
  end			

  price[:wom] = combined_b / ( combined_b + combined_l ) unless combined_b.nil? or combined_l.nil?

  return price			  		
end

#prices(prices) ⇒ Object



371
372
373
374
375
376
377
378
379
380
# File 'lib/betfair/api.rb', line 371

def prices(prices)
  price_hash = {}					
  prices.gsub! '\:', "\0"
  pieces = prices.split ":"
  pieces.each do |piece|
    piece.gsub! "\0", '\:'
    price_hash[piece.split('~')[0].to_i] = piece
  end
  return price_hash
end

#prices_complete(prices) ⇒ Object

Complete representation of market price data response, except “removed runners” which is returned as raw string.



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/betfair/api.rb', line 388

def prices_complete(prices)
  aux_hash   = {}
  price_hash = {}         

  prices.gsub! '\:', "\0"
  pieces = prices.split ":"

  # parsing first the auxiliary price info
  aux = pieces.first
  aux.gsub! "\0", '\:'
  aux_hash =   {  :market_id            => aux.split('~')[0].to_i,
    :currency             => aux.split('~')[1].to_s,
    :market_status        => aux.split('~')[2].to_s,
    :in_play_delay        => aux.split('~')[3].to_i,
    :number_of_winners    => aux.split('~')[4].to_i,
    :market_information   => aux.split('~')[5].to_s,
    :discount_allowed     => aux.split('~')[6] == 'true' ? true : false,
    :market_base_rate     => aux.split('~')[7].to_s,
    :refresh_time_in_milliseconds => aux.split('~')[8].to_i,
    :removed_runners      => aux.split('~')[9].to_s,
    :bsp_market           => aux.split('~')[10] == 'Y' ? true : false
  }

  # now iterating over the prices excluding the first piece that we already parsed above 
  pieces[1..-1].each do |piece|
    piece.gsub! "\0", '\:'

    # using the selection_id as hash key
    price_hash_key = piece.split('~')[0].to_i

    price_hash[price_hash_key] = {  :selection_id         => piece.split('~')[0].to_i,
      :order_index          => piece.split('~')[1].to_i,
      :total_amount_matched => piece.split('~')[2].to_f,
      :last_price_matched   => piece.split('~')[3].to_f,
      :handicap             => piece.split('~')[4].to_f,
      :reduction_factor     => piece.split('~')[5].to_f,
      :vacant               => piece.split('~')[6] == 'true' ? true : false,
      :far_sp_price         => piece.split('~')[7].to_f,
      :near_sp_price        => piece.split('~')[8].to_f,
      :actual_sp_price      => piece.split('~')[9].to_f                           
    }

    # merge lay and back prices into price_hash
    price_hash[price_hash_key].merge!(price_string(piece, true))
  end

  price_hash.merge!(aux_hash)

  return price_hash
end