Class: Asianodds::Login

Inherits:
Object
  • Object
show all
Defined in:
lib/asianodds/login.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, password) ⇒ Login

Initialize the user with a username and password, as well as hashed password as requested from AO



36
37
38
39
40
41
42
43
44
# File 'lib/asianodds/login.rb', line 36

def initialize(user, password)
  @user = user
  @password = password
  @password_md5 = Digest::MD5.hexdigest(@password)
  @ao_token = 'default'
  @ao_key = 'default'
  @base_url = BASE_API_URL
  
end

Instance Attribute Details

#ao_keyObject (readonly)

Returns the value of attribute ao_key.



10
11
12
# File 'lib/asianodds/login.rb', line 10

def ao_key
  @ao_key
end

#ao_tokenObject (readonly)

Returns the value of attribute ao_token.



10
11
12
# File 'lib/asianodds/login.rb', line 10

def ao_token
  @ao_token
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/asianodds/login.rb', line 10

def base_url
  @base_url
end

#codeObject (readonly)

Returns the value of attribute code.



10
11
12
# File 'lib/asianodds/login.rb', line 10

def code
  @code
end

#messageObject (readonly)

Returns the value of attribute message.



10
11
12
# File 'lib/asianodds/login.rb', line 10

def message
  @message
end

#successful_loginObject (readonly)

Returns the value of attribute successful_login.



10
11
12
# File 'lib/asianodds/login.rb', line 10

def 
  @successful_login
end

Instance Method Details

#get_account_summaryObject

Get the users account summary (i.e. users currency, users credit, outstanding amount, P&L’s etc.)



140
141
142
143
144
145
146
# File 'lib/asianodds/login.rb', line 140

def 
  if loggedin?
    return get_request('GetAccountSummary')
  else
    #raise NotLoggedIn
  end
end

#get_bet_by_reference(bet_reference) ⇒ Object

You can also request a single bet with its reference - useful to check whether the bet was accepted by the bookmaker



113
114
115
116
117
118
119
# File 'lib/asianodds/login.rb', line 113

def get_bet_by_reference(bet_reference)
  if loggedin?
    return get_request("GetBetByReference?betReference=#{bet_reference}")
  else
    #raise NotLoggedIn
  end
end

#get_bet_history_summary(attributes) ⇒ Object

Get all the transactions of a specific day (defaults to yesterday) including win and loss values



179
180
181
182
183
184
185
186
187
# File 'lib/asianodds/login.rb', line 179

def get_bet_history_summary(attributes)
  attributes[:date].nil? ? date = (Date.today - 1).strftime('%m/%d/%Y') : date = attributes[:date]
  attributes[:bookies].nil? ? bookies = 'ALL' : bookies = attributes[:bookies]
  if loggedin?
    return get_request("GetBetHistorySummary?date=#{date}&bookies=#{bookies}")
  else
    #raise NotLoggedIn
  end
end

#get_betsObject

Get all active bets (= running, outstanding, finished) of the current user



104
105
106
107
108
109
110
# File 'lib/asianodds/login.rb', line 104

def get_bets
  if loggedin?
    return get_request('GetBets')
  else
    #raise NotLoggedIn
  end
end

#get_bookiesObject

Get the list of bookmakers available on the Asianodds platform



161
162
163
164
165
166
167
# File 'lib/asianodds/login.rb', line 161

def get_bookies
  if loggedin?
    return get_request('GetBookies')
  else
    #raise NotLoggedIn
  end
end

#get_feeds(attributes) ⇒ Object

Get all the Match Feeds (odds, status, etc.) based on the settings provided



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/asianodds/login.rb', line 88

