dcidev_utility 0.0.1 → 0.0.2

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +52 -1
  3. data/lib/dcidev_utility.rb +176 -174
  4. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa0c58aa35c1f7c87e63d092e841072a67b41fd6be491b1e40ead581db5958d8
4
- data.tar.gz: 9dfa55c2d4dc429e978b2c16a4cc081025203fe6854665adfe8093b47a83b1fd
3
+ metadata.gz: 1b79e4bc2680eb78f5c13a5df23a4bb73968f22b65b43f8cc9b7857d68823922
4
+ data.tar.gz: a1c143e52268fe952b3c4c2939e88f339fe7c4b6f025a84a97f16bad0f447281
5
5
  SHA512:
6
- metadata.gz: 76ccced64bf049a28ae60333b2dd105674b4b38b95410b2701b460a47281d3a857d9ccabe04b76f678943533d18deaa4a82d1c55d3b92ce4945797f990c47174
7
- data.tar.gz: 8b23680a78d0cdb0956241f3e5b3abff8c88aaf18644d628d97db983eabf982100f30506e5a3486f84482ee9c587331fb0afec39e42a1df65701e9459ec25e0a
6
+ metadata.gz: 2b7b73e76edea0ec734ac415d5935dc3d3aa50d96ef2fbe9d71b3e01f8ec5106c4508e8b5f59c346dbf88012e52a81f563bde194eed6a939c4cc75e682f6c445
7
+ data.tar.gz: 966e8df807768ecaf1ac81d367ba6d0e3d58072acbabf0c379dc9ef9f557199093386b5f84a3fb724c48e59923bb2897f92e72895b20deb7eb9cb04b21706c84
data/README.md CHANGED
@@ -1 +1,52 @@
1
- dcidev_utility
1
+ # Features
2
+
3
+ ```ruby
4
+ # check if numeric value
5
+ DcidevUtility.is_numeric?(number)
6
+
7
+ # convert phone number prefixed with '+62' or '0' to '62' format
8
+ # +628xxxxx --> 628xxxxx
9
+ DcidevUtility.phone_converter(phone)
10
+
11
+ # download file from url and save it as Tempfile
12
+ DcidevUtility.download_to_file(url)
13
+
14
+ # check if value is a phone number
15
+ DcidevUtility.is_phone_number?(phone)
16
+
17
+ # revert 62xx phone number format to 08xxx format
18
+ DcidevUtility.original_phone(phone)
19
+
20
+ # download file from url and return it as base64
21
+ # the returned value will be an array containing [extension, encoded_string, full_base64_string]
22
+ DcidevUtility.file_url_to_base64(url)
23
+
24
+ # check if base64
25
+ DcidevUtility.is_base64?(string)
26
+
27
+ # encode base64 to Tempfile
28
+ DcidevUtility.base64_to_file(string)
29
+
30
+ # check if url valid
31
+ DcidevUtility.url_exists?(url)
32
+
33
+ # extract dob from nik
34
+ DcidevUtility.dob_from_nik(nik)
35
+
36
+ # extract gender from nik
37
+ DcidevUtility.gender_from_nik(nik)
38
+
39
+ # convert integer value to formatted string currency
40
+ DcidevUtility.currency_formatter(amount, unit: "Rp. ", separator: ".", delimiter: ".", precision: 0)
41
+
42
+ # extract encoded string from base64
43
+ DcidevUtility.base64_encoded_string(base64)
44
+
45
+ # extract extension from base64
46
+ DcidevUtility.base64_extension(base64)
47
+
48
+ # mask a string
49
+ DcidevUtility.string_masking(string, length = 9)
50
+
51
+ DcidevUtility.response_simplifier(response)
52
+ ```
@@ -1,202 +1,204 @@
1
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] == '+'
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
15
9
 
16
- phone
17
- end
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] == '+'
18
15
 
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
16
+ phone
17
+ end
34
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
35
34
 
