Class: Mailscope::Mailbox

Inherits:
Struct
  • Object
show all
Defined in:
lib/mailscope/mailbox.rb

Overview

A recipient address, with the messages that landed in it. This is what the left rail lists: one entry per "caixa de e-mail". rubocop:disable Lint/StructNewOverride -- count reads better than any alternative here, and this struct is never used as an enumerable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#addressObject

Returns the value of attribute address

Returns:

  • (Object)

    the current value of address



8
9
10
# File 'lib/mailscope/mailbox.rb', line 8

def address
  @address
end

#countObject

Returns the value of attribute count

Returns:

  • (Object)

    the current value of count



8
9
10
# File 'lib/mailscope/mailbox.rb', line 8

def count
  @count
end

#latest_atObject

Returns the value of attribute latest_at

Returns:

  • (Object)

    the current value of latest_at



8
9
10
# File 'lib/mailscope/mailbox.rb', line 8

def latest_at
  @latest_at
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



8
9
10
# File 'lib/mailscope/mailbox.rb', line 8

def name
  @name
end

Class Method Details

.absorb(buckets, addr, message) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/mailscope/mailbox.rb', line 28

def self.absorb(buckets, addr, message)
  key = addr.address.to_s.downcase
  return if key.empty?

  box = buckets[key] ||= new(address: key, name: addr.name, count: 0, latest_at: nil)
  box.name ||= addr.name
  box.count += 1
  box.latest_at = message.sent_at if box.latest_at.nil? || message.sent_at > box.latest_at
end

.index(messages) ⇒ Object



22
23
24
25
26
# File 'lib/mailscope/mailbox.rb', line 22

def self.index(messages)
  buckets = {}
  messages.each { |message| message.recipients.each { |addr| absorb(buckets, addr, message) } }
  buckets.values.sort_by { |box| [-box.latest_at.to_i, box.address] }
end

Instance Method Details

#hueObject

Stable colour per mailbox, so the same address always looks the same.



18
# File 'lib/mailscope/mailbox.rb', line 18

def hue = address.to_s.each_byte.sum % 360

#initialsObject



11
12
13
14
15
# File 'lib/mailscope/mailbox.rb', line 11

def initials
  source = label.to_s
  letters = source.scan(/[[:alnum:]]+/).first(2).map { |w| w[0] }.join
  (letters.empty? ? source[0, 2] : letters).to_s.upcase
end

#labelObject



9
# File 'lib/mailscope/mailbox.rb', line 9

def label = name.to_s.empty? ? address : name

#to_hObject



20
# File 'lib/mailscope/mailbox.rb', line 20

def to_h = { address:, name:, count:, latest_at: latest_at&.iso8601 }