Class: Apostle::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/apostle/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueue

Returns a new instance of Queue.



9
10
11
# File 'lib/apostle/queue.rb', line 9

def initialize
  @emails = []
end

Instance Attribute Details

#emailsObject

Returns the value of attribute emails.



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

def emails
  @emails
end

#resultsObject

Returns the value of attribute results.



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

def results
  @results
end

Instance Method Details

#<<(email) ⇒ Object



13
14
15
# File 'lib/apostle/queue.rb', line 13

def <<(email)
  add(email)
end

#add(email) ⇒ Object



17
18
19
# File 'lib/apostle/queue.rb', line 17

def add(email)
  @emails << email
end

#clearObject



25
26
27
28
# File 'lib/apostle/queue.rb', line 25

def clear
  emails = []
  results = nil
end

#deliverObject



34
35
36
37
38
# File 'lib/apostle/queue.rb', line 34

def deliver
  deliver!
rescue DeliveryError
  false
end

#deliver!Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/apostle/queue.rb', line 40

def deliver!
  return true unless Apostle.deliver

  # Validate the minimum requirement of a recipient and template
  unless Apostle.domain_key
    raise DeliveryError,
      "No Apostle Domain Key has been defined. Preferably this should be in your environment, as ENV['APOSTLE_DOMAIN_KEY']. If you need to configure this manually, you can call Apostle.configure.

  Apostle.configure do |config|
    config.domain_key = 'Your domain key'
  end"
  end

  raise DeliveryError, "Mail queue is empty" if emails.size == 0

  payload, @results = process_emails

  if @results[:invalid].size > 0
    raise DeliveryError,
      "Invalid emails: #{@results[:invalid].size}"
  end

  # Deliver the payload
  response = deliver_payload(payload)

  true
end

#flushObject



30
31
32
# File 'lib/apostle/queue.rb', line 30

def flush
  deliver && clear
end

#sizeObject



21
22
23
# File 'lib/apostle/queue.rb', line 21

def size
  emails.size
end