module Mailstro class Delivery require 'net/http' require 'openssl' attr_reader :template, :payload def self.deliver(template, recipient, payload) new(template, recipient, payload).deliver end def initialize(template, recipient, payload) @template = template @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.new(http.request(request)).json_body 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" end end end