Class: FederalOffense::Message
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- FederalOffense::Message
- Defined in:
- app/models/federal_offense/message.rb
Constant Summary collapse
- ROOT =
Rails.root.join("tmp", "federal_offense").freeze
- EMPTY_TEXT_BODY =
"No message content"
- EMPTY_HTML_BODY =
"<html><body><em>#{EMPTY_TEXT_BODY}</em></body></html>"
- EMPTY_SUBJECT =
"(no subject)"
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(*args) ⇒ Object
- #date ⇒ Object
- #destroy ⇒ Object
- #html? ⇒ Boolean
- #html_body ⇒ Object
- #read? ⇒ Boolean
- #save_attachments(message) ⇒ Object
- #subject ⇒ Object
- #text? ⇒ Boolean
- #text_body ⇒ Object
- #unread? ⇒ Boolean
- #update(attributes) ⇒ Object
- #write ⇒ Object
- #write_attributes(attributes) ⇒ Object
Class Method Details
.all ⇒ Object
12 13 14 15 16 |
# File 'app/models/federal_offense/message.rb', line 12 def all Dir[ROOT.join("*.json")].map { || read() }.sort_by(&:date) end |
.create(message) ⇒ Object
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 |
# File 'app/models/federal_offense/message.rb', line 18 def create() # Create a unique ID for the message = Time.zone.now.to_i components = [] + Array(.from) + Array(.to) + [.subject, SecureRandom.hex(3)] id = components.join(" ") id = Digest::SHA1.hexdigest(id) # Prepare the filesystem for the JSON record FileUtils.mkdir_p(ROOT) path = ROOT.join("#{id}.json") html, text = if .parts.any? [.html_part&.body, .text_part&.body] else is_html = .content_type.match?(/html/i) [ is_html ? .body : nil, is_html ? nil : .body, ] end # Set up some default attributes attributes = { bcc: .bcc, cc: .cc, date: , from: .from, html: html, id: id, path: path, subject: .subject, text: text, to: .to, } new(attributes).tap do || .write .() end end |
.destroy_all ⇒ Object
59 60 61 |
# File 'app/models/federal_offense/message.rb', line 59 def destroy_all FileUtils.rm_rf(ROOT) end |
Instance Method Details
#as_json(*args) ⇒ Object
73 74 75 |
# File 'app/models/federal_offense/message.rb', line 73 def as_json(*args) @table.as_json(*args) end |
#date ⇒ Object
77 78 79 |
# File 'app/models/federal_offense/message.rb', line 77 def date @date ||= Time.zone.at(super) end |
#destroy ⇒ Object
81 82 83 84 |
# File 'app/models/federal_offense/message.rb', line 81 def destroy FileUtils.rm_rf(ROOT.join(id)) File.unlink(path) end |
#html? ⇒ Boolean
86 87 88 |
# File 'app/models/federal_offense/message.rb', line 86 def html? html.present? end |
#html_body ⇒ Object
90 91 92 |
# File 'app/models/federal_offense/message.rb', line 90 def html_body (html || {}).fetch("raw_source", EMPTY_HTML_BODY).html_safe # rubocop:disable Rails/OutputSafety end |
#read? ⇒ Boolean
94 95 96 |
# File 'app/models/federal_offense/message.rb', line 94 def read? read_at.present? end |
#save_attachments(message) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'app/models/federal_offense/message.rb', line 98 def () # return unless message.attachments.any? # FileUtils.mkdir_p(ROOT.join(id)) # message.attachments.each do |attachment| # # TODO: Save the attachments if need be, but this is complex and we don't need to sweat it # # for now # end end |
#subject ⇒ Object
108 109 110 |
# File 'app/models/federal_offense/message.rb', line 108 def subject super.presence || EMPTY_SUBJECT end |
#text? ⇒ Boolean
112 113 114 |
# File 'app/models/federal_offense/message.rb', line 112 def text? text.present? end |
#text_body ⇒ Object
116 117 118 |
# File 'app/models/federal_offense/message.rb', line 116 def text_body (text || {}).fetch("raw_source", EMPTY_TEXT_BODY) end |
#unread? ⇒ Boolean
120 121 122 |
# File 'app/models/federal_offense/message.rb', line 120 def unread? !read? end |
#update(attributes) ⇒ Object
124 125 126 127 |
# File 'app/models/federal_offense/message.rb', line 124 def update(attributes) write_attributes(attributes) write end |
#write ⇒ Object
129 130 131 132 133 |
# File 'app/models/federal_offense/message.rb', line 129 def write File.open(path, "w+") do |file| file.puts to_json end end |
#write_attributes(attributes) ⇒ Object
135 136 137 138 139 |
# File 'app/models/federal_offense/message.rb', line 135 def write_attributes(attributes) attributes.each do |key, value| send("#{key}=", value) end end |