kaspay 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3088cb7f94f5cd5b2c29a34c39a19dc233c68f9a
4
+ data.tar.gz: e3248f982d6859a04cecc0b131fbeef12f79ef4b
5
+ SHA512:
6
+ metadata.gz: fab807cd628a82bb18a1a41e090ee71f177ec1b1c0d6741453835a51c74a90c4e242a7062285048b94f51aa8e9b68ff80badf94b7115e16fff9dba279da4c4f6
7
+ data.tar.gz: a615c12b5c77842dab0dea3eaf734d15b958bcf907022dcaf07bba5c94a4b6f9376bb397da21b7ff5c39cb5fb1f057d4c2be953672954ce27b8f36af8f058c8b
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2015 Adrian Setyadi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/kaspay.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kaspay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kaspay"
8
+ spec.version = KasPay::VERSION
9
+ spec.date = "2015-08-24"
10
+
11
+ spec.authors = ["Adrian Setyadi"]
12
+ spec.email = ["a.styd@yahoo.com"]
13
+ spec.summary = %q{Unofficial KasPay access wrapper gem.}
14
+ spec.description = %q{A gem to access KasPay web using watir-webdriver gem and X virtual framebuffer wrapped by headless gem.}
15
+ spec.homepage = "https://github.com/styd/kaspay"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ # spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,3 @@
1
+ class KasPay
2
+ VERSION = "0.0.1"
3
+ end
data/lib/kaspay.rb ADDED
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'watir-webdriver'
4
+ require 'headless'
5
+ require_relative 'meta_stuff'
6
+ require_relative 'kernel_patch'
7
+
8
+ class KasPay
9
+ extend MetaStuff
10
+
11
+ class << self
12
+ static_pages = %w(about term)
13
+ static_pages.each do |page|
14
+ define_method(page) do
15
+ browse BASE_URL if (defined?(@@browser)).nil?
16
+ @@browser.goto "#{BASE_URL}/#{page}"
17
+ @@browser.div(id: "body").text
18
+ end
19
+ end
20
+
21
+ def browse url
22
+ @@headless = Headless.new
23
+ @@headless.start
24
+ @@browser = Watir::Browser.start url
25
+ end
26
+
27
+ alias_method :terms, :term
28
+ alias_method :login, :new
29
+ private :new
30
+ 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
+
37
+ attr_accessor :browser
38
+ attr_accessor :headless
39
+
40
+ def initialize email, password
41
+ headless = Headless.new
42
+ headless.start
43
+
44
+ @browser = Watir::Browser.start LOGIN_URL
45
+
46
+ browser.text_field(id: 'username').set email
47
+ browser.text_field(id: 'password').set password
48
+ browser.button(name: 'button').click
49
+ end
50
+
51
+ def current_url
52
+ browser.url
53
+ end
54
+
55
+ def goto path
56
+ url = (path[0] == "/" ? (BASE_URL + path) : path)
57
+ browser.goto url
58
+ end
59
+
60
+ def get_name
61
+ browser.span(class: "user-name").text
62
+ end
63
+
64
+ def get_balance
65
+ Money.new browser.div(class: "kaspay-balance") \
66
+ .span.text.sub("Rp ","").sub(".","").to_i
67
+ end
68
+
69
+ def get_acc_num
70
+ browser.span(class: "kaspay-id").text.sub("KasPay Account: ", "").to_i
71
+ end
72
+
73
+ def home
74
+ goto "/"
75
+ end
76
+
77
+ def logout!
78
+ logout_link.click
79
+ end
80
+
81
+ def logged_in?
82
+ logout_link.exists?
83
+ end
84
+
85
+ def method_missing(m, *args, &block)
86
+ if THINGS_TO_GET.include? m
87
+ send("get_#{m}")
88
+ else
89
+ raise NoMethodError, "undefined method `#{m}' for #{self}"
90
+ end
91
+ end
92
+
93
+ def check_login
94
+ raise LoginError, "you are not logged in" unless logged_in?
95
+ end
96
+
97
+ before( THE_GET_METHODS + [:logout!] ){ :check_login }
98
+
99
+ alias_method :logout, :logout!
100
+ alias_method :url, :current_url
101
+
102
+ private
103
+
104
+ def logout_link
105
+ logout_link = browser.a(href: "https://www.kaspay.com/account/logout")
106
+ end
107
+
108
+ # inner class Money
109
+ class Money
110
+ attr_accessor :value
111
+
112
+ fixnum_methods_to_discard = %w(inspect -@ abs magnitude to_s dclone ~ & | ^ [] << >> size bit_length to_f).map(&:to_sym)
113
+ new_methods = Fixnum.instance_methods(false) \
114
+ .delete_if{|m| fixnum_methods_to_discard.include? m}
115
+
116
+ new_methods.each do |m|
117
+ define_method(m) do |arg, &block|
118
+ arg = arg.to_i # to convert string or Money object to Integer
119
+ self.value = value.send(m, arg)
120
+ return self
121
+ end
122
+ end
123
+
124
+ def initialize value = 0
125
+ @value = Integer(value)
126
+ end
127
+
128
+ def to_f
129
+ value * 1.0
130
+ end
131
+
132
+ def to_s
133
+ "Rp " + value.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse + ",-"
134
+ end
135
+
136
+ alias_method :to_i, :value
137
+ end
138
+ end
139
+
140
+ class LoginError < StandardError
141
+ end
@@ -0,0 +1,5 @@
1
+ module Kernel
2
+ def add__get__to array
3
+ array.map {|el| "get_#{el}".to_sym}
4
+ end
5
+ end
data/lib/meta_stuff.rb ADDED
@@ -0,0 +1,17 @@
1
+ module MetaStuff
2
+ def method_missing(m, *args, &block)
3
+ raise NoMethodError, "undefined method `#{m}' for #{self}:#{self.class}"
4
+ end
5
+
6
+ def before(names, &block)
7
+ names.each do |name|
8
+ m = instance_method(name)
9
+ n = instance_method(yield)
10
+ define_method(name) do
11
+ n.bind(self).call
12
+ m.bind(self).call
13
+ end
14
+ end
15
+ private yield
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kaspay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Setyadi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem to access KasPay web using watir-webdriver gem and X virtual framebuffer
14
+ wrapped by headless gem.
15
+ email:
16
+ - a.styd@yahoo.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE.txt
22
+ - kaspay.gemspec
23
+ - lib/kaspay.rb
24
+ - lib/kaspay/version.rb
25
+ - lib/kernel_patch.rb
26
+ - lib/meta_stuff.rb
27
+ homepage: https://github.com/styd/kaspay
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.4.8
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Unofficial KasPay access wrapper gem.
51
+ test_files: []