Module: DcidevUtility
- Defined in:
- lib/dcidev_utility.rb
Class Method Summary collapse
- .base64_encoded_string(base64) ⇒ Object
- .base64_extension(base64) ⇒ Object
- .base64_to_file(string) ⇒ Object
- .body_simplifier(body) ⇒ Object
- .check_integer(integer) ⇒ Object
- .check_string(string) ⇒ Object
- .currency_formatter(amount, unit: "Rp. ", separator: ".", delimiter: ".", precision: 0) ⇒ Object
- .dob_from_nik(nik) ⇒ Object
- .download_to_file(url) ⇒ Object
- .email_valid?(email) ⇒ Boolean
- .file_to_base64(file) ⇒ Object
- .file_url_to_base64(url) ⇒ Object
- .gender_from_nik(nik) ⇒ Object
- .is_base64?(value) ⇒ Boolean
- .is_numeric?(number) ⇒ Boolean
- .is_phone_number?(phone) ⇒ Boolean
- .json_simplifier(json) ⇒ Object
- .name_validator(string) ⇒ Object
- .original_phone(phone) ⇒ Object
- .phone_converter(number) ⇒ Object
- .random_string_masking(string, length = 0, replace_character: '*') ⇒ Object
- .response_simplifier(response) ⇒ Object
- .seconds_diff(start, finish) ⇒ Object
- .string_masking(string, length = 9, replace_charcter: 'x') ⇒ Object
- .url_exist?(url) ⇒ Boolean
- .valid_json?(json) ⇒ Boolean
- .years_between(date_from, date_to) ⇒ Object
Class Method Details
.base64_encoded_string(base64) ⇒ Object
157 158 159 |
# File 'lib/dcidev_utility.rb', line 157 def base64_encoded_string(base64) base64.split(",").last.strip end |
.base64_extension(base64) ⇒ Object
161 162 163 |
# File 'lib/dcidev_utility.rb', line 161 def base64_extension(base64) base64.split(";").first.split(":").last end |
.base64_to_file(string) ⇒ Object
64 65 66 |
# File 'lib/dcidev_utility.rb', line 64 def base64_to_file(string) Base64.strict_decode64(string) end |
.body_simplifier(body) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/dcidev_utility.rb', line 75 def body_simplifier(body) if body.class == String && (valid_json? body) JSON.parse(body) else body end end |
.check_integer(integer) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/dcidev_utility.rb', line 83 def check_integer(integer) if integer.is_a? String chars = ('a'..'z').to_a + ('A'..'Z').to_a integer.chars.detect { |ch| chars.include?(ch) }.nil? else return true end end |
.check_string(string) ⇒ Object
92 93 94 95 96 |
# File 'lib/dcidev_utility.rb', line 92 def check_string(string) string = string.delete(" ") chars = ('a'..'z').to_a + ('A'..'Z').to_a string.chars.detect { |ch| !chars.include?(ch) }.nil? end |
.currency_formatter(amount, unit: "Rp. ", separator: ".", delimiter: ".", precision: 0) ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/dcidev_utility.rb', line 142 def currency_formatter(amount, unit: "Rp. ", separator: ".", delimiter: ".", precision: 0) begin amount = amount.to_i rescue amount = 0 end ActionController::Base.helpers.number_to_currency(amount, unit: unit, separator: separator, delimiter: delimiter, precision: precision) end |
.dob_from_nik(nik) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/dcidev_utility.rb', line 108 def dob_from_nik(nik) now = Time.now.utc.to_date tanggal_lahir = nik[6..7].to_i if tanggal_lahir > 40 tanggal_lahir = tanggal_lahir - 40 end bulan_lahir = nik[8..9].to_i if bulan_lahir < 10 bulan_lahir = "0" + bulan_lahir.to_s end tahun_lahir = nik[10..11].to_i if (tahun_lahir + 2000) > now.year tahun_lahir = "19" + nik[10..11].to_s else tahun_lahir = "20" + nik[10..11].to_s end if tanggal_lahir.to_s.length == 1 tanggal_lahir = '0' + tanggal_lahir.to_s end dob = tahun_lahir.to_s + "-" + bulan_lahir.to_s + "-" + tanggal_lahir.to_s 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 dob = '1945-08-17' else dob = dob end dob end |
.download_to_file(url) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dcidev_utility.rb', line 19 def download_to_file(url) uri = URI::parse(url) extension = File.extname(uri.path) stream = URI::open(url, "rb") Tempfile.new([File.basename(uri.path), extension]).tap do |file| file.binmode IO.copy_stream(stream, file) stream.close file.rewind end end |
.email_valid?(email) ⇒ Boolean
211 212 213 |
# File 'lib/dcidev_utility.rb', line 211 def email_valid?(email) 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? end |
.file_to_base64(file) ⇒ Object
54 55 56 57 58 |
# File 'lib/dcidev_utility.rb', line 54 def file_to_base64(file) encoded = Base64.strict_encode64(file.read) extension = MimeMagic.by_magic(file).type.to_s [extension, encoded, "data:#{extension};base64,#{encoded}"] end |
.file_url_to_base64(url) ⇒ Object
48 49 50 51 52 |
# File 'lib/dcidev_utility.rb', line 48 def file_url_to_base64(url) return [nil, nil, nil] if url.nil? file = self.download_to_file(url) return self.file_to_base64(file) end |
.gender_from_nik(nik) ⇒ Object
138 139 140 |
# File 'lib/dcidev_utility.rb', line 138 def gender_from_nik(nik) nik[6..7].to_i < 40 ? "L" : "P" end |
.is_base64?(value) ⇒ Boolean
60 61 62 |
# File 'lib/dcidev_utility.rb', line 60 def is_base64?(value) value.is_a?(String) && Base64.strict_encode64(Base64.decode64(value)) == value end |
.is_numeric?(number) ⇒ Boolean
3 4 5 6 7 8 |
# File 'lib/dcidev_utility.rb', line 3 def is_numeric?(number) number = number.to_s data = number.delete("+") result = data =~ /^-?[0-9]+$/ result == 0 end |
.is_phone_number?(phone) ⇒ Boolean
31 32 33 34 |
# File 'lib/dcidev_utility.rb', line 31 def is_phone_number?(phone) chars = ('a'..'z').to_a + ('A'..'Z').to_a phone.chars.detect { |ch| !chars.include?(ch) }.nil? end |
.json_simplifier(json) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/dcidev_utility.rb', line 215 def json_simplifier(json) simplified = {} json.each do |k, value| if value.is_a?(Array) simplified[k] = [] value.each_with_index do |array_value, index_array| simplified[k][index_array] = json_simplifier(array_value) end end if value.is_a?(String) && value.include?(';base64,') begin Base64.strict_decode64(value) value = "base64_#{k.to_s}" rescue => _ value = 'invalid base64' ensure simplified[k] = value end else simplified[k] = value end end return simplified end |
.name_validator(string) ⇒ Object
151 152 153 154 155 |
# File 'lib/dcidev_utility.rb', line 151 def name_validator(string) string = string.to_s.delete(" ") chars = ('a'..'z').to_a + ('A'..'Z').to_a string.chars.detect { |ch| !chars.include?(ch) }.nil? end |
.original_phone(phone) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dcidev_utility.rb', line 36 def original_phone(phone) unless phone.nil? phone = phone.to_s.scan(/\d+/).join return phone.sub('62', '0') if phone[0] == '6' && phone[1] == '2' if phone[0] == '+' && phone[1] == '6' && phone[2] == '2' return phone.sub('+62', '0') end phone end end |
.phone_converter(number) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/dcidev_utility.rb', line 10 def phone_converter(number) return if number.nil? phone = number.to_s.scan(/\d+/).join return phone.sub('0', '62') if number[0] == '0' return phone.sub('+', '') if number[0] == '+' phone end |
.random_string_masking(string, length = 0, replace_character: '*') ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/dcidev_utility.rb', line 170 def random_string_masking(string, length = 0, replace_character: '*') return "" if string.nil? length.clamp(0, string.length).times do random_pos = nil loop do random_pos = rand(0...string.length) if string[random_pos] != replace_character break end end string[random_pos] = replace_character end return string end |
.response_simplifier(response) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/dcidev_utility.rb', line 185 def response_simplifier(response) if response.class == String return response_simplifier(JSON.parse(response)) end return response if response.class == Hash return response if response.nil? if response.class == Net::HTTPInternalServerError || response.class == Net::HTTPCreated || response.class == Net::HTTPBadGateway || response.class == Net::HTTPUnprocessableEntity return response.to_json end if valid_json? response simple_response = JSON.parse(response) else simple_response = response end if simple_response.class == RestClient::Response simple_response = { :error => response.bytes.pack("c*").force_encoding("UTF-8") }.to_json end json_simplifier(simple_response) end |
.seconds_diff(start, finish) ⇒ Object
240 241 242 |
# File 'lib/dcidev_utility.rb', line 240 def seconds_diff(start, finish) (start - finish).seconds.round end |
.string_masking(string, length = 9, replace_charcter: 'x') ⇒ Object
165 166 167 168 |
# File 'lib/dcidev_utility.rb', line 165 def string_masking(string, length = 9, replace_charcter: 'x') return "" if string.nil? return string.sub(string[0...length], replace_charcter * length) end |
.url_exist?(url) ⇒ Boolean
98 99 100 101 102 103 104 105 106 |
# File 'lib/dcidev_utility.rb', line 98 def url_exist?(url) success = true begin success = false unless Net::HTTP.get_response(URI.parse(url)).is_a?(Net::HTTPSuccess) rescue success = false end success end |
.valid_json?(json) ⇒ Boolean
68 69 70 71 72 73 |
# File 'lib/dcidev_utility.rb', line 68 def valid_json?(json) JSON.parse(json) true rescue JSON::ParserError => e return false end |
.years_between(date_from, date_to) ⇒ Object
244 245 246 |
# File 'lib/dcidev_utility.rb', line 244 def years_between(date_from, date_to) (date_from.year - date_to.year).abs end |