kaspay 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -1
  3. data/lib/kaspay/version.rb +1 -1
  4. data/lib/kaspay.rb +77 -12
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe31038fcff900af5444f5018b64e335bce662c6
4
- data.tar.gz: 4db5d733155f0b6e3f57d3622d16a28e1faabfd7
3
+ metadata.gz: 43081c96cd556ee5cc3f775ef57e9b4b2f7139e9
4
+ data.tar.gz: be34341f29d037b715d41cb0fb2f4366c6975690
5
5
  SHA512:
6
- metadata.gz: 1701cdb520bfbbb93e12d5af8f39f1b4bfaa6fb292a675fb251310c9b21fea74d98344d9cee01baf1546ad3aaf5b446b5e369c55b7a0e82e7f0e365a37569378
7
- data.tar.gz: b74d09a869b6a16953bc53fd88d64cca14b4411c961ecac76b2feac2bbab0bd7e864f386cf499b4d6fd8f34892569b3fd8f07dbac2a34065c695eca347064f3b
6
+ metadata.gz: 2a2fc30b97487116b4761f12110d435d046da79e6d293aabde5f49ac014c821ecdecb17ef3c0964705c223b8ffc8f41ce39bab261eb375639869d8b3519df676
7
+ data.tar.gz: a81f7ade37886a8b8b9e0c5ca87b33532acdd96aaa41aca62ff88e9978b70445958af9362077f5d28d6375e1e6be5b1c0bcada06d77f7e3aec72f806f0bc5217
data/README.md CHANGED
@@ -21,15 +21,34 @@ In `Gemfile`:
21
21
  gem 'kaspay'
22
22
 
23
23
  ## Examples
24
- ###Code
24
+ ### Code
25
25
  ```ruby
26
26
  require 'kaspay'
27
27
 
28
28
  kaspay = KasPay.login email: "email@example.com", password: "yOurp@sSw0rD"
29
+ # Alternative:
30
+ # kaspay = KasPay.login
31
+ # kaspay.email = "email@example.com"
32
+ # kaspay.password = "yOurp@sSw0rD"
29
33
  puts "#{kaspay.get_name}'s savings with account number #{kaspay.get_acc_num}:"
34
+ # `<method>` is an alias for `get_<method>`. So, you can use `kaspay.name` or `kaspay.acc_num` instead.
30
35
  puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
31
36
  puts " Bank A balance".ljust(15) + ": " + "Rp 500,000.00".rjust(20)
32
37
  puts " Bank B balance".ljust(15) + ": " + "Rp 600,000.00".rjust(20)
33
38
  puts " KasPay balance".ljust(15) + ": " + kaspay.get_balance.to_s.rjust(20)
34
39
  puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
40
+ puts " Total balance".ljust(15) + ": " + (kaspay.balance + 500000 + KasPay::Money.new(600000)).to_s.rjust(20)
41
+ puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
42
+ kaspay.logout!
43
+ ```
44
+ ### Output
45
+ ```ruby
46
+ #=> John Doe's savings with account number 12345678:
47
+ #=> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48
+ #=> Bank A balance: Rp 500,000.00
49
+ #=> Bank B balance: Rp 600,000.00
50
+ #=> KasPay balance: Rp 915,826.00
51
+ #=> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52
+ #=> Total balance : Rp 2,015,826.00
53
+ #=> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35
54
  ```
@@ -1,3 +1,3 @@
1
1
  class KasPay
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/kaspay.rb CHANGED
@@ -1,55 +1,91 @@
1
1
  #!/usr/bin/env ruby
2
-
2
+ # @author Adrian Setyadi
3
+ # Copyright 2015
4
+ #
5
+ require 'pstore'
3
6
  require 'watir'
4
7
  require 'headless'
5
8
  require_relative 'meta_stuff'
6
9
  require_relative 'kernel_patch'
7
10
 
8
11
  class KasPay
12
+ # Assign methods from MetaStuff module as KasPay class methods
9
13
  extend MetaStuff
10
14
 
15
+ # A bunch of constants belong to KasPay.
16
+ BASE_URL = "https://www.kaspay.com"
17
+ LOGIN_URL = BASE_URL + "/login"
18
+ THINGS_TO_GET = %w(name balance acc_num).map(&:to_sym)
19
+ THE_GET_METHODS = add__get__to(THINGS_TO_GET)
20
+ DATA_DIR = ENV['HOME'] + "/.kaspay"
21
+ DATA_PATH = DATA_DIR + "/kaspay.dat"
22
+
23
+ # Opening KasPay class singleton scope
11
24
  class << self
12
- static_pages = %w(about term)
25
+
26
+ # A list of kaspay.com static pages to open for the sake of
27
+ # the information.
28
+ static_pages = %w(about term) # more will come
29
+
30
+ # Iterate through static pages list to make methods named the
31
+ # the same as the member of the list. These methods will be
32
+ # the class methods of KasPay.
33
+ # Example usage:
34
+ # `about_page = KasPay.about`
13
35
  static_pages.each do |page|