35
+ def is_phone_number?(phone)
36
+ chars = ('a'..'z').to_a + ('A'..'Z').to_a
37
+ phone.chars.detect { |ch| !chars.include?(ch) }.nil?
38
+ end
36
39
 
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
40
+ def original_phone(phone)
41
+ unless phone.nil?
42
+ phone = phone.to_s.scan(/\d+/).join
43
+ return phone.sub('62', '0') if phone[0] == '6' && phone[1] == '2'
44
+ if phone[0] == '+' && phone[1] == '6' && phone[2] == '2'
45
+ return phone.sub('+62', '0')
46
+ end
41
47
 
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
+ phone
49
+ end
48
50
  end
49
51
 
50
- phone
51
- end
52
- end
52
+ def file_url_to_base64(url)
53
+ return [nil, nil, nil] if url.nil?
54
+ file = self.download_to_file(url)
55
+ return self.file_to_base64(file)
56
+ end
53
57
 
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
58
+ def file_to_base64(file)
59
+ encoded = Base64.strict_encode64(file.read)
60
+ extension = MimeMagic.by_magic(file).type.to_s
61
+ [extension, encoded, "data:#{extension};base64,#{encoded}"]
62
+ end
59
63
 
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
64
+ def is_base64?(value)
65
+ value.is_a?(String) && Base64.strict_encode64(Base64.decode64(value)) == value
66
+ end
65
67
 
66
- def is_base64?(value)
67
- value.is_a?(String) && Base64.strict_encode64(Base64.decode64(value)) == value
68
- end
68
+ def base64_to_file(string)
69
+ Base64.strict_decode64(string)
70
+ end
69
71
 
70
- def base64_to_file(string)
71
- Base64.strict_decode64(string)
72
- end
72
+ def valid_json?(json)
73
+ JSON.parse(json)
74
+ true
75
+ rescue JSON::ParserError => e
76
+ return false
77
+ end
73
78
 
74
- def valid_json?(json)
75
- JSON.parse(json)
76
- true
77
- rescue JSON::ParserError => e
78
- return false
79
- end
79
+ def body_simplifier(body)
80
+ if body.class == String && (valid_json? body)
81
+ JSON.parse(body)
82
+ else
83
+ body
84
+ end
85
+ end
80
86
 
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
87
+ def check_integer(integer)
88
+ if integer.is_a? String
89
+ chars = ('a'..'z').to_a + ('A'..'Z').to_a
90
+ integer.chars.detect { |ch| chars.include?(ch) }.nil?
91
+ else
92
+ return true
93
+ end
94
+ end
88
95
 
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
96
+ def check_string(string)
97
+ string = string.delete(" ")
98
+ chars = ('a'..'z').to_a + ('A'..'Z').to_a
99
+ string.chars.detect { |ch| !chars.include?(ch) }.nil?
100
+ end
97
101
 
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
102
+ def url_exist?(url)
103
+ success = true
104
+ begin
105
+ success = false unless Net::HTTP.get_response(URI.parse(url)).is_a?(Net::HTTPSuccess)
106
+ rescue
107
+ success = false
108
+ end
109
+ success
110
+ end
103
111
 
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
112
+ def dob_from_nik(nik)
113
+ now = Time.now.utc.to_date
114
+ tanggal_lahir = nik[6..7].to_i
115
+ if tanggal_lahir > 40
116
+ tanggal_lahir = tanggal_lahir - 40
117
+ end
118
+ bulan_lahir = nik[8..9].to_i
119
+ if bulan_lahir < 10
120
+ bulan_lahir = "0" + bulan_lahir.to_s
121
+ end
122
+ tahun_lahir = nik[10..11].to_i
123
+ if (tahun_lahir + 2000) > now.year
124
+ tahun_lahir = "19" + nik[10..11].to_s
125
+ else
126
+ tahun_lahir = "20" + nik[10..11].to_s
127
+ end
128
+
129
+ if tanggal_lahir.to_s.length == 1
130
+ tanggal_lahir = '0' + tanggal_lahir.to_s
131
+ end
132
+
133
+ dob = tahun_lahir.to_s + "-" + bulan_lahir.to_s + "-" + tanggal_lahir.to_s
134
+ 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
135
+ dob = '1945-08-17'
136
+ else
137
+ dob = dob
138
+ end
139
+ dob
140
+ end
113
141
 
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
142
+ def gender_from_nik(nik)
143
+ nik[6..7].to_i < 40 ? "L" : "P"
144
+ end
143
145
 
