Class: Mandrill::API

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey = nil, debug = false) ⇒ API

Returns a new instance of API.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



11
12
13
# File 'lib/mandrill.rb', line 11

def apikey
  @apikey
end

#debugObject

Returns the value of attribute debug.



11
12
13
# File 'lib/mandrill.rb', line 11

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/mandrill.rb', line 11

def host
  @host
end

#pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/mandrill.rb', line 11

def path
  @path
end

#sessionObject

Returns the value of attribute session.



11
12
13
# File 'lib/mandrill.rb', line 11

def session
  @session
end

Instance Method Details

#call(url, params = {}) ⇒ Object



32
33
34
35
36
37
38
39
# 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) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mandrill.rb', line 52

def cast_error(body)

    error_map = {
        'ValidationError' => ValidationError,
        'Invalid_Key' => InvalidKeyError,
        'Unknown_Template' => UnknownTemplateError,
        'Invalid_Tag_Name' => InvalidTagNameError,
        'Invalid_Reject' => InvalidRejectError,
        'Unknown_Sender' => UnknownSenderError,
        'Unknown_Url' => UnknownUrlError,
        'Invalid_Template' => InvalidTemplateError,
        'Unknown_Webhook' => UnknownWebhookError,
        'Unknown_InboundDomain' => UnknownInboundDomainError
    }

    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

#inboundObject



91
92
93
# File 'lib/mandrill.rb', line 91

def inbound()
    Inbound.new self
end

#internalObject



100
101
102
# File 'lib/mandrill.rb', line 100

def internal()
    Internal.new self
end

#messagesObject



97
98
99
# File 'lib/mandrill.rb', line 97

def messages()
    Messages.new self
end

#read_configsObject



41
42
43
44
45
46
47
48
49
50
# 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

#rejectsObject



88
89
90
# File 'lib/mandrill.rb', line 88

def rejects()
    Rejects.new self
end

#sendersObject



109
110
111
# File 'lib/mandrill.rb', line 109

def senders()
    Senders.new self
end

#tagsObject



94
95
96
# File 'lib/mandrill.rb', line 94

def tags()
    Tags.new self
end

#templatesObject



82
83
84
# File 'lib/mandrill.rb', line 82

def templates()
    Templates.new self
end

#urlsObject



103
104
105
# File 'lib/mandrill.rb', line 103

def urls()
    Urls.new self
end

#usersObject



85
86
87
# File 'lib/mandrill.rb', line 85

def users()
    Users.new self
end

#webhooksObject



106
107
108
# File 'lib/mandrill.rb', line 106

def webhooks()
    Webhooks.new self
end