Module: NoticeFormatter
- Defined in:
- lib/notice_formatter.rb
Class Method Summary collapse
- .add_namespace(name, keywords) ⇒ Object
- .format_event(event, time = Time.now) ⇒ Object
- .format_integer_json_value(value) ⇒ Object
- .format_integer_value(value) ⇒ Object
- .format_part_of_date(date) ⇒ Object
- .format_part_of_datetime(datetime) ⇒ Object
- .format_part_of_device(device) ⇒ Object
- .format_part_of_event(event) ⇒ Object
- .format_part_of_time(time) ⇒ Object
- .format_part_of_user(user) ⇒ Object
- .format_string_json_value(value) ⇒ Object
- .format_string_value(value) ⇒ Object
- .replace_keywords(str, keywords) ⇒ Object
Class Method Details
.add_namespace(name, keywords) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/notice_formatter.rb', line 10 def self.add_namespace(name, keywords) return keywords.inject({}) { |memo, (key, value)| memo[name + ":" + key] = value memo } end |
.format_event(event, time = Time.now) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/notice_formatter.rb', line 98 def self.format_event(event, time = Time.now) result = {} result.merge!(self.add_namespace("now", self.format_part_of_datetime(time))) result.merge!(self.format_part_of_event(event)) result.merge!(self.format_part_of_device(event.try(:device))) result.merge!(self.format_part_of_user(event.try(:device).try(:user))) return result end |
.format_integer_json_value(value) ⇒ Object
21 22 23 |
# File 'lib/notice_formatter.rb', line 21 def self.format_integer_json_value(value) return value.to_json end |
.format_integer_value(value) ⇒ Object
17 18 19 |
# File 'lib/notice_formatter.rb', line 17 def self.format_integer_value(value) return (value.nil? ? "-" : value.to_s) end |
.format_part_of_date(date) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/notice_formatter.rb', line 33 def self.format_part_of_date(date) return { "date" => date.try(:strftime, "%Y-%m-%d") || "-", "date:json" => date.try(:strftime, '"%Y-%m-%d"') || "null", "date:ja" => date.try(:strftime, "%Y年%m月%d日") || "-", "yyyy" => date.try(:strftime, "%Y") || "-", "mm" => date.try(:strftime, "%m") || "-", "dd" => date.try(:strftime, "%d") || "-", } end |
.format_part_of_datetime(datetime) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/notice_formatter.rb', line 55 def self.format_part_of_datetime(datetime) result = { "datetime" => datetime.try(:strftime, "%Y-%m-%d %H:%M:%S") || "-", "datetime:json" => datetime.try(:strftime, '"%Y-%m-%d %H:%M:%S"') || "null", "datetime:ja" => datetime.try(:strftime, "%Y年%m月%d日 %H時%M分%S秒") || "-", } result.merge!(self.format_part_of_date(datetime)) result.merge!(self.format_part_of_time(datetime)) return result end |
.format_part_of_device(device) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/notice_formatter.rb', line 75 def self.format_part_of_device(device) return { "device:token" => self.format_string_value(device.try(:device_token)), "device:token:json" => self.format_string_json_value(device.try(:device_token)), "device:name" => self.format_string_value(device.try(:name)), "device:name:json" => self.format_string_json_value(device.try(:name)), } end |
.format_part_of_event(event) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/notice_formatter.rb', line 84 def self.format_part_of_event(event) result = { "event:trigger-operator" => self.format_string_value(event.try(:trigger_operator_symbol)), "event:trigger-operator:json" => self.format_string_json_value(event.try(:trigger_operator_symbol)), "event:trigger-level" => self.format_integer_value(event.try(:trigger_level)), "event:trigger-level:json" => self.format_integer_json_value(event.try(:trigger_level)), "event:observed-level" => self.format_integer_value(event.try(:observed_level)), "event:observed-level:json" => self.format_integer_json_value(event.try(:observed_level)), } result.merge!(self.add_namespace("event:created-at", self.format_part_of_datetime(event.try(:created_at)))) result.merge!(self.add_namespace("event:observed-at", self.format_part_of_datetime(event.try(:observed_at)))) return result end |
.format_part_of_time(time) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/notice_formatter.rb', line 44 def self.format_part_of_time(time) return { "time" => time.try(:strftime, "%H:%M:%S") || "-", "time:json" => time.try(:strftime, '"%H:%M:%S"') || "null", "time:ja" => time.try(:strftime, "%H時%M分%S秒") || "-", "hh" => time.try(:strftime, "%H") || "-", "nn" => time.try(:strftime, "%M") || "-", "ss" => time.try(:strftime, "%S") || "-", } end |
.format_part_of_user(user) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/notice_formatter.rb', line 66 def self.format_part_of_user(user) return { "user:token" => self.format_string_value(user.try(:user_token)), "user:token:json" => self.format_string_json_value(user.try(:user_token)), "user:nickname" => self.format_string_value(user.try(:nickname)), "user:nickname:json" => self.format_string_json_value(user.try(:nickname)), } end |
.format_string_json_value(value) ⇒ Object
29 30 31 |
# File 'lib/notice_formatter.rb', line 29 def self.format_string_json_value(value) return (value.blank? ? nil.to_json : value.to_json) end |
.format_string_value(value) ⇒ Object
25 26 27 |
# File 'lib/notice_formatter.rb', line 25 def self.format_string_value(value) return (value.blank? ? "-" : value.to_s) end |
.replace_keywords(str, keywords) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/notice_formatter.rb', line 3 def self.replace_keywords(str, keywords) return keywords.inject(str.dup) { |memo, (key, value)| memo.gsub!(/\{#{Regexp.escape(key)}\}/, value) memo } end |