mailchimp 0.0.1.alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/CONTRIBUTORS.markdown +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +18 -0
- data/README-api.markdown +93 -0
- data/README-mandrill.markdown +64 -0
- data/README-sts.markdown +70 -0
- data/README.markdown +58 -0
- data/Rakefile +26 -0
- data/examples/api_example.rb +17 -0
- data/examples/mandrill_example.rb +35 -0
- data/examples/sts_example.rb +25 -0
- data/lib/mailchimp/API.rb +94 -0
- data/lib/mailchimp/ext/httparty.rb +34 -0
- data/lib/mailchimp/handlers/mandrill_delivery_handler.rb +47 -0
- data/lib/mailchimp/handlers/sts_delivery_handler.rb +47 -0
- data/lib/mailchimp/mandrill.rb +61 -0
- data/lib/mailchimp/sts.rb +61 -0
- data/lib/mailchimp/version.rb +3 -0
- data/lib/mailchimp.rb +13 -0
- data/mailchimp.gemspec +29 -0
- data/test/integration/api_test.rb +18 -0
- data/test/integration/mandrill_test.rb +18 -0
- data/test/integration/sts_test.rb +18 -0
- data/test/lib/api_test.rb +191 -0
- data/test/lib/mandrill_test.rb +78 -0
- data/test/lib/sts_test.rb +78 -0
- data/test/test_helper.rb +22 -0
- metadata +157 -0
data/.gitignore
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
* [Chris Kelly](https://github.com/ckdake)
|
2
|
+
* [Stafford Brooke](https://github.com/srbiv)
|
3
|
+
* [Loren Norman](https://github.com/lorennorman)
|
4
|
+
* [Ali Faiz](https://github.com/alif)
|
5
|
+
* [Calvin Yu](https://github.com/cyu)
|
6
|
+
* [James Kyburz](https://github.com/JamesKyburz)
|
7
|
+
* [Justin Ip](https://github.com/ippy04)
|
8
|
+
* [elshimone](https://github.com/elshimone)
|
9
|
+
* [jlxw](https://github.com/jlxw)
|
10
|
+
* [Jon McCartie](https://github.com/jmccartie)
|
11
|
+
* [Calvin Yu](https://github.com/cyu)
|
12
|
+
* [Dave Worth](https://github.com/daveworth)
|
13
|
+
* [Mike Skalnik](https://github.com/skalnik)
|
14
|
+
* [Kristopher Murata](https://github.com/krsmurata)
|
15
|
+
* [Michael Klishin](https://github.com/michaelklishin)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
2
|
+
a copy of this software and associated documentation files (the
|
3
|
+
"Software"), to deal in the Software without restriction, including
|
4
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
5
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
6
|
+
permit persons to whom the Software is furnished to do so, subject to
|
7
|
+
the following conditions:
|
8
|
+
|
9
|
+
The above copyright notice and this permission notice shall be
|
10
|
+
included in all copies or substantial portions of the Software.
|
11
|
+
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
13
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
14
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
15
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
16
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
17
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
18
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README-api.markdown
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Mailchimp::API
|
2
|
+
|
3
|
+
API is a simple API wrapper for interacting with [MailChimp API](http://www.mailchimp.com/api) 1.3.
|
4
|
+
|
5
|
+
##Requirements
|
6
|
+
|
7
|
+
A MailChimp account and API key. You can see your API keys [here](http://admin.mailchimp.com/account/api).
|
8
|
+
|
9
|
+
##Usage
|
10
|
+
|
11
|
+
There are a few ways to use API:
|
12
|
+
|
13
|
+
You can create an instance of the API wrapper:
|
14
|
+
|
15
|
+
api = Mailchimp::API.new("your_api_key")
|
16
|
+
|
17
|
+
You can set your api_key globally and call class methods:
|
18
|
+
|
19
|
+
Mailchimp::API.api_key = "your_api_key"
|
20
|
+
Mailchimp::API.lists
|
21
|
+
|
22
|
+
You can also set the environment variable 'MC_API_KEY' and API will use it when you create an instance:
|
23
|
+
|
24
|
+
api = Mailchimp::API.new
|
25
|
+
|
26
|
+
Fetching data is as simple as calling API methods directly on the wrapper
|
27
|
+
object. The API calls may be made with either camelcase or underscore
|
28
|
+
separated formatting as you see in the "More Advanced Examples" section below.
|
29
|
+
|
30
|
+
Check the API [documentation](http://apidocs.mailchimp.com/api/1.3/) for details.
|
31
|
+
|
32
|
+
### Fetching Campaigns
|
33
|
+
|
34
|
+
For example, to fetch your first 100 campaigns (page 0):
|
35
|
+
|
36
|
+
campaigns = api.campaigns({:start => 0, :limit => 100})
|
37
|
+
|
38
|
+
### Fetching Lists
|
39
|
+
|
40
|
+
Similarly, to fetch your first 100 lists:
|
41
|
+
|
42
|
+
lists = api.lists({:start => 0, :limit=> 100})
|
43
|
+
|
44
|
+
### More Advanced Examples
|
45
|
+
|
46
|
+
Getting batch member information for subscribers looks like this:
|
47
|
+
|
48
|
+
info = api.list_member_info({:id => list_id, :email_address => email_array})
|
49
|
+
|
50
|
+
or
|
51
|
+
|
52
|
+
info = api.listMemberInfo({:id => list_id, :email_address => email_array})
|
53
|
+
|
54
|
+
Fetch open and click detail for recipients of a particular campaign:
|
55
|
+
|
56
|
+
email_stats = api.campaign_email_stats_aim({:cid => campaign_id, :email_address => email_array})
|
57
|
+
|
58
|
+
or
|
59
|
+
|
60
|
+
email_stats = api.campaignEmailStatsAIM({:cid => campaign_id, :email_address => email_array})
|
61
|
+
|
62
|
+
### Other Stuff
|
63
|
+
|
64
|
+
API defaults to a 30 second timeout. You can optionally set your own timeout (in seconds) like so:
|
65
|
+
|
66
|
+
api.timeout = 5
|
67
|
+
|
68
|
+
### Export API usage
|
69
|
+
|
70
|
+
In addition to the standard API you can make calls to the
|
71
|
+
[MailChimp Export API](http://apidocs.mailchimp.com/export/1.0/) using a APIExport object. Given an existing
|
72
|
+
Mailchimp::API object you can request a new Mailchimp::APIExporter object:
|
73
|
+
|
74
|
+
api = Mailchimp::API.new(@api_key)
|
75
|
+
mailchimp_export = api.get_exporter
|
76
|
+
|
77
|
+
or you can construct a new object directly:
|
78
|
+
|
79
|
+
mailchimp_export = Mailchimp::APIExport.new(@api_key)
|
80
|
+
|
81
|
+
Calling Export API functions is identical to making standard API calls but the
|
82
|
+
return value is an Enumerator which loops over the lines returned from the
|
83
|
+
Export API. This is because the data returned from the Export API is a stream
|
84
|
+
of JSON objects rather than a single JSON array.
|
85
|
+
|
86
|
+
### Error handling
|
87
|
+
|
88
|
+
By default you are expected to handle errors returned by the APIs manually. The
|
89
|
+
APIs will return a Hash with two keys "errors", a string containing some textual
|
90
|
+
information about the error, and "code", the numeric code of the error.
|
91
|
+
|
92
|
+
If you set the `throws_exceptions` boolean attribute for a given instance then
|
93
|
+
API will attempt to intercept the errors and raise an exception.
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Mailchimp::Mandrill
|
2
|
+
|
3
|
+
Mailchimp::Mandrill is a simple API wrapper for the [Mailchimp Mandrill API](http://mandrillapp.com/api/docs/index.html).
|
4
|
+
|
5
|
+
##Requirements
|
6
|
+
|
7
|
+
A MailChimp account and a Mandrill specific API key.
|
8
|
+
|
9
|
+
Note, to be able to send messages, you'll have to click an e-mail in a message sent to you the first time you try to use that sender.
|
10
|
+
|
11
|
+
##Usage
|
12
|
+
|
13
|
+
There are a few ways to use Mandrill.
|
14
|
+
|
15
|
+
You can create an instance of the API wrapper:
|
16
|
+
|
17
|
+
mandrill = Mailchimp::Mandrill.new("your_api_key")
|
18
|
+
|
19
|
+
You can set your api_key globally and call class methods:
|
20
|
+
|
21
|
+
Mailchimp::Mandrill.api_key = "your_api_key"
|
22
|
+
Mailchimp::Mandrill.user_senders(...)
|
23
|
+
|
24
|
+
You can also set the environment variable 'MAILCHIMP_MANDRILL_API_KEY' and Mandrill will use it when you create an instance:
|
25
|
+
|
26
|
+
mandrill = Mailchimp::Mandrill.new
|
27
|
+
|
28
|
+
### Sending a message
|
29
|
+
|
30
|
+
Send a message so a single email:
|
31
|
+
|
32
|
+
response = mandrill.messages_send({
|
33
|
+
:message => {
|
34
|
+
:subject => 'your subject',
|
35
|
+
:html => '<html>hello world</html>',
|
36
|
+
:text => 'hello world',
|
37
|
+
:from_name => 'John Smith',
|
38
|
+
:from_email => 'support@somedomain.com',
|
39
|
+
:to => ['user@someotherdomain.com']
|
40
|
+
}
|
41
|
+
})
|
42
|
+
|
43
|
+
Calling other methods is as simple as calling API methods directly on the Mandrill instance (e.g. mandrill.users_verify_sender(...)). Check the API [documentation](http://mandrillapp.com/api/docs/index.html)) for more information about API calls and their parameters.
|
44
|
+
|
45
|
+
|
46
|
+
### Plugging into ActionMailer
|
47
|
+
|
48
|
+
You can tell ActionMailer to send mail using Mandrill by adding the follow to to your config/application.rb or to the proper environment (eg. config/production.rb) :
|
49
|
+
|
50
|
+
config.action_mailer.delivery_method = :mailchimp_mandrill
|
51
|
+
config.action_mailer.mailchimp_mandrill_settings = {
|
52
|
+
:api_key => "your_mailchimp_mandrill_apikey",
|
53
|
+
:track_clicks => true,
|
54
|
+
:track_opens => true,
|
55
|
+
:from_name => "Change Me"
|
56
|
+
}
|
57
|
+
|
58
|
+
These setting will allow you to use ActionMailer as you normally would, any calls to mail() will be sent using Mandrill
|
59
|
+
|
60
|
+
### Other Stuff
|
61
|
+
|
62
|
+
Mandrill defaults to a 30 second timeout. You can optionally set your own timeout (in seconds) like so:
|
63
|
+
|
64
|
+
sts.timeout = 5
|
data/README-sts.markdown
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Mailchimp::STS
|
2
|
+
|
3
|
+
STS is a simple API wrapper for the [MailChimp STS API](http://http://apidocs.mailchimp.com/sts/1.0/) 1.0, which wraps Amazon SES.
|
4
|
+
|
5
|
+
##Requirements
|
6
|
+
|
7
|
+
A paid MailChimp account, MailChimp API key, and Amazon AWS account with SES ready to go. You can see your API keys [here](http://admin.mailchimp.com/account/api). Caveats include the inability to send to unconfirmed email addresses until you request (and Amazon provides) production access to your AWS account.
|
8
|
+
|
9
|
+
##Usage
|
10
|
+
|
11
|
+
There are a few ways to use STS.
|
12
|
+
|
13
|
+
You can create an instance of the API wrapper:
|
14
|
+
|
15
|
+
sts = Mailchimp::STS.new("your_api_key")
|
16
|
+
|
17
|
+
You can set your api_key globally and call class methods:
|
18
|
+
|
19
|
+
Mailchimp::STS.api_key = "your_api_key"
|
20
|
+
Mailchimp::STS.send_email(...)
|
21
|
+
|
22
|
+
You can also set the environment variable 'MAILCHIMP_API_KEY' and STS will use it when you create an instance:
|
23
|
+
|
24
|
+
sts = Mailchimp::STS.new
|
25
|
+
|
26
|
+
### Sending a message
|
27
|
+
|
28
|
+
Send a message so a single email:
|
29
|
+
|
30
|
+
response = sts.send_email({
|
31
|
+
:track_opens => true,
|
32
|
+
:track_clicks => true,
|
33
|
+
:tags => ["awesome", "tags", "here"] #optional STS tags
|
34
|
+
:message => {
|
35
|
+
:subject => 'your subject',
|
36
|
+
:html => '<html>hello world</html>',
|
37
|
+
:text => 'hello world',
|
38
|
+
:from_name => 'John Smith',
|
39
|
+
:from_email => 'support@somedomain.com',
|
40
|
+
:to_email => ['user@someotherdomain.com']
|
41
|
+
}
|
42
|
+
})
|
43
|
+
|
44
|
+
Calling other methods is as simple as calling API methods directly on the STS instance (e.g. u.get_send_quota, u.verify_email_address, and so on). Check the API [documentation](http://apidocs.mailchimp.com/sts/1.0/) for more information about API calls and their parameters.
|
45
|
+
|
46
|
+
|
47
|
+
### Plugging into ActionMailer
|
48
|
+
|
49
|
+
You can tell ActionMailer to send mail using Mailchimp STS by adding the follow to to your config/application.rb or to the proper environment (eg. config/production.rb) :
|
50
|
+
|
51
|
+
config.action_mailer.delivery_method = :mailchimp_sts
|
52
|
+
config.action_mailer.mailchimp_sts_settings = {
|
53
|
+
:api_key => "your_mailchimp_apikey",
|
54
|
+
:track_clicks => true,
|
55
|
+
:track_opens => true,
|
56
|
+
:from_name => "Change Me"
|
57
|
+
:tags => ["awesome", "tags", "here"] #optional STS tags
|
58
|
+
}
|
59
|
+
|
60
|
+
These setting will allow you to use ActionMailer as you normally would, any calls to mail() will be sent using Mailchimp STS
|
61
|
+
|
62
|
+
If, for some reason, you want to use ActionMailer and change your tags dynamically at runtime, you can do something like:
|
63
|
+
|
64
|
+
ActionMailer::Base.mailchimp_sts_settings[:tags] = ["dynamically", "set", "tags"]
|
65
|
+
|
66
|
+
### Other Stuff
|
67
|
+
|
68
|
+
STS defaults to a 30 second timeout. You can optionally set your own timeout (in seconds) like so:
|
69
|
+
|
70
|
+
sts.timeout = 5
|
data/README.markdown
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# mailchimp
|
2
|
+
|
3
|
+
Mailchimp is a simple API wrapper public Mailchimp APIs.
|
4
|
+
|
5
|
+
This is heavily based on https://github.com/amro/uakari/ and https://github.com/amro/gibbon/, and adds
|
6
|
+
support for the private beta release of Mandrill.
|
7
|
+
|
8
|
+
While this works for some simple use cases, it comes with warranty of any kind and may not work for
|
9
|
+
your needs yet!
|
10
|
+
|
11
|
+
We welcome bug reports and pull requests, and will remove the .alpha label from this gem once we are
|
12
|
+
confident that it should work out of the box for most usage scenarios.
|
13
|
+
|
14
|
+
##Installation
|
15
|
+
|
16
|
+
# This supercedes uakari and gibbon
|
17
|
+
$ gem install mailchimp
|
18
|
+
|
19
|
+
##Requirements
|
20
|
+
|
21
|
+
A MailChimp account, MailChimp API key, and any extra setup/keys for other services/APIs that
|
22
|
+
you want to use.
|
23
|
+
|
24
|
+
##Basic Usage
|
25
|
+
|
26
|
+
See README-api.markdown for details about the API class, which supersedes the Gibbon Gem
|
27
|
+
|
28
|
+
api = Mailchimp::API.new("your_api_key")
|
29
|
+
|
30
|
+
See README-sts.markdown for details about the STS class, which supersedes the Uakari Gem
|
31
|
+
|
32
|
+
sts = Mailchimp::STS.new("your_api_key")
|
33
|
+
|
34
|
+
See README-mandrill for details about the Mandrill class
|
35
|
+
|
36
|
+
mandrill = Mailchimp::Mandrill.new("your_api_key")
|
37
|
+
|
38
|
+
##Examples
|
39
|
+
|
40
|
+
Take a look in the examples/ folder for examples using each of the APIs. The only actions these perform
|
41
|
+
are either read only or only send emails to you or the address you specify, so it's ok to use your real
|
42
|
+
Mailchimp API keys for them. run them like:
|
43
|
+
|
44
|
+
ruby examples/api_example.rb
|
45
|
+
ruby examples/sts_example.rb
|
46
|
+
ruby examples/mandrill_example.rb
|
47
|
+
|
48
|
+
##Development
|
49
|
+
|
50
|
+
Write tests before you change anything, run tests before you commit anything:
|
51
|
+
|
52
|
+
$ rake test
|
53
|
+
|
54
|
+
We welcome concise pull requests that match the style of this gem and have appropriate tests.
|
55
|
+
|
56
|
+
##Contributors
|
57
|
+
|
58
|
+
See CONTRIBUTORS.markdown for the people that have contributed in some way to this Gem
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |test|
|
5
|
+
test.libs << 'lib' << 'test'
|
6
|
+
test.pattern = 'test/**/*_test.rb'
|
7
|
+
test.verbose = true
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: [:test]
|
11
|
+
|
12
|
+
namespace :cover_me do
|
13
|
+
desc "Generates and opens code coverage report."
|
14
|
+
task :report do
|
15
|
+
require 'cover_me'
|
16
|
+
CoverMe.complete!
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
task :test do
|
21
|
+
Rake::Task['cover_me:report'].invoke
|
22
|
+
end
|
23
|
+
|
24
|
+
task :spec do
|
25
|
+
Rake::Task['cover_me:report'].invoke
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
if ENV['MAILCHIMP_API_KEY'] == nil
|
2
|
+
puts 'Set ENV["MAILCHIMP_API_KEY"] to use this example'
|
3
|
+
exit
|
4
|
+
end
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
|
+
require 'mailchimp' # Use the 'mailchimp' gem instead
|
9
|
+
|
10
|
+
# Set MAILCHIMP_API_KEY in your environment to use this.
|
11
|
+
api = Mailchimp::API.new(ENV['MAILCHIMP_API_KEY'])
|
12
|
+
|
13
|
+
puts "Your lists:"
|
14
|
+
|
15
|
+
api.lists['data'].each do |list|
|
16
|
+
puts "\t #{list['name']}"
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
if ENV['MAILCHIMP_MANDRILL_API_KEY'] == nil
|
2
|
+
puts 'Set ENV["MAILCHIMP_MANDRILL_API_KEY"] to use this example'
|
3
|
+
exit
|
4
|
+
end
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
|
+
require 'mailchimp' # Use the 'mailchimp' gem instead
|
9
|
+
|
10
|
+
# Set MAILCHIMP_MANDRILL_API_KEY in your environment to use this.
|
11
|
+
mandrill = Mailchimp::Mandrill.new(ENV['MAILCHIMP_MANDRILL_API_KEY'])
|
12
|
+
|
13
|
+
puts "Your username is #{mandrill.users_info['username']}. lets email that person as that person to that person!"
|
14
|
+
|
15
|
+
response = mandrill.messages_send(
|
16
|
+
message: {
|
17
|
+
html: "<html><body><h1>Test</h1></body></html>",
|
18
|
+
text: "Test",
|
19
|
+
subject: "Test Message From Mandrill",
|
20
|
+
from_email: mandrill.users_info['username'],
|
21
|
+
from_name: 'Mandrill User',
|
22
|
+
to: [mandrill.users_info['username']]
|
23
|
+
}
|
24
|
+
)
|
25
|
+
|
26
|
+
puts "valid senders:"
|
27
|
+
|
28
|
+
mandrill.users_senders.map { |sender| puts "\t#{sender['address']}" }
|
29
|
+
|
30
|
+
puts "sending a validation request for your user..."
|
31
|
+
|
32
|
+
mandrill.users_verify_sender(email: mandrill.users_info['username'])
|
33
|
+
|
34
|
+
puts "Sent! Status: #{response['status']}"
|
35
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
if ENV['MAILCHIMP_API_KEY'] == nil
|
2
|
+
puts 'Set ENV["MAILCHIMP_API_KEY"] to use this example'
|
3
|
+
exit
|
4
|
+
end
|
5
|
+
|
6
|
+
if ENV['TEST_EMAIL_ADDRESS'] == nil
|
7
|
+
puts 'Set ENV["TEST_EMAIL_ADDRESS"] to use this example'
|
8
|
+
exit
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
13
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
+
require 'mailchimp' # Use the 'mailchimp' gem instead
|
15
|
+
|
16
|
+
# Set MAILCHIMP_API_KEY in your environment to use this. set TEST_EMAIL_ADDRESS to use this.
|
17
|
+
sts = Mailchimp::STS.new(ENV['MAILCHIMP_API_KEY'])
|
18
|
+
|
19
|
+
puts "valid senders:"
|
20
|
+
|
21
|
+
puts sts.list_verified_email_addresses['email_addresses'].map { |address| puts "\t#{address}" }
|
22
|
+
|
23
|
+
puts "sending a validation request for your user..."
|
24
|
+
|
25
|
+
sts.verify_email_address(email: ENV['TEST_EMAIL_ADDRESS'])
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module Mailchimp
|
2
|
+
class API
|
3
|
+
include HTTParty
|
4
|
+
format :plain
|
5
|
+
default_timeout 30
|
6
|
+
|
7
|
+
attr_accessor :api_key, :timeout, :throws_exceptions
|
8
|
+
|
9
|
+
def initialize(api_key = nil, extra_params = {})
|
10
|
+
@api_key = api_key || ENV['MAILCHIMP_API_KEY'] || self.class.api_key
|
11
|
+
@default_params = {:apikey => @api_key}.merge(extra_params)
|
12
|
+
@throws_exceptions = false
|
13
|
+
end
|
14
|
+
|
15
|
+
def api_key=(value)
|
16
|
+
@api_key = value
|
17
|
+
@default_params = @default_params.merge({:apikey => @api_key})
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_exporter
|
21
|
+
APIExport.new(@api_key, @default_params)
|
22
|
+
end
|
23
|
+
|
24
|
+
def base_api_url
|
25
|
+
"https://#{dc_from_api_key}api.mailchimp.com/1.3/?method="
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def call(method, params = {})
|
31
|
+
api_url = base_api_url + method
|
32
|
+
params = @default_params.merge(params)
|
33
|
+
response = self.class.post(api_url, :body => CGI::escape(params.to_json), :timeout => @timeout)
|
34
|
+
|
35
|
+
begin
|
36
|
+
response = JSON.parse(response.body)
|
37
|
+
rescue
|
38
|
+
response = JSON.parse('['+response.body+']').first
|
39
|
+
end
|
40
|
+
|
41
|
+
if @throws_exceptions && response.is_a?(Hash) && response["error"]
|
42
|
+
raise "Error from MailChimp API: #{response["error"]} (code #{response["code"]})"
|
43
|
+
end
|
44
|
+
|
45
|
+
response
|
46
|
+
end
|
47
|
+
|
48
|
+
def method_missing(method, *args)
|
49
|
+
method = method.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } #Thanks for the gsub, Rails
|
50
|
+
method = method[0].chr.downcase + method[1..-1].gsub(/aim$/i, 'AIM')
|
51
|
+
args = {} unless args.length > 0
|
52
|
+
args = args[0] if args.is_a?(Array)
|
53
|
+
call(method, args)
|
54
|
+
end
|
55
|
+
|
56
|
+
class << self
|
57
|
+
attr_accessor :api_key
|
58
|
+
|
59
|
+
def method_missing(sym, *args, &block)
|
60
|
+
new(self.api_key).send(sym, *args, &block)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def dc_from_api_key
|
65
|
+
(@api_key.nil? || @api_key.length == 0 || @api_key !~ /-/) ? '' : "#{@api_key.split("-").last}."
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class APIExport < API
|
70
|
+
def initialize(api_key = nil, extra_params = {})
|
71
|
+
super(api_key, extra_params)
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
|
76
|
+
def export_api_url
|
77
|
+
"http://#{dc_from_api_key}api.mailchimp.com/export/1.0/"
|
78
|
+
end
|
79
|
+
|
80
|
+
def call(method, params = {})
|
81
|
+
api_url = export_api_url + method + "/"
|
82
|
+
params = @default_params.merge(params)
|
83
|
+
response = self.class.post(api_url, :body => params, :timeout => @timeout)
|
84
|
+
|
85
|
+
lines = response.body.lines
|
86
|
+
if @throws_exceptions
|
87
|
+
first_line_object = JSON.parse(lines.first) if lines.first
|
88
|
+
raise "Error from MailChimp Export API: #{first_line_object["error"]} (code #{first_line_object["code"]})" if first_line_object.is_a?(Hash) && first_line_object["error"]
|
89
|
+
end
|
90
|
+
|
91
|
+
lines
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module HTTParty
|
2
|
+
module HashConversions
|
3
|
+
# @param key<Object> The key for the param.
|
4
|
+
# @param value<Object> The value for the param.
|
5
|
+
#
|
6
|
+
# @return <String> This key value pair as a param
|
7
|
+
#
|
8
|
+
# @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
|
9
|
+
def self.normalize_param(key, value)
|
10
|
+
param = ''
|
11
|
+
stack = []
|
12
|
+
|
13
|
+
if value.is_a?(Array)
|
14
|
+
param << Hash[*(0...value.length).to_a.zip(value).flatten].map {|i,element| normalize_param("#{key}[#{i}]", element)}.join
|
15
|
+
elsif value.is_a?(Hash)
|
16
|
+
stack << [key,value]
|
17
|
+
else
|
18
|
+
param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
|
19
|
+
end
|
20
|
+
|
21
|
+
stack.each do |parent, hash|
|
22
|
+
hash.each do |key, value|
|
23
|
+
if value.is_a?(Hash)
|
24
|
+
stack << ["#{parent}[#{key}]", value]
|
25
|
+
else
|
26
|
+
param << normalize_param("#{parent}[#{key}]", value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
param
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'action_mailer'
|
2
|
+
|
3
|
+
module Mailchimp
|
4
|
+
class MandrillDeliveryHandler
|
5
|
+
attr_accessor :settings
|
6
|
+
|
7
|
+
def initialize options
|
8
|
+
self.settings = {:track_opens => true, :track_clicks => true}.merge(options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def deliver! message
|
12
|
+
message_payload = {
|
13
|
+
:track_opens => settings[:track_opens],
|
14
|
+
:track_clicks => settings[:track_clicks],
|
15
|
+
:message => {
|
16
|
+
:subject => message.subject,
|
17
|
+
:from_name => settings[:from_name],
|
18
|
+
:from_email => message.from.first,
|
19
|
+
:to_email => message.to
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
mime_types = {
|
24
|
+
:html => "text/html",
|
25
|
+
:text => "text/plain"
|
26
|
+
}
|
27
|
+
|
28
|
+
get_content_for = lambda do |format|
|
29
|
+
content = message.send(:"#{format}_part")
|
30
|
+
content ||= message if message.content_type =~ %r{#{mime_types[format]}}
|
31
|
+
content
|
32
|
+
end
|
33
|
+
|
34
|
+
[:html, :text].each do |format|
|
35
|
+
content = get_content_for.call(format)
|
36
|
+
message_payload[:message][format] = content.body if content
|
37
|
+
end
|
38
|
+
|
39
|
+
message_payload[:tags] = settings[:tags] if settings[:tags]
|
40
|
+
|
41
|
+
Mandrill.new(settings[:api_key]).send_email(message_payload)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
ActionMailer::Base.add_delivery_method :mailchimp_mandrill, Mailchimp::MandrillDeliveryHandler
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'action_mailer'
|
2
|
+
|
3
|
+
module Mailchimp
|
4
|
+
class StsDeliveryHandler
|
5
|
+
attr_accessor :settings
|
6
|
+
|
7
|
+
def initialize options
|
8
|
+
self.settings = {:track_opens => true, :track_clicks => true}.merge(options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def deliver! message
|
12
|
+
message_payload = {
|
13
|
+
:track_opens => settings[:track_opens],
|
14
|
+
:track_clicks => settings[:track_clicks],
|
15
|
+
:message => {
|
16
|
+
:subject => message.subject,
|
17
|
+
:from_name => settings[:from_name],
|
18
|
+
:from_email => message.from.first,
|
19
|
+
:to_email => message.to
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
mime_types = {
|
24
|
+
:html => "text/html",
|
25
|
+
:text => "text/plain"
|
26
|
+
}
|
27
|
+
|
28
|
+
get_content_for = lambda do |format|
|
29
|
+
content = message.send(:"#{format}_part")
|
30
|
+
content ||= message if message.content_type =~ %r{#{mime_types[format]}}
|
31
|
+
content
|
32
|
+
end
|
33
|
+
|
34
|
+
[:html, :text].each do |format|
|
35
|
+
content = get_content_for.call(format)
|
36
|
+
message_payload[:message][format] = content.body if content
|
37
|
+
end
|
38
|
+
|
39
|
+
message_payload[:tags] = settings[:tags] if settings[:tags]
|
40
|
+
|
41
|
+
STS.new(settings[:api_key]).send_email(message_payload)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
ActionMailer::Base.add_delivery_method :mailchimp_sts, Mailchimp::StsDeliveryHandler
|