Module: Astrotrain::Transports::HttpPost

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.connectionObject



12
13
14
# File 'lib/astrotrain/transports/http_post.rb', line 12

def self.connection
  @connection ||= Faraday.default_connection
end

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 RestClient.post



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/astrotrain/transports/http_post.rb', line 43

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, :attachments => {}}
  [:to, :from, :cc].each do |key|
    message.send(key).each_with_index do |addr, i|
      h[key][i] = {:name => addr.display_name, :address => addr.address}
    end
  end
  message.attachments.each_with_index do |a, i|
    h[:attachments][i] = a
  end
  [:attachments, :to, :from, :cc].each do |key|
    h.delete(key) if h[key].empty?
  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 recipient service options - Optional Hash:

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

Returns a RestClient::Response object for responses between 200..206 Raises RestClient::Exception for any code not between 200..206 or 301..302



27
28
29
30
31
32
33
34
# File 'lib/astrotrain/transports/http_post.rb', line 27

def self.deliver(message, url, options = {})
  recipient = options[:recipient] || message.recipients.first
  payload   = create_hash(message, recipient)
  if extra = options[:extra]
    payload.update(extra)
  end
  connection.post(url, payload)
end

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

kept for backwards compatibility



62
63
64
# File 'lib/astrotrain/transports/http_post.rb', line 62

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