Class: Malone

Inherits:
Object
  • Object
show all
Defined in:
lib/malone.rb,
lib/malone/test.rb

Defined Under Namespace

Classes: Configuration, Envelope

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Malone

Returns a new instance of Malone.



27
28
29
# File 'lib/malone.rb', line 27

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/malone.rb', line 7

def config
  @config
end

Class Method Details

.connect(options = {}) ⇒ Object



9
10
11
12
13
# File 'lib/malone.rb', line 9

def self.connect(options = {})
  @config = Configuration.new(options)

  current
end

.currentObject



15
16
17
18
19
20
21
# File 'lib/malone.rb', line 15

def self.current
  unless defined?(@config)
    raise RuntimeError, "Missing configuration: Try doing `Malone.connect`."
  end

  return new(@config)
end

.deliver(dict) ⇒ Object



23
24
25
# File 'lib/malone.rb', line 23

def self.deliver(dict)
  current.deliver(dict)
end

.deliveriesObject



4
5
6
# File 'lib/malone/test.rb', line 4

def self.deliveries
  @deliveries ||= []
end

Instance Method Details

#deliver(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/malone.rb', line 31

def deliver(dict)
  mail = envelope(dict)

  smtp = Net::SMTP.new(config.host, config.port)
  smtp.enable_starttls_auto if config.tls

  begin
    smtp.start(config.domain, config.user, config.password, config.auth)
    smtp.send_message(mail.to_s, mail.from.first, *recipients(mail))
  ensure
    smtp.finish if smtp.started?
  end
end

#envelope(dict) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/malone.rb', line 53

def envelope(dict)
  envelope = Envelope.new
  envelope.from    = dict[:from]
  envelope.to      = dict[:to]
  envelope.replyto = dict[:replyto]
  envelope.cc      = dict[:cc] if dict[:cc]
  envelope.bcc     = dict[:bcc] if dict[:bcc]
  envelope.text    = dict[:text]
  envelope.rawhtml = dict[:html] if dict[:html]
  envelope.subject = dict[:subject]

  envelope.attach(dict[:attach]) if dict[:attach]
  
  envelope.add_attachment_as(*dict[:attach_as]) if dict[:attach_as]

  return envelope
end

#recipients(mail) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/malone.rb', line 45

def recipients(mail)
  [].tap do |ret|
    ret.push(*mail.to)
    ret.push(*mail.cc)
    ret.push(*mail.bcc)
  end
end