def get_feeds(attributes)
  attributes[:sports_type].nil? ? sports_type = 1 : sports_type = attributes[:sports_type]
  attributes[:market_type].nil? ? market_type = 1 : market_type = attributes[:market_type]
  attributes[:bookies].nil? ? bookies = 'ALL' : bookies = attributes[:bookies]
  attributes[:leagues].nil? ? leagues = 'ALL' : leagues = attributes[:leagues]
  attributes[:odds_format].nil? ? odds_format = '00' : odds_format = attributes[:odds_format]
  attributes[:since].nil? ? since = '0' : since = attributes[:since]

  if loggedin?
    return get_request("GetFeeds?sportsType=#{sports_type}&marketTypeId=#{market_type}&bookies=#{bookies}&leagues=#{leagues}&oddsFormat=#{odds_format}&since=#{since}")
  else
    #raise NotLoggedIn
  end
end

#get_history_statement(attributes) ⇒ Object

Get a transaction history of a certain time-frame - defaults to the last 7 days



149
150
151
152
153
154
155
156
157
158
# File 'lib/asianodds/login.rb', line 149

def get_history_statement(attributes)
  attributes[:from_date].nil? ? from_date = (Date.today - 7).strftime('%m/%d/%Y') : from_date = attributes[:from_date]
  attributes[:to_date].nil? ? to_date = Date.today.strftime('%m/%d/%Y') : to_date = attributes[:to_date]
  attributes[:bookies].nil? ? bookies = 'ALL' : bookies = attributes[:bookies]
  if loggedin?
    return get_request("GetHistoryStatement?from=#{from_date}&to=#{to_date}&bookies=#{bookies}&shouldHideTransactionData=false")
  else
    #raise NotLoggedIn
  end
end

#get_leagues(attributes) ⇒ Object

Get the ids for all leagues for a certain sport (football as default)



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/asianodds/login.rb', line 190

def get_leagues(attributes)
  attributes[:sports_type].nil? ? sports_type = 1 : sports_type = attributes[:sports_type]
  attributes[:market_type].nil? ? market_type = 1 : market_type = attributes[:market_type]
  attributes[:bookies].nil? ? bookies = 'ALL' : bookies = attributes[:bookies]
  attributes[:since].nil? ? since = '0' : since = attributes[:since]
  if loggedin?
    return get_request("GetLeagues?sportsType=#{sports_type}&marketTypeId=#{market_type}&bookies=#{bookies}&since=#{since}")
  else
    #raise NotLoggedIn
  end
end

#get_matches(attributes) ⇒ Object

Get all the Matches (!= feeds) based on the settings provided



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

def get_matches(attributes)
  attributes[:sports_type].nil? ? sports_type = 1 : sports_type = attributes[:sports_type]
  attributes[:market_type].nil? ? market_type = 1 : market_type = attributes[:market_type]
  attributes[:bookies].nil? ? bookies = 'ALL' : bookies = attributes[:bookies]
  attributes[:leagues].nil? ? leagues = 'ALL' : leagues = attributes[:leagues]
  attributes[:since].nil? ? since = '0' : since = attributes[:since]

  if loggedin?
    return get_request("GetMatches?sportsType=#{sports_type}&marketTypeId=#{market_type}&bookies=#{bookies}&leagues=#{leagues}&since=#{since}")
  else
    #raise NotLoggedIn
  end
end

#get_non_running_betsObject

A subset of GetBets which returns only the currently not running bets



131
132
133
134
135
136
137
# File 'lib/asianodds/login.rb', line 131

def get_non_running_bets
  if loggedin?
    return get_request('GetNonRunningBets')
  else
    #raise NotLoggedIn
  end
end

#get_placement_info(attributes) ⇒ Object

Get information about the odds, minimum and maximum amounts to bet etc. for a certain match THIS IS A POST REQUEST



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/asianodds/login.rb', line 228

def get_placement_info(attributes)
  body = {
    "GameId": attributes[:game_id],
    "GameType": attributes[:game_type],
    "IsFullTime": attributes[:is_full_time],
    "Bookies": attributes[:bookies],
    "MarketTypeId": attributes[:market_type],
    "OddsFormat": attributes[:odds_format],
    "OddsName": attributes[:odds_name],
    "SportsType": attributes[:sports_type]
  }.to_json

  if loggedin?
    return post_request('GetPlacementInfo', body)
  else
    #raise NotLoggedIn
  end
end

