lib/mailstro/delivery.rb in mailstro-0.0.3 vs lib/mailstro/delivery.rb in mailstro-0.0.4

- old
+ new

@@ -1,57 +1,21 @@ module Mailstro - class Delivery - require 'net/http' - require 'openssl' - - attr_reader :template, :payload - - def self.deliver(template, recipient, payload) - new(template, recipient, payload).deliver + class Delivery < Resource + def self.deliver(template_name, contact_email, payload) + new(template_name, contact_email, payload).deliver end - def initialize(template, recipient, payload) - @template = template - @recipient = recipient - @payload = payload - end + attr_reader :contact_email, :template_name - 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.new(http.request(request)).json_body + def initialize(template_name, contact_email, payload) + @template_name = template_name + @contact_email = contact_email + @payload = payload end - def recipient - if @recipient.is_a?(String) - { :email => @recipient } - else - @recipient - end - end - - private - - def post_data - { - 'template' => template, - 'recipient' => recipient, - 'payload' => payload, - 'api_key' => Mailstro.configuration.api_key - } - end - - def endpoint_uri - @endpoint_uri ||= URI.parse(endpoint) - end - - def endpoint - (Mailstro.configuration.use_ssl ? "https://" : "http://") + - "#{Mailstro.configuration.endpoint}/v#{Mailstro.configuration.api_version}/postman" + def deliver + post("deliveries", :template_name => @template_name, + :contact_email => @contact_email, + :payload => @payload) end end end