Class: FederalOffense::Message

Inherits:
OpenStruct
  • Object
show all
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

Class Method Details

.allObject



12
13
14
15
16
# File 'app/models/federal_offense/message.rb', line 12

def all
  Dir[ROOT.join("*.json")].map { |message|
    read(message)
  }.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(message)
  # Create a unique ID for the message
  timestamp = Time.zone.now.to_i
  components = [timestamp] + Array(message.from) + Array(message.to) + [message.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 message.parts.any?
    [message.html_part&.body, message.text_part&.body]
  else
    is_html = message.content_type.match?(/html/i)
    [
      is_html ? message.body : nil,
      is_html ? nil : message.body,
    ]
  end

  # Set up some default attributes
  attributes = {
    bcc: message.bcc,
    cc: message.cc,
    date: timestamp,
    from: message.from,
    html: html,
    id: id,
    path: path,
    subject: message.subject,
    text: text,
    to: message.to,
  }

  new(attributes).tap do |new_message|
    new_message.write
    new_message.save_attachments(message)
  end
end

.destroy_allObject



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

#dateObject



77
78
79
# File 'app/models/federal_offense/message.rb', line 77

def date
  @date ||= Time.zone.at(super)
end

#destroyObject



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

Returns:

  • (Boolean)


86
87
88
# File 'app/models/federal_offense/message.rb', line 86

def html?
  html.present?
end

#html_bodyObject



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

Returns:

  • (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 save_attachments(message)
  # 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

#subjectObject



108
109
110
# File 'app/models/federal_offense/message.rb', line 108

def subject
  super.presence || EMPTY_SUBJECT
end

#text?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/federal_offense/message.rb', line 112

def text?
  text.present?
end

#text_bodyObject



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

Returns:

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

#writeObject



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