Sha256: 417ab813807b1247c3da05bf8b45c4f1ea900a1acd644659b51b012fb9ee0c36

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'mandrill'

# Patch mandrill API to play ball with EventMachine
module Mandrill
  class API
    def initialize(apikey=nil, debug=false)
      @host = 'https://mandrillapp.com'
      @path = '/api/1.0/'

      @debug = debug

      unless apikey
        if ENV['MANDRILL_APIKEY']
          apikey = ENV['MANDRILL_APIKEY']
        else
          apikey = read_configs
        end
      end

      raise Error, 'You must provide a Mandrill API key' unless apikey
      @apikey = apikey
    end

    def call(url, params={})
      defer = EventMachine::DefaultDeferrable.new
      params[:key] = @apikey
      params = JSON.generate(params)
      http = Mandrill::API.request.get(path: "#{@path}#{url}.json", headers: {'Content-Type' => 'application/json'}, body: params, keepalive: true)
      http.callback do |r|
        cast_error(r.response) if r.response_header.status != 200
        defer.succeed(JSON.parse(r.response))
      end

      http.errback do |r|
        raise Mandrill::ServiceUnavailableError.new
      end

      EventMachine::Synchrony.sync defer
    end

    class << self
      def request
        EventMachine::HttpRequest.new('https://mandrillapp.com/api/1.0/')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-gen-0.0.6 template/config/initializers/em-patches/mandrill.rb