class Mandrill::API

Attributes

apikey[RW]
debug[RW]
host[RW]
path[RW]
session[RW]

Public Class Methods

new(apikey=nil, debug=false) click to toggle source
# File lib/mandrill.rb, line 13
def initialize(apikey=nil, debug=false)
    @host = 'https://mandrillapp.com'
    @path = '/api/1.0/'

    @session = Excon.new @host
    @debug = debug

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

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

Public Instance Methods

call(url, params={}) click to toggle source
# File lib/mandrill.rb, line 32
def call(url, params={})
    params[:key] = @apikey
    params = JSON.generate(params)
    r = @session.post(:path => "#{@path}#{url}.json", :headers => {'Content-Type' => 'application/json'}, :body => params)
    
    cast_error(r.body) if r.status != 200
    return JSON.parse(r.body)
end
cast_error(body) click to toggle source
# File lib/mandrill.rb, line 52
def cast_error(body)

    error_map = {
        'ValidationError' => ValidationError,
        'Invalid_Key' => InvalidKeyError,
        'PaymentRequired' => PaymentRequiredError,
        'Unknown_Subaccount' => UnknownSubaccountError,
        'Unknown_Template' => UnknownTemplateError,
        'ServiceUnavailable' => ServiceUnavailableError,
        'Unknown_Message' => UnknownMessageError,
        'Invalid_Tag_Name' => InvalidTagNameError,
        'Invalid_Reject' => InvalidRejectError,
        'Unknown_Sender' => UnknownSenderError,
        'Unknown_Url' => UnknownUrlError,
        'Unknown_TrackingDomain' => UnknownTrackingDomainError,
        'Invalid_Template' => InvalidTemplateError,
        'Unknown_Webhook' => UnknownWebhookError,
        'Unknown_InboundDomain' => UnknownInboundDomainError,
        'Unknown_InboundRoute' => UnknownInboundRouteError,
        'Unknown_Export' => UnknownExportError,
        'IP_ProvisionLimit' => IPProvisionLimitError,
        'Unknown_Pool' => UnknownPoolError,
        'NoSendingHistory' => NoSendingHistoryError,
        'PoorReputation' => PoorReputationError,
        'Unknown_IP' => UnknownIPError,
        'Invalid_EmptyDefaultPool' => InvalidEmptyDefaultPoolError,
        'Invalid_DeleteDefaultPool' => InvalidDeleteDefaultPoolError,
        'Invalid_DeleteNonEmptyPool' => InvalidDeleteNonEmptyPoolError,
        'Invalid_CustomDNS' => InvalidCustomDNSError,
        'Invalid_CustomDNSPending' => InvalidCustomDNSPendingError,
        'Metadata_FieldLimit' => MetadataFieldLimitError,
        'Unknown_MetadataField' => UnknownMetadataFieldError
    }

    begin
        error_info = JSON.parse(body)
        if error_info['status'] != 'error' or not error_info['name']
            raise Error, "We received an unexpected error: #{body}"
        end
        if error_map[error_info['name']]
            raise error_map[error_info['name']], error_info['message']
        else
            raise Error, error_info['message']
        end
    rescue JSON::ParserError
        raise Error, "We received an unexpected error: #{body}"
    end
end
exports() click to toggle source
# File lib/mandrill.rb, line 104
def exports()
    Exports.new self
end
inbound() click to toggle source
# File lib/mandrill.rb, line 113
def inbound()
    Inbound.new self
end
internal() click to toggle source
# File lib/mandrill.rb, line 128
def internal()
    Internal.new self
end
ips() click to toggle source
# File lib/mandrill.rb, line 125
def ips()
    Ips.new self
end
messages() click to toggle source
# File lib/mandrill.rb, line 119
def messages()
    Messages.new self
end
metadata() click to toggle source
# File lib/mandrill.rb, line 143
def metadata()
    Metadata.new self
end
read_configs() click to toggle source
# File lib/mandrill.rb, line 41
def read_configs()
    [File.expand_path('~/.mandrill.key'), '/etc/mandrill.key'].delete_if{ |p| not File.exist? p}.each do |path|
        f = File.new path
        apikey = f.read.strip
        f.close
        return apikey if apikey != ''
    end

    return nil
end
rejects() click to toggle source
# File lib/mandrill.rb, line 110
def rejects()
    Rejects.new self
end
senders() click to toggle source
# File lib/mandrill.rb, line 140
def senders()
    Senders.new self
end
subaccounts() click to toggle source
# File lib/mandrill.rb, line 131
def subaccounts()
    Subaccounts.new self
end
tags() click to toggle source
# File lib/mandrill.rb, line 116
def tags()
    Tags.new self
end
templates() click to toggle source
# File lib/mandrill.rb, line 101
def templates()
    Templates.new self
end
urls() click to toggle source
# File lib/mandrill.rb, line 134
def urls()
    Urls.new self
end
users() click to toggle source
# File lib/mandrill.rb, line 107
def users()
    Users.new self
end
webhooks() click to toggle source
# File lib/mandrill.rb, line 137
def webhooks()
    Webhooks.new self
end
whitelists() click to toggle source
# File lib/mandrill.rb, line 122
def whitelists()
    Whitelists.new self
end