Module: Astrotrain::Transports::Resque

Defined in:
lib/astrotrain/transports/resque.rb

Class Method Summary collapse

Class Method Details

.create_hash(message, recipient) ⇒ Object

Creates a param hash for RestClient.

message - Astrotrain::Message instance recipient - String email of the main recipient.

Returns a Hash for Resque.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/astrotrain/transports/resque.rb', line 44

def self.create_hash(message, recipient)
  h = {:subject => message.subject, :to => [], :from => [], :cc => [],
       :body => message.body, :emails => message.recipients.join(", "), :html => message.html,
       :headers => message.headers}
  [:to, :from, :cc].each do |key|
    message.send(key).each_with_index do |addr, i|
      h[key] << {:name => addr.display_name, :address => addr.address}
    end
  end
  h
end

.deliver(message, url, options = {}) ⇒ Object

Public: Sends the message to the given address.

message - Astrotrain::Message instance url - String address of the Resque in this form:

"QUEUE/KLASS"

options - Optional Hash:

:recipient - String email of the main recipient
:extra     - Hash to be merged with the payload

Returns a queued Resque::Job instance.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/astrotrain/transports/resque.rb', line 19

def self.deliver(message, url, options = {})
  recipient = options[:recipient] || message.recipients.first
  uri       = url.is_a?(Addressable::URI) ?
    url :
    Addressable::URI.parse(url)
  path = uri.path.dup
  path.sub! /^\//, ''
  queue, *job = path.split("/")
  if qu = uri.host
    job.unshift(queue)
    queue = qu
  end
  payload = create_hash(message, recipient)
  if extra = options[:extra]
    payload.update(extra)
  end
  ::Resque::Job.create(queue, job.join("/"), payload)
end

.process(url, message, recipient = nil, extra = nil) ⇒ Object

kept for backwards compatibility



57
58
59
# File 'lib/astrotrain/transports/resque.rb', line 57

def self.process(url, message, recipient = nil, extra = nil)
  deliver(message, url, :recipient => recipient, :extra => extra)
end