Class: LetterOpenerWeb::BaseLetter

Inherits:
Object
  • Object
show all
Defined in:
app/models/letter_opener_web/base_letter.rb

Direct Known Subclasses

AwsLetter, Letter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ BaseLetter

Returns a new instance of BaseLetter.



7
8
9
10
# File 'app/models/letter_opener_web/base_letter.rb', line 7

def initialize(params)
  @id      = params.fetch(:id)
  @sent_at = params[:sent_at]
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'app/models/letter_opener_web/base_letter.rb', line 5

def id
  @id
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



5
6
7
# File 'app/models/letter_opener_web/base_letter.rb', line 5

def sent_at
  @sent_at
end

Class Method Details

.find(id) ⇒ Object



17
18
19
# File 'app/models/letter_opener_web/base_letter.rb', line 17

def self.find(id)
  new(id:)
end

.letters_locationObject



12
13
14
# File 'app/models/letter_opener_web/base_letter.rb', line 12

def self.letters_location
  LetterOpenerWeb.config.letters_location
end

Instance Method Details

#default_styleObject



45
46
47
# File 'app/models/letter_opener_web/base_letter.rb', line 45

def default_style
  style_exists?('rich') ? 'rich' : 'plain'
end

#headersObject



21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/letter_opener_web/base_letter.rb', line 21

def headers
  html = read_file(:rich) if style_exists?('rich')
  html ||= read_file(:plain)

  # NOTE: This is ugly, we should look into using nokogiri and making that a
  # dependency of this gem
  match_data = html.match(%r{<body>\s*<div[^>]+id="container">\s*<div[^>]+id="message_headers">\s*(<dl>.+</dl>)}m)
  return remove_attachments_link(match_data[1]).html_safe if match_data && match_data[1].present?

  'UNABLE TO PARSE HEADERS'
end

#plain_textObject



33
34
35
# File 'app/models/letter_opener_web/base_letter.rb', line 33

def plain_text
  @plain_text ||= adjust_link_targets(read_file(:plain))
end

#rich_textObject



37
38
39
# File 'app/models/letter_opener_web/base_letter.rb', line 37

def rich_text
  @rich_text ||= adjust_link_targets(read_file(:rich))
end

#to_paramObject



41
42
43
# File 'app/models/letter_opener_web/base_letter.rb', line 41

def to_param
  id
end