Class: Mandrill::API
- Inherits:
-
Object
- Object
- Mandrill::API
- Defined in:
- lib/mandrill.rb
Instance Attribute Summary collapse
-
#apikey ⇒ Object
Returns the value of attribute apikey.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#host ⇒ Object
Returns the value of attribute host.
-
#path ⇒ Object
Returns the value of attribute path.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
- #call(url, params = {}) ⇒ Object
- #cast_error(body) ⇒ Object
- #exports ⇒ Object
- #inbound ⇒ Object
-
#initialize(apikey = nil, debug = false) ⇒ API
constructor
A new instance of API.
- #internal ⇒ Object
- #ips ⇒ Object
- #messages ⇒ Object
- #read_configs ⇒ Object
- #rejects ⇒ Object
- #senders ⇒ Object
- #subaccounts ⇒ Object
- #tags ⇒ Object
- #templates ⇒ Object
- #urls ⇒ Object
- #users ⇒ Object
- #webhooks ⇒ Object
- #whitelists ⇒ Object
Constructor Details
#initialize(apikey = nil, debug = false) ⇒ API
Returns a new instance of API.
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
#apikey ⇒ Object
Returns the value of attribute apikey.
11 12 13 |
# File 'lib/mandrill.rb', line 11 def apikey @apikey end |
#debug ⇒ Object
Returns the value of attribute debug.
11 12 13 |
# File 'lib/mandrill.rb', line 11 def debug @debug end |
#host ⇒ Object
Returns the value of attribute host.
11 12 13 |
# File 'lib/mandrill.rb', line 11 def host @host end |
#path ⇒ Object
Returns the value of attribute path.
11 12 13 |
# File 'lib/mandrill.rb', line 11 def path @path end |
#session ⇒ Object
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 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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, 'Unknown_IP' => UnknownIPError, 'Invalid_EmptyDefaultPool' => InvalidEmptyDefaultPoolError, 'Invalid_DeleteDefaultPool' => InvalidDeleteDefaultPoolError, 'Invalid_DeleteNonEmptyPool' => InvalidDeleteNonEmptyPoolError } 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 ⇒ Object
98 99 100 |
# File 'lib/mandrill.rb', line 98 def exports() Exports.new self end |
#inbound ⇒ Object
107 108 109 |
# File 'lib/mandrill.rb', line 107 def inbound() Inbound.new self end |
#internal ⇒ Object
122 123 124 |
# File 'lib/mandrill.rb', line 122 def internal() Internal.new self end |
#ips ⇒ Object
119 120 121 |
# File 'lib/mandrill.rb', line 119 def ips() Ips.new self end |
#messages ⇒ Object
113 114 115 |
# File 'lib/mandrill.rb', line 113 def () Messages.new self end |
#read_configs ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mandrill.rb', line 41 def read_configs() [File.('~/.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 ⇒ Object
104 105 106 |
# File 'lib/mandrill.rb', line 104 def rejects() Rejects.new self end |
#senders ⇒ Object
134 135 136 |
# File 'lib/mandrill.rb', line 134 def senders() Senders.new self end |
#subaccounts ⇒ Object
125 126 127 |
# File 'lib/mandrill.rb', line 125 def subaccounts() Subaccounts.new self end |
#tags ⇒ Object
110 111 112 |
# File 'lib/mandrill.rb', line 110 def () Tags.new self end |
#templates ⇒ Object
95 96 97 |
# File 'lib/mandrill.rb', line 95 def templates() Templates.new self end |
#urls ⇒ Object
128 129 130 |
# File 'lib/mandrill.rb', line 128 def urls() Urls.new self end |
#users ⇒ Object
101 102 103 |
# File 'lib/mandrill.rb', line 101 def users() Users.new self end |
#webhooks ⇒ Object
131 132 133 |
# File 'lib/mandrill.rb', line 131 def webhooks() Webhooks.new self end |
#whitelists ⇒ Object
116 117 118 |
# File 'lib/mandrill.rb', line 116 def whitelists() Whitelists.new self end |