-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rb
More file actions
83 lines (71 loc) · 2.43 KB
/
utils.rb
File metadata and controls
83 lines (71 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Object
def try(m, *args, &block)
if is_a?(NilClass)
self
else
send(m, *args, &block)
end
end
end
class Hash
def slice *keys
select{|k| keys.member?(k)}
end
def except *keys
reject{|k| keys.member?(k)}
end
def deep_symbolize_keys
reduce({}){|memo,(k,v)| memo[k.to_sym] = v.deep_symbolize_keys; memo}
end
end
class Array
def stringify
reduce([]){|a,t| a << t.to_s}
end
end
module Utils
# for filtering sensitive data in hashes
def self.shallow_hash_filter(h, *args)
default_sensitive_attrs = ['password', 'token', 'digest', 'otp', 'image', 'file', 'data']
sensitive_attrs = (default_sensitive_attrs + args).flatten
r = h.dup
r.each do |k,v|
if sensitive_attrs.include?(k.to_s)
r[k] = '[filtered]'
else
r[k] = v
end
end
end
def self.generate_random_token
ROTP::Base32.random_base32
end
def self.error_handler(e, context)
time_spent = Time.now - context.instance_variable_get(:@start_time)
Mailer.sendmail("/app/exception", e, context, time_spent)
event_hash = {
type: 'exception', title: "[#{time_spent} sec] #{e.class}: #{e.message[0..255]}", body: e.backtrace,
params: self.shallow_hash_filter(context.request.params),
env: self.shallow_hash_filter(context.env, 'roda.json_params')
}
Event.create(event_hash)
LOGGER.error "APP ERROR:\n"
LOGGER.error event_hash[:title]
LOGGER.error event_hash[:body].join("\n")
LOGGER.error self.format_hash[event_hash[:params]]
LOGGER.error self.format_hash[event_hash[:env]]
end
def self.format_hash
lambda{|h| h.map{|k, v| "#{k.inspect} => #{v.inspect}"}.sort.join("\n")}
end
def self.random_pin(size)
('0' * size + rand(('9' * size).to_i).to_s)[-size..-1]
end
def self.translit(str)
str.tr( "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz")
end
def self.to_money(int)
"%.2f" % (int / 100.0).to_s
end
end