Module: DcidevUtility

Defined in:
lib/dcidev_utility.rb

Class Method Summary collapse

Class Method Details

.base64_encoded_string(base64) ⇒ Object



161
162
163
# File 'lib/dcidev_utility.rb', line 161

def base64_encoded_string(base64)
    base64.split(",").last.strip
end

.base64_extension(base64) ⇒ Object



165
166
167
# File 'lib/dcidev_utility.rb', line 165

def base64_extension(base64)
    base64.split(";").first.split(":").last
end

.base64_to_file(string) ⇒ Object



68
69
70
# File 'lib/dcidev_utility.rb', line 68

def base64_to_file(string)
    Base64.strict_decode64(string)
end

.body_simplifier(body) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/dcidev_utility.rb', line 79

def body_simplifier(body)
    if body.class == String && (valid_json? body)
        JSON.parse(body)
    else
        body
    end
end

.check_integer(integer) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/dcidev_utility.rb', line 87

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



96
97
98
99
100
# File 'lib/dcidev_utility.rb', line 96

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



146
147
148
149
150
151
152
153
# File 'lib/dcidev_utility.rb', line 146

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



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
137
138
139
140
# File 'lib/dcidev_utility.rb', line 112

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
30
31
32
33
# File 'lib/dcidev_utility.rb', line 19

def download_to_file(url)
    begin
        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
    rescue => e
        return nil
    end
end

.email_valid?(email) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/dcidev_utility.rb', line 200

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



58
59
60
61
62
# File 'lib/dcidev_utility.rb', line 58

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



52
53
54
55
56
# File 'lib/dcidev_utility.rb', line 52

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



142
143
144
# File 'lib/dcidev_utility.rb', line 142

def gender_from_nik(nik)
    nik[6..7].to_i < 40 ? "L" : "P"
end

.is_base64?(value) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/dcidev_utility.rb', line 64

def is_base64?(value)
    value.is_a?(String) && Base64.strict_encode64(Base64.decode64(value)) == value
end

.is_numeric?(number) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/dcidev_utility.rb', line 35

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



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/dcidev_utility.rb', line 204

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



155
156
157
158
159
# File 'lib/dcidev_utility.rb', line 155

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



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dcidev_utility.rb', line 40

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

.response_simplifier(response) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/dcidev_utility.rb', line 174

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

.string_masking(string, length = 9) ⇒ Object



169
170
171
172
# File 'lib/dcidev_utility.rb', line 169

def string_masking(string, length = 9)
    return "" if string.nil?
    return string.sub(string[0...length], 'x' * length)
end

.url_exist?(url) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
# File 'lib/dcidev_utility.rb', line 102

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

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/dcidev_utility.rb', line 72

def valid_json?(json)
    JSON.parse(json)
    true
rescue JSON::ParserError => e
    return false
end