Sha256: d45a3ad99c1c4ef342b372d2680eb786858abb0f151953a83e3cbca6398c1362

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

# 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

5 entries across 5 versions & 1 rubygems

Version Path
grape-gen-0.0.5 template/config/initializers/em-patches/mandrill.rb
grape-gen-0.0.4 template/config/initializers/em-patches/mandrill.rb
grape-gen-0.0.3 template/config/initializers/em-patches/mandrill.rb
grape-gen-0.0.2 template/config/initializers/em-patches/mandrill.rb
grape-gen-0.0.1 template/config/initializers/em-patches/mandrill.rb