#get_request(route) ⇒ Object

The API has a very standard request format for get requests - Use this one to stay DRY



13
14
15
16
17
18
19
20
# File 'lib/asianodds/login.rb', line 13

def get_request(route)
  response = Faraday.get "#{@base_url}/#{route}", {}, {
    'Accept': 'application/json',
    'AOToken': @ao_token,
    'AOKey': @ao_key
  }
  return JSON.parse(response.body, :symbolize_names => true)
end

#get_running_betsObject

A subset of GetBets which returns only the currently running bets



122
123
124
125
126
127
128
# File 'lib/asianodds/login.rb', line 122

def get_running_bets
  if loggedin?
    return get_request('GetRunningBets')
  else
    #raise NotLoggedIn
  end
end

#get_sportsObject

Get the ids for the sports offered by Asianodds (football and basketball as of today)



203
204
205
206
207
208
209
# File 'lib/asianodds/login.rb', line 203

def get_sports
  if loggedin?
    return get_request('GetSports')
  else
    #raise NotLoggedIn
  end
end

#get_user_informationObject

Get user setting, such as account status, ip address, username etc.



170
171
172
173
174
175
176
# File 'lib/asianodds/login.rb', line 170

def get_user_information
  if loggedin?
    return get_request('GetUserInformation')
  else
    #raise NotLoggedIn
  end
end

#loggedin?Boolean

Before executing any request which requires a logged in user (all), check for login Check for all following requests whether user is logged in and if not, log her in

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
# File 'lib/asianodds/login.rb', line 72

def loggedin?
  if @ao_token
    response = get_request('IsLoggedIn')
    return response[:Result][:CurrentlyLoggedIn] ? true : false
  else
    return false
  end
end

#loginObject

Log the user in to receive a token and key



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/asianodds/login.rb', line 47

def 
  response = get_request("Login?username=#{@user}&password=#{@password_md5}")
  @code = response[:Code]
  @ao_token = response[:Result][:Token]
  @ao_key = response[:Result][:Key]
  @base_url = response[:Result][:Url]
  @successful_login = response[:Result][:SuccessfulLogin]

  # All logged in users need to be registered with a token and key
  if @successful_login
    @message = "You successfully logged in - registering..."
    register
  else
    @message = response[:Result][:TextMessage]
  end
end

#logoutObject

Log the user out. In order for this to work, the user must be logged in



82
83
84
85
# File 'lib/asianodds/login.rb', line 82

def logout
  response = get_request('Logout')
  return response[:Result]
end

#place_bet(attributes) ⇒ Object

Get information about the odds, minimum and maximum amounts to bet etc. for a certain match THIS IS A POST REQUEST



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/asianodds/login.rb', line 249

def place_bet(attributes)
  body = {
    "GameId": attributes[:game_id],
    "GameType": attributes[:game_type],
    "IsFullTime": attributes[:is_full_time],
    "Bookies": attributes[:bookies],
    "MarketTypeId": attributes[:market_type],
    "OddsFormat": attributes[:odds_format],
    "OddsName": attributes[:odds_name],
    "SportsType": attributes[:sports_type],
    "BookieOdds": attributes[:bookie_odds],
    "Amount": attributes[:amount]
  }.to_json

  if loggedin?
    # Always needs to be called before placing the bet
    get_placement_info(attributes)
    return post_request('PlaceBet', body)
  else
    #raise NotLoggedIn
  end
end

#post_request(route, body) ⇒ Object

The API has a very standard request format for post requests - Use this one to stay DRY



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/asianodds/login.rb', line 23

def post_request(route, body)
  response = Faraday.post "#{@base_url}/#{route}",
  body,
  {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'AOToken': @ao_token
  }

  return JSON.parse(response.body, :symbolize_names => true)
end

#registerObject

With the token and key the user has to be registered



65
66
67
68
# File 'lib/asianodds/login.rb', line 65

def register
  response = get_request("Register?username=#{@user}")
  response[:Result][:Success] == true ? @message = response[:Result][:TextMessage] : @message = "Registration failed"
end