-
Notifications
You must be signed in to change notification settings - Fork 26
/
storage.rb
64 lines (57 loc) · 2.02 KB
/
storage.rb
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
class RailsExceptionHandler::Storage
def self.active_record(info)
if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0
RailsExceptionHandler::ActiveRecord::ErrorMessage.create(info, :without_protection => true) if RailsExceptionHandler::ActiveRecord::ErrorMessage.respond_to?(:create)
else
RailsExceptionHandler::ActiveRecord::ErrorMessage.create(info) if RailsExceptionHandler::ActiveRecord::ErrorMessage.respond_to?(:create)
end
end
def self.mongoid(info)
if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0
RailsExceptionHandler::Mongoid::ErrorMessage.create(info, :without_protection => true) if RailsExceptionHandler::Mongoid::ErrorMessage.respond_to?(:create)
else
RailsExceptionHandler::Mongoid::ErrorMessage.create(info) if RailsExceptionHandler::Mongoid::ErrorMessage.respond_to?(:create)
end
end
def self.rails_log(info)
message = ""
info.each do |key,val|
message += "#{key.to_s.upcase}: #{val.to_s}\n"
end
Rails.logger.fatal(message)
end
def self.remote_url(target, info)
uri = URI.parse(target)
params = flatten_hash({:error_message => info})
Net::HTTP::post_form(uri, params)
end
# Notify application admin that an error occured
def self.email(recipients,info)
ErrorMailer.send_error_mail_to_admin(info.to_json,recipients).deliver_later unless recipients.blank?
end
private
# Credit: Hash flattening technique borrowed from Peter Marklund: http://marklunds.com/articles/one/314
def self.flatten_hash(hash, ancestor_names = [])
flat_hash = {}
hash.each do |k, v|
names = Array.new(ancestor_names)
names << k
if v.is_a?(Hash)
flat_hash.merge!(flatten_hash(v, names))
else
key = flat_hash_key(names)
key += "[]" if v.is_a?(Array)
flat_hash[key] = v
end
end
flat_hash
end
def self.flat_hash_key(names)
names = Array.new(names)
name = names.shift.to_s.dup
names.each do |n|
name << "[#{n}]"
end
name
end
end