144
- def gender_from_nik(nik)
145
- nik[6..7].to_i < 40 ? "L" : "P"
146
- end
146
+ def currency_formatter(amount, unit: "Rp. ", separator: ".", delimiter: ".", precision: 0)
147
+ begin
148
+ amount = amount.to_i
149
+ rescue
150
+ amount = 0
151
+ end
152
+ ActionController::Base.helpers.number_to_currency(amount, unit: unit, separator: separator, delimiter: delimiter, precision: precision)
153
+ end
147
154
 
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
155
+ def name_validator(string)
156
+ string = string.to_s.delete(" ")
157
+ chars = ('a'..'z').to_a + ('A'..'Z').to_a
158
+ string.chars.detect { |ch| !chars.include?(ch) }.nil?
159
+ end
156
160
 
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
161
+ def base64_encoded_string(base64)
162
+ base64.split(",").last.strip
163
+ end
162
164
 
163
- def base64_encoded_string(base64)
164
- base64.split(",").last.strip
165
- end
165
+ def base64_extension(base64)
166
+ base64.split(";").first.split(":").last
167
+ end
166
168
 
167
- def base64_extension(base64)
168
- base64.split(";").first.split(":").last
169
- end
169
+ def string_masking(string, length = 9)
170
+ return "" if string.nil?
171
+ return string.sub(string[0...length], 'x' * length)
172
+ end
170
173
 
171
- def string_masking(string, length = 9)
172
- return "" if string.nil?
173
- return string.sub(string[0...length], 'x' * length)
174
- end
174
+ def response_simplifier(response)
175
+ if response.class == String
176
+ return JSON.parse response
177
+ end
178
+ return response if response.class == Hash
179
+ return response if response.nil?
180
+
181
+ if response.class == Net::HTTPInternalServerError || response.class == Net::HTTPCreated || response.class == Net::HTTPBadGateway || response.class == Net::HTTPUnprocessableEntity
182
+ return response.to_json
183
+ end
184
+
185
+ if valid_json? response
186
+ simple_response = JSON.parse(response)
187
+ else
188
+ simple_response = response
189
+ end
190
+
191
+ if simple_response.class == RestClient::Response
192
+ simple_response = {
193
+ :error => response.bytes.pack("c*").force_encoding("UTF-8")
194
+ }.to_json
195
+ end
196
+
197
+ simple_response
198
+ end
175
199
 
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
+ def email_valid?(email)
201
+ email.to_s.match(/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/).present?
202
+ end
200
203
  end
201
- end
202
204
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dcidev_utility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Punto Damar P
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-05 00:00:00.000000000 Z
11
+ date: 2022-04-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Testing phase
14
14
  email:
@@ -19,10 +19,10 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - README.md
21
21
  - lib/dcidev_utility.rb
22
- homepage:
22
+ homepage:
23
23
  licenses: []
24
24
  metadata: {}
25
- post_install_message:
25
+ post_install_message:
26
26
  rdoc_options: []
27
27
  require_paths:
28
28
  - lib
@@ -37,8 +37,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  requirements: []
40
- rubygems_version: 3.0.6
41
- signing_key:
40
+ rubygems_version: 3.1.2
41
+ signing_key:
42
42
  specification_version: 4
43
43
  summary: Commonly used methods used in DCI
44
44
  test_files: []