lib/mailstro/delivery.rb in mailstro-0.0.2 vs lib/mailstro/delivery.rb in mailstro-0.0.3
- old
+ new
@@ -1,48 +1,49 @@
module Mailstro
class Delivery
require 'net/http'
require 'openssl'
- require 'json'
- attr_reader :template, :recipient, :payload
+ attr_reader :template, :payload
- def self.deliver(*args)
- new(*args).deliver
+ def self.deliver(template, recipient, payload)
+ new(template, recipient, payload).deliver
end
- def initialize(template, options)
+ def initialize(template, recipient, payload)
@template = template
- @recipient = options[:recipient]
- @payload = options[:payload]
+ @recipient = recipient
+ @payload = payload
end
def deliver
http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
http.use_ssl = endpoint_uri.scheme == "https"
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(endpoint_uri.request_uri, { 'Content-Type' =>'application/json' })
request.body = JSON.generate(post_data)
- response = http.request(request)
+ Response.new(http.request(request)).json_body
+ end
- # response.status
- # response["header-here"] # All headers are lowercase
-
- JSON.parse(response.body)
+ def recipient
+ if @recipient.is_a?(String)
+ { :email => @recipient }
+ else
+ @recipient
+ end
end
private
def post_data
- { 'template' => template,
+ {
+ 'template' => template,
'recipient' => recipient,
- 'api_key' => Mailstro.configuration.api_key }.tap do |payload|
- if self.payload.kind_of?(Hash)
- payload['payload'] = self.payload
- end
- end
+ 'payload' => payload,
+ 'api_key' => Mailstro.configuration.api_key
+ }
end
def endpoint_uri
@endpoint_uri ||= URI.parse(endpoint)
end