dcidev_utility 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +1 -0
- data/lib/dcidev_utility.rb +202 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aa0c58aa35c1f7c87e63d092e841072a67b41fd6be491b1e40ead581db5958d8
|
4
|
+
data.tar.gz: 9dfa55c2d4dc429e978b2c16a4cc081025203fe6854665adfe8093b47a83b1fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 76ccced64bf049a28ae60333b2dd105674b4b38b95410b2701b460a47281d3a857d9ccabe04b76f678943533d18deaa4a82d1c55d3b92ce4945797f990c47174
|
7
|
+
data.tar.gz: 8b23680a78d0cdb0956241f3e5b3abff8c88aaf18644d628d97db983eabf982100f30506e5a3486f84482ee9c587331fb0afec39e42a1df65701e9459ec25e0a
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
dcidev_utility
|
@@ -0,0 +1,202 @@
|
|
1
|
+
module DcidevUtility
|
2
|
+
class << self
|
3
|
+
def is_numeric?(number)
|
4
|
+
number = number.to_s
|
5
|
+
data = number.delete("+")
|
6
|
+
result = data =~ /^-?[0-9]+$/
|
7
|
+
result == 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def phone_converter(number)
|
11
|
+
return if number.nil?
|
12
|
+
phone = number.to_s.scan(/\d+/).join
|
13
|
+
return phone.sub('0', '62') if number[0] == '0'
|
14
|
+
return phone.sub('+', '') if number[0] == '+'
|
15
|
+
|
16
|
+
phone
|
17
|
+
end
|
18
|
+
|
19
|
+
def download_to_file(url)
|
20
|
+
begin
|
21
|
+
uri = URI::parse(url)
|
22
|
+
extension = File.extname(uri.path)
|
23
|
+
stream = URI::open(url, "rb")
|
24
|
+
Tempfile.new([File.basename(uri.path), extension]).tap do |file|
|
25
|
+
file.binmode
|
26
|
+
IO.copy_stream(stream, file)
|
27
|
+
stream.close
|
28
|
+
file.rewind
|
29
|
+
end
|
30
|
+
rescue => e
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
def is_phone_number?(phone)
|
38
|
+
chars = ('a'..'z').to_a + ('A'..'Z').to_a
|
39
|
+
phone.chars.detect { |ch| !chars.include?(ch) }.nil?
|
40
|
+
end
|
41
|
+
|
42
|
+
def original_phone(phone)
|
43
|
+
unless phone.nil?
|
44
|
+
phone = phone.to_s.scan(/\d+/).join
|
45
|
+
return phone.sub('62', '0') if phone[0] == '6' && phone[1] == '2'
|
46
|
+
if phone[0] == '+' && phone[1] == '6' && phone[2] == '2'
|
47
|
+
return phone.sub('+62', '0')
|
48
|
+
end
|
49
|
+
|
50
|
+
phone
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def file_url_to_base64(url)
|
55
|
+
return [nil, nil, nil] if url.nil?
|
56
|
+
file = self.download_to_file(url)
|
57
|
+
return self.file_to_base64(file)
|
58
|
+
end
|
59
|
+
|
60
|
+
def file_to_base64(file)
|
61
|
+
encoded = Base64.strict_encode64(file.read)
|
62
|
+
extension = MimeMagic.by_magic(file).type.to_s
|
63
|
+
[extension, encoded, "data:#{extension};base64,#{encoded}"]
|
64
|
+
end
|
65
|
+
|
66
|
+
def is_base64?(value)
|
67
|
+
value.is_a?(String) && Base64.strict_encode64(Base64.decode64(value)) == value
|
68
|
+
end
|
69
|
+
|
70
|
+
def base64_to_file(string)
|
71
|
+
Base64.strict_decode64(string)
|
72
|
+
end
|
73
|
+
|
74
|
+
def valid_json?(json)
|
75
|
+
JSON.parse(json)
|
76
|
+
true
|
77
|
+
rescue JSON::ParserError => e
|
78
|
+
return false
|
79
|
+
end
|
80
|
+
|
81
|
+
def body_simplifier(body)
|
82
|
+
if body.class == String && (valid_json? body)
|
83
|
+
JSON.parse(body)
|
84
|
+
else
|
85
|
+
body
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def check_integer(integer)
|
90
|
+
if integer.is_a? String
|
91
|
+
chars = ('a'..'z').to_a + ('A'..'Z').to_a
|
92
|
+
integer.chars.detect { |ch| chars.include?(ch) }.nil?
|
93
|
+
else
|
94
|
+
return true
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def check_string(string)
|
99
|
+
string = string.delete(" ")
|
100
|
+
chars = ('a'..'z').to_a + ('A'..'Z').to_a
|
101
|
+
string.chars.detect { |ch| !chars.include?(ch) }.nil?
|
102
|
+
end
|
103
|
+
|
104
|
+
def url_exist?(url)
|
105
|
+
success = true
|
106
|
+
begin
|
107
|
+
success = false unless Net::HTTP.get_response(URI.parse(url)).is_a?(Net::HTTPSuccess)
|
108
|
+
rescue
|
109
|
+
success = false
|
110
|
+
end
|
111
|
+
success
|
112
|
+
end
|
113
|
+
|
114
|
+
def dob_from_nik(nik)
|
115
|
+
now = Time.now.utc.to_date
|
116
|
+
tanggal_lahir = nik[6..7].to_i
|
117
|
+
if tanggal_lahir > 40
|
118
|
+
tanggal_lahir = tanggal_lahir - 40
|
119
|
+
end
|
120
|
+
bulan_lahir = nik[8..9].to_i
|
121
|
+
if bulan_lahir < 10
|
122
|
+
bulan_lahir = "0" + bulan_lahir.to_s
|
123
|
+
end
|
124
|
+
tahun_lahir = nik[10..11].to_i
|
125
|
+
if (tahun_lahir + 2000) > now.year
|
126
|
+
tahun_lahir = "19" + nik[10..11].to_s
|
127
|
+
else
|
128
|
+
tahun_lahir = "20" + nik[10..11].to_s
|
129
|
+
end
|
130
|
+
|
131
|
+
if tanggal_lahir.to_s.length == 1
|
132
|
+
tanggal_lahir = '0' + tanggal_lahir.to_s
|
133
|
+
end
|
134
|
+
|
135
|
+
dob = tahun_lahir.to_s + "-" + bulan_lahir.to_s + "-" + tanggal_lahir.to_s
|
136
|
+
if tahun_lahir.to_i > now.year or bulan_lahir.to_i > 12 or tanggal_lahir.to_i > 31 or tahun_lahir.to_i == 0 or bulan_lahir.to_i == 0 or tanggal_lahir.to_i == 0
|
137
|
+
dob = '1945-08-17'
|
138
|
+
else
|
139
|
+
dob = dob
|
140
|
+
end
|
141
|
+
dob
|
142
|
+
end
|
143
|
+
|
144
|
+
def gender_from_nik(nik)
|
145
|
+
nik[6..7].to_i < 40 ? "L" : "P"
|
146
|
+
end
|
147
|
+
|
148
|
+
def currency_formatter(amount, unit: "Rp. ", separator: ".", delimiter: ".", precision: 0)
|
149
|
+
begin
|
150
|
+
amount = amount.to_i
|
151
|
+
rescue
|
152
|
+
amount = 0
|
153
|
+
end
|
154
|
+
ActionController::Base.helpers.number_to_currency(amount, unit: unit, separator: separator, delimiter: delimiter, precision: precision)
|
155
|
+
end
|
156
|
+
|
157
|
+
def name_validator(string)
|
158
|
+
string = string.to_s.delete(" ")
|
159
|
+
chars = ('a'..'z').to_a + ('A'..'Z').to_a
|
160
|
+
string.chars.detect {|ch| !chars.include?(ch)}.nil?
|
161
|
+
end
|
162
|
+
|
163
|
+
def base64_encoded_string(base64)
|
164
|
+
base64.split(",").last.strip
|
165
|
+
end
|
166
|
+
|
167
|
+
def base64_extension(base64)
|
168
|
+
base64.split(";").first.split(":").last
|
169
|
+
end
|
170
|
+
|
171
|
+
def string_masking(string, length = 9)
|
172
|
+
return "" if string.nil?
|
173
|
+
return string.sub(string[0...length], 'x' * length)
|
174
|
+
end
|
175
|
+
|
176
|
+
def response_simplifier(response)
|
177
|
+
if response.class == String
|
178
|
+
return JSON.parse response
|
179
|
+
end
|
180
|
+
return response if response.class == Hash
|
181
|
+
return response if response.nil?
|
182
|
+
|
183
|
+
if response.class == Net::HTTPInternalServerError || response.class == Net::HTTPCreated || response.class == Net::HTTPBadGateway || response.class == Net::HTTPUnprocessableEntity
|
184
|
+
return response.to_json
|
185
|
+
end
|
186
|
+
|
187
|
+
if valid_json? response
|
188
|
+
simple_response = JSON.parse(response)
|
189
|
+
else
|
190
|
+
simple_response = response
|
191
|
+
end
|
192
|
+
|
193
|
+
if simple_response.class == RestClient::Response
|
194
|
+
simple_response = {
|
195
|
+
:error => response.bytes.pack("c*").force_encoding("UTF-8")
|
196
|
+
}.to_json
|
197
|
+
end
|
198
|
+
|
199
|
+
simple_response
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dcidev_utility
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Punto Damar P
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-01-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Testing phase
|
14
|
+
email:
|
15
|
+
- punto@privyid.tech
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib/dcidev_utility.rb
|
22
|
+
homepage:
|
23
|
+
licenses: []
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.0.6
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Commonly used methods used in DCI
|
44
|
+
test_files: []
|