module Betfair::API::REST

Public Class Methods

extended(obj) click to toggle source
# File lib/betfair/api/rest.rb, line 7
def self.extended(obj)
  obj.persistent_headers.merge!({
    "Accept" => "application/json",
    "Content-Type" => "application/json"
  })

  apis = {
    betting: "https://#{obj.endpoint}.betfair.com/exchange/betting/rest/v1.0",
    account: "https://#{obj.endpoint}.betfair.com/exchange/account/rest/v1.0",
  }

  obj.class::OPERATIONS.each do |api, operations|
    operations.each do |operation|
      define_method(operation) do |body = nil|
        raise "Not signed in" unless ["X-Authentication", "X-Application"].all? { |k| persistent_headers.key?(k) }

        post(url: "#{apis[api]}/#{operation.to_s.camelize(:lower)}/", body: body.to_json)
      end
    end
  end
end

Public Instance Methods

interactive_login(username, password) click to toggle source
# File lib/betfair/api/rest.rb, line 29
def interactive_login(username, password)
  json = post({
    url: "https://identitysso.betfair.com/api/login",
    body: { username: username, password: password },
    headers: { "Content-Type" => "application/x-www-form-urlencoded" }
  })

  add_session_token_to_persistent_headers(json["token"])
end
logout() click to toggle source
# File lib/betfair/api/rest.rb, line 56
def logout
  get(url: "https://identitysso.betfair.com/api/logout")
end
non_interactive_login(username, password, cert_key_file_path, cert_file_path) click to toggle source

Performs the login procedure recommended for applications which run autonomously

username: Betfair account username string
password: Betfair account password string
cert_key_file_path: Path to Betfair client certificate private key file
cert_key_path: Path to Betfair client certificate public key file associated with Betfair account
# File lib/betfair/api/rest.rb, line 44
def non_interactive_login(username, password, cert_key_file_path, cert_file_path)
  json = post({
    url: "https://identitysso-cert.betfair.com/api/certlogin",
    body: { username: username, password: password },
    headers: { "Content-Type"  => "application/x-www-form-urlencoded" },
    cert_key_file_path: cert_key_file_path,
    cert_file_path: cert_file_path
  })

  add_session_token_to_persistent_headers(json["sessionToken"])
end

Private Instance Methods

add_session_token_to_persistent_headers(session_token) click to toggle source
# File lib/betfair/api/rest.rb, line 68
def add_session_token_to_persistent_headers(session_token)
  persistent_headers.merge!({
    "X-Authentication" => session_token
  })
end
handle_errors(response) click to toggle source
# File lib/betfair/api/rest.rb, line 78
def handle_errors(response)
end
parse_response(response) click to toggle source
# File lib/betfair/api/rest.rb, line 74
def parse_response(response)
  JSON.parse(response).tap { |r| handle_errors(r) }
end