Class: KasPay
- Inherits:
-
Object
show all
- Extended by:
- MetaStuff
- Defined in:
- lib/kaspay.rb,
lib/kaspay/money.rb,
lib/kaspay/version.rb,
lib/kaspay/transaction.rb,
lib/kaspay/transaction_collection.rb
Defined Under Namespace
Classes: Money, Transaction, TransactionCollection
Constant Summary
collapse
- BASE_URL =
A bunch of KasPay constants.
"https://www.kaspay.com"
- LOGIN_URL =
BASE_URL + "/login"
- TRANSACTION_URL =
BASE_URL + "/account/transactiondetails/"
- TRANSACTION_HISTORY_URL =
BASE_URL + "/account/history/"
- DATA_DIR =
ENV['HOME'] + "/.kaspay"
- LOGIN_PATH =
DATA_DIR + "/login.dat"
- COOKIE_PATH =
DATA_DIR + "/cookie.dat"
- VERSION =
"0.1.2"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from MetaStuff
before
Constructor Details
#initialize(user = { email: nil, password: nil }) ⇒ KasPay
Accept a hash argument from class methods ‘login`
140
141
142
143
144
145
146
|
# File 'lib/kaspay.rb', line 140
def initialize user = { email: nil, password: nil }
unless user[:email] == nil || user[:password] == nil
@email = user[:email]
@password = user[:password]
login
end
end
|
Instance Attribute Details
#email ⇒ Object
Returns the value of attribute email.
129
130
131
|
# File 'lib/kaspay.rb', line 129
def email
@email
end
|
#password=(pass) ⇒ Object
186
187
188
189
190
|
# File 'lib/kaspay.rb', line 186
def password= pass
@password = pass
login if user_data_complete?
return nil
end
|
Class Method Details
.all_get_methods ⇒ Object
Hidden to force the use of ‘login` as the class method for instantiation.
63
64
65
|
# File 'lib/kaspay.rb', line 63
def all_get_methods
instance_methods.grep /^get_/
end
|
.browse(url) ⇒ Object
Creates Watir::Browser object for navigating web pages.
47
48
49
50
51
|
# File 'lib/kaspay.rb', line 47
def browse url
@@headless = Headless.new
@@headless.start
@@browser = Watir::Browser.start url
end
|
.clear_login(login_name = nil) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/kaspay.rb', line 92
def clear_login login_name = nil
login_scope do |login|
if !login_name.nil?
login.delete login_name
elsif login_name.nil?
login.roots.each do |name|
login.delete name
end
end
end
end
|
.cookies_scope(&block) ⇒ Object
116
117
118
|
# File 'lib/kaspay.rb', line 116
def cookies_scope(&block)
data_scope(COOKIE_PATH, &block)
end
|
.data_scope(path, &block) ⇒ Object
71
72
73
74
75
76
|
# File 'lib/kaspay.rb', line 71
def data_scope(path, &block)
kasdb = PStore.new(path)
kasdb.transaction do
yield(kasdb)
end
end
|
.load_login(login_name) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/kaspay.rb', line 104
def load_login login_name
raise LoadLoginError,
"login data \"#{login_name}\" cannot be found" \
unless login_data_exists? login_name
email, password = nil
login_scope do |login|
email = login[login_name][:email]
password = login[login_name][:password]
end
login email: email, password: password
end
|
.login_data_exists?(login_name) ⇒ Boolean
82
83
84
85
86
87
88
89
90
|
# File 'lib/kaspay.rb', line 82
def login_data_exists? login_name
data = nil
begin
login_scope {|login| data = login.roots}
return data.include? login_name
rescue NameError
return false
end
end
|
.login_scope(&block) ⇒ Object
78
79
80
|
# File 'lib/kaspay.rb', line 78
def login_scope(&block)
data_scope(LOGIN_PATH, &block)
end
|
.things_to_get ⇒ Object
67
68
69
|
# File 'lib/kaspay.rb', line 67
def things_to_get
all_get_methods.map{|m| m.id2name.sub("get_","")}
end
|
Instance Method Details
#check_login ⇒ Object
334
335
336
|
# File 'lib/kaspay.rb', line 334
def check_login
raise LoginError, "you are not logged in" unless logged_in?
end
|
#current_url ⇒ Object
Also known as:
url
192
193
194
|
# File 'lib/kaspay.rb', line 192
def current_url
browser.url
end
|
#get_acc_num ⇒ Object
209
210
211
|
# File 'lib/kaspay.rb', line 209
def get_acc_num
browser.span(class: "kaspay-id").text.sub("KasPay Account: ", "").to_i
end
|
#get_balance ⇒ Object
205
206
207
|
# File 'lib/kaspay.rb', line 205
def get_balance
Money.new browser.div(class: "kaspay-balance").span.text
end
|
#get_cookies_expire_time ⇒ Object
213
214
215
216
217
218
219
220
221
|
# File 'lib/kaspay.rb', line 213
def get_cookies_expire_time
current_cookies_scope do |cookies, cookies_name|
cookies[cookies_name][:cookies].each do |el|
if el[:name] == "kaspay_csrf_cookie"
return DateTime.parse(el[:expires].to_s)
end
end
end
end
|
#get_latest_transactions(no_of_trx, type = 0) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/kaspay.rb', line 253
def get_latest_transactions no_of_trx, type = 0
trx_ids = []
while no_of_trx - trx_ids.length > 0
browser.goto(TRANSACTION_HISTORY_URL \
+ trx_ids.length.to_s \
+ "?f=01/01/2009&t=" \
+ Time.now.strftime("%d/%m/%Y") \
+ "&transactiontype=" \
+ type.to_s)
browser.tds(class: "trxid").each do |td|
trx_ids << td.link.href.sub(/.*\/(.*)$/, '\1')
break if trx_ids.length == no_of_trx
end
break if trx_ids.length == 0 \
|| trx_ids.length >= no_of_trx
end
trx_ids.each_with_index do |trx_id, i|
trx_ids[i] = Transaction.new trx_id, browser
end
browser.goto(BASE_URL)
return trx_ids
end
|
#get_name ⇒ Object
201
202
203
|
# File 'lib/kaspay.rb', line 201
def get_name
browser.span(class: "user-name").text
end
|
#get_transaction(options = {}) ⇒ Object
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# File 'lib/kaspay.rb', line 237
def get_transaction options = {}
default_options = {
latest: 5,
type: :all_trx,
oldest_only: false
}
options = default_options.merge(options)
unless options[:oldest_only]
no_of_trx = options[:latest]
get_latest_transactions no_of_trx, trx_type_to_num(options[:type])
else
trx_no = options[:latest] - 1
get_transaction_number trx_no, trx_type_to_num(options[:type])
end
end
|
#get_transaction_number(trx_no, type = 0) ⇒ Object
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/kaspay.rb', line 277
def get_transaction_number trx_no, type = 0
row_number = trx_no % 5
browser.goto(TRANSACTION_HISTORY_URL \
+ (trx_no - row_number).to_s \
+ "?f=01/01/2009&t=" \
+ Time.now.strftime("%d/%m/%Y") \
+ "&transactiontype=" \
+ type.to_s)
trx_id = Transaction.new(browser.tds(class: "trxid")[row_number] \
.link.href.sub(/.*\/(.*)$/, '\1'), browser)
browser.goto(BASE_URL)
return trx_id
end
|
#goto(path) ⇒ Object
196
197
198
199
|
# File 'lib/kaspay.rb', line 196
def goto path
url = (path[0] == "/" ? (BASE_URL + path) : path)
browser.goto url
end
|
#home ⇒ Object
298
299
300
|
# File 'lib/kaspay.rb', line 298
def home
goto "/"
end
|
#inspect ⇒ Object
338
339
340
|
# File 'lib/kaspay.rb', line 338
def inspect
"#<#{self.class}:0x#{(object_id << 1).to_s(16)} logged_in=#{logged_in?}>"
end
|
#links ⇒ Object
292
293
294
295
296
|
# File 'lib/kaspay.rb', line 292
def links
links = []
browser.tds(class: "trxid").each{|td| links << td.link.href.to_s}
links
end
|
#logged_in? ⇒ Boolean
313
314
315
316
|
# File 'lib/kaspay.rb', line 313
def logged_in?
return logout_link.exists? unless browser.nil?
return false
end
|
#logged_out? ⇒ Boolean
318
319
320
|
# File 'lib/kaspay.rb', line 318
def logged_out?
return !logged_in?
end
|
#login ⇒ Object
Actually starts the login process after email and password provided. This is different from the class method ‘login` that only creates KasPay object and compounding the data for login.
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/kaspay.rb', line 152
def login
headless = Headless.new
headless.start
@browser = Watir::Browser.start LOGIN_URL
delete_cookies_if_expire
the_cookies = use_cookies
if the_cookies.nil?
browser.text_field(id: 'username').set email
browser.text_field(id: 'password').set password
browser.button(name: 'button').click
save_cookies
else
browser.cookies.clear
the_cookies.each do |cookies|
browser.cookies.add(cookies[:name], cookies[:value])
end
browser.goto BASE_URL
end
end
|
#logout ⇒ Object
309
310
311
|
# File 'lib/kaspay.rb', line 309
def logout
logout! if logged_in?
end
|
#logout! ⇒ Object
302
303
304
305
306
|
# File 'lib/kaspay.rb', line 302
def logout!
calling_method = caller[0][/(?<=`).*(?=')/]
logout_link.click
delete_cookies unless calling_method == "delete_cookies"
end
|
#save_login(login_name) ⇒ Object
326
327
328
329
330
331
332
|
# File 'lib/kaspay.rb', line 326
def save_login login_name
Dir.mkdir(DATA_DIR) unless Dir.exists?(DATA_DIR)
KasPay.login_scope do |login|
login[login_name] = {email: email, password: password}
end
return nil
end
|
#trx_type_to_num(val_in_sym) ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/kaspay.rb', line 223
def trx_type_to_num val_in_sym
num = case val_in_sym
when :all_trx then 0
when :top_up then 1
when :payment_to then 2
when :payment_from then 3
when :redeem then 4
when :refund then 5
when :trx_fee then 6
else 0
end
return num
end
|
#user_data_complete? ⇒ Boolean
322
323
324
|
# File 'lib/kaspay.rb', line 322
def user_data_complete?
!email.nil? && !password.nil?
end
|