14
36
  define_method(page) do
37
+ # Watir::Browser object is created when it's not exist.
15
38
  browse BASE_URL if (defined?(@@browser)).nil?
16
39
  @@browser.goto "#{BASE_URL}/#{page}"
17
40
  @@browser.div(id: "body").text
18
41
  end
19
42
  end
20
43
 
44
+ # Creates Watir::Browser object for navigating web pages.
21
45
  def browse url
22
46
  @@headless = Headless.new
23
47
  @@headless.start
24
48
  @@browser = Watir::Browser.start url
25
49
  end
26
50
 
51
+ # Creates an alias for `KasPay.term` method, which is
52
+ # `KasPay.terms`
27
53
  alias_method :terms, :term
54
+
55
+ # Changes class method `new` to `login` (the more natural
56
+ # name) to make an instance of KasPay.
28
57
  alias_method :login, :new
58
+ # Hidden to force the use of `login` as the class method
59
+ # for instantiation.
29
60
  private :new
30
61
  end
31
-
32
- BASE_URL = "https://www.kaspay.com"
33
- LOGIN_URL = BASE_URL + "/login"
34
- THINGS_TO_GET = %w(name balance acc_num).map(&:to_sym)
35
- THE_GET_METHODS = add__get__to(THINGS_TO_GET)
36
62
 
63
+ # Creates browser and headless methods for browsing without
64
+ # a visible browser or 'head', and email and password methods
65
+ # for reading @email and @password instance variables.
37
66
  attr_accessor :browser
38
67
  attr_accessor :headless
39
68
  attr_reader :email
40
69
  attr_reader :password
70
+
71
+ # Some methods need to be inaccessible
41
72
  private :headless
42
73
  private :headless=
43
74
  private :password
44
75
 
45
- def initialize user = { email: "", password: "" }
46
- unless user[:email] == "" || user[:password] == ""
76
+ # Accept a hash argument from class methods `login`
77
+ def initialize user = { email: nil, password: nil }
78
+ unless user[:email] == nil || user[:password] == nil
47
79
  @email = user[:email]
48
80
  @password = user[:password]
49
81
  login
50
82
  end
51
83
  end
52
84
 
85
+ # Actually starts the login process after email and password
86
+ # provided. This is actualy different from the class method
87
+ # `login` that only creates KasPay object and compounding the
88
+ # data for login.
53
89
  def login
54
90
  headless = Headless.new
55
91
  headless.start
@@ -61,16 +97,18 @@ class KasPay
61
97
  browser.button(name: 'button').click
62
98
  end
63
99
 
100
+ # Sets email input
64
101
  def email= mail
65
102
  @email = mail
66
103
  login if user_data_complete?
67
104
  end
68
105
 
106
+ # Sets password input
69
107
  def password= pass
70
108
  @password = pass
71
109
  login if user_data_complete?
72
110
  end
73
-
111
+
74
112
  def current_url
75
113
  browser.url
76
114
  end
@@ -111,9 +149,26 @@ class KasPay
111
149
  end
112
150
 
113
151
  def user_data_complete?
114
- email != "" && password != ""
152
+ !email.nil? && !password.nil?
115
153
  end
116
154
 
155
+ def save_login login_name
156
+ Dir.mkdir(DATA_DIR) unless Dir.exists?(DATA_DIR)
157
+ kasdb = PStore.new(DATA_PATH)
158
+ kasdb.transaction do
159
+ kasdb[login_name] = {email: email, password: password}
160
+ end
161
+ end
162
+
163
+ def load_login login_name
164
+ kasdb = PStore.new(DATA_PATH)
165
+ kasdb.transaction do
166
+ @email = kasdb[login_name][:email]
167
+ @password = kasdb[login_name][:password]
168
+ return kasdb[login_name]
169
+ end
170
+ end
171
+
117
172
  def inspect
118
173
  "#<#{self.class}:0x#{(object_id << 1).to_s(16)} logged_in=#{logged_in?}>"
119
174
  end
@@ -122,7 +177,7 @@ class KasPay
122
177
  if THINGS_TO_GET.include? m
123
178
  send("get_#{m}")
124
179
  else
125
- raise NoMethodError, "undefined method `#{m}' for #{self}"
180
+ super
126
181
  end
127
182
  end
128
183
 
@@ -137,6 +192,16 @@ class KasPay
137
192
 
138
193
  private
139
194
 
195
+ def login_data_exists?
196
+ data = nil
197
+ begin
198
+ PStore.new("kaspay.dat").tap{|x| x.transaction{ data = x.roots}}
199
+ return (data != [])
200
+ rescue NameError
201
+ return false
202
+ end
203
+ end
204
+
140
205
  def logout_link
141
206
  logout_link = browser.a(href: "https://www.kaspay.com/account/logout")
142
207
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaspay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Setyadi