mandrill-api 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mandrill.rb +107 -0
- data/lib/mandrill/api.rb +204 -0
- data/lib/mandrill/errors.rb +23 -0
- metadata +98 -0
data/lib/mandrill.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'excon'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'mandrill/errors'
|
6
|
+
require 'mandrill/api'
|
7
|
+
|
8
|
+
module Mandrill
|
9
|
+
class API
|
10
|
+
|
11
|
+
attr_accessor :host, :path, :apikey, :debug, :session
|
12
|
+
|
13
|
+
def initialize(apikey=nil, debug=false)
|
14
|
+
@host = 'https://mandrillapp.com'
|
15
|
+
@path = '/api/1.0/'
|
16
|
+
|
17
|
+
@session = Excon.new @host
|
18
|
+
@debug = debug
|
19
|
+
|
20
|
+
if not apikey
|
21
|
+
if ENV['MANDRILL_APIKEY']
|
22
|
+
apikey = ENV['MANDRILL_APIKEY']
|
23
|
+
else
|
24
|
+
apikey = read_configs
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
raise Error, 'You must provide a Mandrill API key' if not apikey
|
29
|
+
@apikey = apikey
|
30
|
+
end
|
31
|
+
|
32
|
+
def call(url, params={})
|
33
|
+
params[:key] = @apikey
|
34
|
+
params = JSON.generate(params)
|
35
|
+
r = @session.post(:path => "#{@path}#{url}.json", :headers => {'Content-Type' => 'application/json'}, :body => params)
|
36
|
+
|
37
|
+
cast_error(r.body) if r.status != 200
|
38
|
+
return JSON.parse(r.body)
|
39
|
+
end
|
40
|
+
|
41
|
+
def read_configs()
|
42
|
+
[File.expand_path('~/.mandrill.key'), '/etc/mandrill.key'].delete_if{ |p| not File.exist? p}.each do |path|
|
43
|
+
f = File.new path
|
44
|
+
apikey = f.read.strip
|
45
|
+
f.close
|
46
|
+
return apikey if apikey != ''
|
47
|
+
end
|
48
|
+
|
49
|
+
return nil
|
50
|
+
end
|
51
|
+
|
52
|
+
def cast_error(body)
|
53
|
+
|
54
|
+
error_map = {
|
55
|
+
'ValidationError' => ValidationError,
|
56
|
+
'Invalid_Key' => InvalidKeyError,
|
57
|
+
'Unknown_Template' => UnknownTemplateError,
|
58
|
+
'Invalid_Tag_Name' => InvalidTagNameError,
|
59
|
+
'Invalid_Reject' => InvalidRejectError,
|
60
|
+
'Unknown_Sender' => UnknownSenderError,
|
61
|
+
'Unknown_Url' => UnknownUrlError,
|
62
|
+
'Invalid_Template' => InvalidTemplateError,
|
63
|
+
'Unknown_Webhook' => UnknownWebhookError
|
64
|
+
}
|
65
|
+
|
66
|
+
begin
|
67
|
+
error_info = JSON.parse(body)
|
68
|
+
if error_info['status'] != 'error' or not error_info['name']:
|
69
|
+
raise Error, "We received an unexpected error: #{body}"
|
70
|
+
end
|
71
|
+
if error_map[error_info['name']]
|
72
|
+
raise error_map[error_info['name']], error_info['message']
|
73
|
+
else
|
74
|
+
raise Error, error_info['message']
|
75
|
+
end
|
76
|
+
rescue JSON::ParserError
|
77
|
+
raise Error, "We received an unexpected error: #{body}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def templates()
|
82
|
+
Templates.new self
|
83
|
+
end
|
84
|
+
def users()
|
85
|
+
Users.new self
|
86
|
+
end
|
87
|
+
def rejects()
|
88
|
+
Rejects.new self
|
89
|
+
end
|
90
|
+
def tags()
|
91
|
+
Tags.new self
|
92
|
+
end
|
93
|
+
def messages()
|
94
|
+
Messages.new self
|
95
|
+
end
|
96
|
+
def urls()
|
97
|
+
Urls.new self
|
98
|
+
end
|
99
|
+
def webhooks()
|
100
|
+
Webhooks.new self
|
101
|
+
end
|
102
|
+
def senders()
|
103
|
+
Senders.new self
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
data/lib/mandrill/api.rb
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
module Mandrill
|
2
|
+
|
3
|
+
class Templates
|
4
|
+
attr_accessor :master
|
5
|
+
|
6
|
+
def initialize(master)
|
7
|
+
@master = master
|
8
|
+
end
|
9
|
+
|
10
|
+
def add(name, code, publish=true)
|
11
|
+
_params = {:name => name, :code => code, :publish => publish}
|
12
|
+
return @master.call 'templates/add', _params
|
13
|
+
end
|
14
|
+
def info(name)
|
15
|
+
_params = {:name => name}
|
16
|
+
return @master.call 'templates/info', _params
|
17
|
+
end
|
18
|
+
def update(name, code, publish=true)
|
19
|
+
_params = {:name => name, :code => code, :publish => publish}
|
20
|
+
return @master.call 'templates/update', _params
|
21
|
+
end
|
22
|
+
def publish(name)
|
23
|
+
_params = {:name => name}
|
24
|
+
return @master.call 'templates/publish', _params
|
25
|
+
end
|
26
|
+
def delete(name)
|
27
|
+
_params = {:name => name}
|
28
|
+
return @master.call 'templates/delete', _params
|
29
|
+
end
|
30
|
+
def list()
|
31
|
+
_params = {}
|
32
|
+
return @master.call 'templates/list', _params
|
33
|
+
end
|
34
|
+
def time_series(name)
|
35
|
+
_params = {:name => name}
|
36
|
+
return @master.call 'templates/time-series', _params
|
37
|
+
end
|
38
|
+
def render(template_name, template_content, merge_vars=nil)
|
39
|
+
_params = {:template_name => template_name, :template_content => template_content, :merge_vars => merge_vars}
|
40
|
+
return @master.call 'templates/render', _params
|
41
|
+
end
|
42
|
+
end
|
43
|
+
class Users
|
44
|
+
attr_accessor :master
|
45
|
+
|
46
|
+
def initialize(master)
|
47
|
+
@master = master
|
48
|
+
end
|
49
|
+
|
50
|
+
def info()
|
51
|
+
_params = {}
|
52
|
+
return @master.call 'users/info', _params
|
53
|
+
end
|
54
|
+
def ping()
|
55
|
+
_params = {}
|
56
|
+
return @master.call 'users/ping2', _params
|
57
|
+
end
|
58
|
+
def senders()
|
59
|
+
_params = {}
|
60
|
+
return @master.call 'users/senders', _params
|
61
|
+
end
|
62
|
+
end
|
63
|
+
class Rejects
|
64
|
+
attr_accessor :master
|
65
|
+
|
66
|
+
def initialize(master)
|
67
|
+
@master = master
|
68
|
+
end
|
69
|
+
|
70
|
+
def list(email=nil)
|
71
|
+
_params = {:email => email}
|
72
|
+
return @master.call 'rejects/list', _params
|
73
|
+
end
|
74
|
+
def delete(email)
|
75
|
+
_params = {:email => email}
|
76
|
+
return @master.call 'rejects/delete', _params
|
77
|
+
end
|
78
|
+
end
|
79
|
+
class Tags
|
80
|
+
attr_accessor :master
|
81
|
+
|
82
|
+
def initialize(master)
|
83
|
+
@master = master
|
84
|
+
end
|
85
|
+
|
86
|
+
def list()
|
87
|
+
_params = {}
|
88
|
+
return @master.call 'tags/list', _params
|
89
|
+
end
|
90
|
+
def info(tag)
|
91
|
+
_params = {:tag => tag}
|
92
|
+
return @master.call 'tags/info', _params
|
93
|
+
end
|
94
|
+
def time_series(tag)
|
95
|
+
_params = {:tag => tag}
|
96
|
+
return @master.call 'tags/time-series', _params
|
97
|
+
end
|
98
|
+
def all_time_series()
|
99
|
+
_params = {}
|
100
|
+
return @master.call 'tags/all-time-series', _params
|
101
|
+
end
|
102
|
+
end
|
103
|
+
class Messages
|
104
|
+
attr_accessor :master
|
105
|
+
|
106
|
+
def initialize(master)
|
107
|
+
@master = master
|
108
|
+
end
|
109
|
+
|
110
|
+
def send(message)
|
111
|
+
_params = {:message => message}
|
112
|
+
return @master.call 'messages/send', _params
|
113
|
+
end
|
114
|
+
def send_template(template_name, template_content, message)
|
115
|
+
_params = {:template_name => template_name, :template_content => template_content, :message => message}
|
116
|
+
return @master.call 'messages/send-template', _params
|
117
|
+
end
|
118
|
+
def search(query='*', date_from=nil, date_to=nil, tags=nil, senders=nil, limit=100)
|
119
|
+
_params = {:query => query, :date_from => date_from, :date_to => date_to, :tags => tags, :senders => senders, :limit => limit}
|
120
|
+
return @master.call 'messages/search', _params
|
121
|
+
end
|
122
|
+
def parse(raw_message)
|
123
|
+
_params = {:raw_message => raw_message}
|
124
|
+
return @master.call 'messages/parse', _params
|
125
|
+
end
|
126
|
+
def send_raw(raw_message, from_email=nil, from_name=nil, to=nil)
|
127
|
+
_params = {:raw_message => raw_message, :from_email => from_email, :from_name => from_name, :to => to}
|
128
|
+
return @master.call 'messages/send-raw', _params
|
129
|
+
end
|
130
|
+
end
|
131
|
+
class Urls
|
132
|
+
attr_accessor :master
|
133
|
+
|
134
|
+
def initialize(master)
|
135
|
+
@master = master
|
136
|
+
end
|
137
|
+
|
138
|
+
def list()
|
139
|
+
_params = {}
|
140
|
+
return @master.call 'urls/list', _params
|
141
|
+
end
|
142
|
+
def search(q)
|
143
|
+
_params = {:q => q}
|
144
|
+
return @master.call 'urls/search', _params
|
145
|
+
end
|
146
|
+
def time_series(url)
|
147
|
+
_params = {:url => url}
|
148
|
+
return @master.call 'urls/time-series', _params
|
149
|
+
end
|
150
|
+
end
|
151
|
+
class Webhooks
|
152
|
+
attr_accessor :master
|
153
|
+
|
154
|
+
def initialize(master)
|
155
|
+
@master = master
|
156
|
+
end
|
157
|
+
|
158
|
+
def list()
|
159
|
+
_params = {}
|
160
|
+
return @master.call 'webhooks/list', _params
|
161
|
+
end
|
162
|
+
def add(url, events=[])
|
163
|
+
_params = {:url => url, :events => events}
|
164
|
+
return @master.call 'webhooks/add', _params
|
165
|
+
end
|
166
|
+
def info(id)
|
167
|
+
_params = {:id => id}
|
168
|
+
return @master.call 'webhooks/info', _params
|
169
|
+
end
|
170
|
+
def update(id, url, events=[])
|
171
|
+
_params = {:id => id, :url => url, :events => events}
|
172
|
+
return @master.call 'webhooks/update', _params
|
173
|
+
end
|
174
|
+
def delete(id)
|
175
|
+
_params = {:id => id}
|
176
|
+
return @master.call 'webhooks/delete', _params
|
177
|
+
end
|
178
|
+
end
|
179
|
+
class Senders
|
180
|
+
attr_accessor :master
|
181
|
+
|
182
|
+
def initialize(master)
|
183
|
+
@master = master
|
184
|
+
end
|
185
|
+
|
186
|
+
def list()
|
187
|
+
_params = {}
|
188
|
+
return @master.call 'senders/list', _params
|
189
|
+
end
|
190
|
+
def domains()
|
191
|
+
_params = {}
|
192
|
+
return @master.call 'senders/domains', _params
|
193
|
+
end
|
194
|
+
def info(address)
|
195
|
+
_params = {:address => address}
|
196
|
+
return @master.call 'senders/info', _params
|
197
|
+
end
|
198
|
+
def time_series(address)
|
199
|
+
_params = {:address => address}
|
200
|
+
return @master.call 'senders/time-series', _params
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Mandrill
|
2
|
+
class Error < Exception
|
3
|
+
end
|
4
|
+
class ValidationError < Error
|
5
|
+
end
|
6
|
+
class InvalidKeyError < Error
|
7
|
+
end
|
8
|
+
class UnknownTemplateError < Error
|
9
|
+
end
|
10
|
+
class InvalidTagNameError < Error
|
11
|
+
end
|
12
|
+
class InvalidRejectError < Error
|
13
|
+
end
|
14
|
+
class UnknownSenderError < Error
|
15
|
+
end
|
16
|
+
class UnknownUrlError < Error
|
17
|
+
end
|
18
|
+
class InvalidTemplateError < Error
|
19
|
+
end
|
20
|
+
class UnknownWebhookError < Error
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mandrill-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Mandrill Devs
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-31 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 7
|
32
|
+
- 4
|
33
|
+
version: 1.7.4
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: excon
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 43
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 15
|
48
|
+
- 4
|
49
|
+
version: 0.15.4
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
description: A Ruby API library for the Mandrill email as a service platform.
|
53
|
+
email: community@mandrill.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- lib/mandrill.rb
|
62
|
+
- lib/mandrill/api.rb
|
63
|
+
- lib/mandrill/errors.rb
|
64
|
+
homepage: https://bitbucket.org/mailchimp/mandrill-api-ruby/
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.24
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: A Ruby API library for the Mandrill email as a service platform.
|
97
|
+
test_files: []
|
98
|
+
|