Class: Ruml::Broadcaster

Inherits:
Object
  • Object
show all
Defined in:
lib/ruml/broadcaster.rb

Instance Method Summary collapse

Constructor Details

#initialize(ml, message) ⇒ Broadcaster

Returns a new instance of Broadcaster.



5
6
7
8
# File 'lib/ruml/broadcaster.rb', line 5

def initialize(ml, message)
  @ml = ml
  @message = Mail.new(message)
end

Instance Method Details

#add_body!(mail) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ruml/broadcaster.rb', line 30

def add_body!(mail)
  if @message.multipart?
    @message.parts.each do |part|
      mail.add_part part
    end
  else
    mail.body @message.body.raw_source
  end
end

#add_headers!(mail) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/ruml/broadcaster.rb', line 20

def add_headers!(mail)
  mail['X-BeenThere'] = @ml.to
  mail['List-Id']     = "<#{@ml.id}>"
  mail['List-Post']   = "<mailto:#{@ml.to}>"
  mail['Precedence']  = 'list'
  mail['Sender']      = @ml.bounce_to
  mail['Errors-To']   = @ml.bounce_to
  mail['User-Agent']  = @message['User-Agent'].value if @message['User-Agent']
end

#been_there?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ruml/broadcaster.rb', line 16

def been_there?
  @message['X-BeenThere'].to_s == @ml.to
end

#broadcast!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruml/broadcaster.rb', line 64

def broadcast!
  return unless broadcastable?
  mail = Mail.new
  mail.subject  subject
  mail.from     from
  mail.to       to
  mail.bcc      recipients

  add_headers! mail
  add_body! mail

  mail.deliver
end

#broadcastable?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ruml/broadcaster.rb', line 60

def broadcastable?
  !been_there? && valid_member?
end

#fromObject



40
41
42
# File 'lib/ruml/broadcaster.rb', line 40

def from
  @message.from.first
end

#recipientsObject



48
49
50
# File 'lib/ruml/broadcaster.rb', line 48

def recipients
  @ml.members
end

#subjectObject



52
53
54
55
56
57
58
# File 'lib/ruml/broadcaster.rb', line 52

def subject
  if @ml.name && !@message.subject.include?(@ml.name)
    "#{@ml.name} #{@message.subject}"
  else
    @message.subject
  end
end

#toObject



44
45
46
# File 'lib/ruml/broadcaster.rb', line 44

def to
  @ml.to
end

#valid_member?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
# File 'lib/ruml/broadcaster.rb', line 10

def valid_member?
  expected = @message.from.size
  actual   = (@message.from.map(&:downcase) & @ml.members.map(&:downcase)).size
  expected == actual
end