Class: LetterOpenerWeb::AwsLetter

Inherits:
BaseLetter show all
Defined in:
app/models/letter_opener_web/aws_letter.rb

Instance Attribute Summary

Attributes inherited from BaseLetter

#id, #sent_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseLetter

#default_style, find, #headers, #initialize, letters_location, #plain_text, #rich_text, #to_param

Constructor Details

This class inherits a constructor from LetterOpenerWeb::BaseLetter

Class Method Details

.aws_list_letters(path = []) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'app/models/letter_opener_web/aws_letter.rb', line 5

def self.aws_list_letters(path = [], **)
  LetterOpenerWeb
    .aws_client
    .list_objects_v2(
      bucket: LetterOpenerWeb.config.aws_bucket,
      prefix: File.join([letters_location, *path].compact),
      **
    )
end

.destroy_allObject



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/letter_opener_web/aws_letter.rb', line 24

def self.destroy_all
  letters = aws_list_letters.contents.map(&:key)

  LetterOpenerWeb.aws_client.delete_objects(
    bucket: LetterOpenerWeb.config.aws_bucket,
    delete: {
      objects: letters.map { |key| { key: } },
      quiet: false
    }
  )
end

.searchObject



16
17
18
19
20
21
22
# File 'app/models/letter_opener_web/aws_letter.rb', line 16

def self.search
  letters = aws_list_letters(delimiter: '.html').common_prefixes.map do |prefix|
    new(id: File.basename(File.dirname(prefix.prefix)))
  end

  letters.reverse
end

Instance Method Details

#attachmentsObject



36
37
38
39
40
# File 'app/models/letter_opener_web/aws_letter.rb', line 36

def attachments
  @attachments ||= aws_list_letters([id, 'attachments/']).contents.each_with_object({}) do |file, hash|
    hash[File.basename(file.key)] = attachment_url(file.key)
  end
end

#deleteObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/letter_opener_web/aws_letter.rb', line 42

def delete
  return unless valid?

  letters = aws_list_letters([id]).contents.map(&:key)

  LetterOpenerWeb.aws_client.delete_objects(
    bucket: LetterOpenerWeb.config.aws_bucket,
    delete: {
      objects: letters.map { |key| { key: } },
      quiet: false
    }
  )
end

#valid?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/letter_opener_web/aws_letter.rb', line 56

def valid?
  aws_list_letters([id]).contents.any?
end