wj-mailgun-ruby 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rubocop.yml +8 -0
  4. data/.rubocop_todo.yml +22 -0
  5. data/.ruby-env.yml.example +12 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +24 -0
  8. data/Gemfile +6 -0
  9. data/LICENSE +191 -0
  10. data/README.md +241 -0
  11. data/Rakefile +35 -0
  12. data/docs/Domains.md +54 -0
  13. data/docs/Events.md +46 -0
  14. data/docs/MessageBuilder.md +105 -0
  15. data/docs/Messages.md +107 -0
  16. data/docs/OptInHandler.md +103 -0
  17. data/docs/Snippets.md +526 -0
  18. data/docs/Suppressions.md +82 -0
  19. data/docs/Webhooks.md +40 -0
  20. data/lib/mailgun-ruby.rb +2 -0
  21. data/lib/mailgun.rb +39 -0
  22. data/lib/mailgun/address.rb +45 -0
  23. data/lib/mailgun/chains.rb +16 -0
  24. data/lib/mailgun/client.rb +199 -0
  25. data/lib/mailgun/domains/domains.rb +84 -0
  26. data/lib/mailgun/events/events.rb +120 -0
  27. data/lib/mailgun/exceptions/exceptions.rb +65 -0
  28. data/lib/mailgun/lists/opt_in_handler.rb +58 -0
  29. data/lib/mailgun/messages/batch_message.rb +125 -0
  30. data/lib/mailgun/messages/message_builder.rb +413 -0
  31. data/lib/mailgun/response.rb +62 -0
  32. data/lib/mailgun/suppressions.rb +270 -0
  33. data/lib/mailgun/version.rb +4 -0
  34. data/lib/mailgun/webhooks/webhooks.rb +101 -0
  35. data/lib/railgun.rb +8 -0
  36. data/lib/railgun/attachment.rb +56 -0
  37. data/lib/railgun/errors.rb +27 -0
  38. data/lib/railgun/mailer.rb +161 -0
  39. data/lib/railgun/message.rb +17 -0
  40. data/lib/railgun/railtie.rb +9 -0
  41. data/mailgun.gemspec +37 -0
  42. data/spec/integration/bounces_spec.rb +44 -0
  43. data/spec/integration/campaign_spec.rb +60 -0
  44. data/spec/integration/complaints_spec.rb +38 -0
  45. data/spec/integration/domains_spec.rb +39 -0
  46. data/spec/integration/email_validation_spec.rb +57 -0
  47. data/spec/integration/events_spec.rb +28 -0
  48. data/spec/integration/list_members_spec.rb +63 -0
  49. data/spec/integration/list_spec.rb +58 -0
  50. data/spec/integration/mailgun_spec.rb +121 -0
  51. data/spec/integration/messages/sample_data/mime.txt +38 -0
  52. data/spec/integration/routes_spec.rb +74 -0
  53. data/spec/integration/stats_spec.rb +15 -0
  54. data/spec/integration/suppressions_spec.rb +126 -0
  55. data/spec/integration/unsubscribes_spec.rb +42 -0
  56. data/spec/integration/webhook_spec.rb +54 -0
  57. data/spec/spec_helper.rb +45 -0
  58. data/spec/unit/connection/test_client.rb +99 -0
  59. data/spec/unit/events/events_spec.rb +50 -0
  60. data/spec/unit/lists/opt_in_handler_spec.rb +24 -0
  61. data/spec/unit/mailgun_spec.rb +127 -0
  62. data/spec/unit/messages/batch_message_spec.rb +131 -0
  63. data/spec/unit/messages/message_builder_spec.rb +584 -0
  64. data/spec/unit/messages/sample_data/mailgun_icon.png +0 -0
  65. data/spec/unit/messages/sample_data/mime.txt +38 -0
  66. data/spec/unit/messages/sample_data/rackspace_logo.jpg +0 -0
  67. data/vcr_cassettes/bounces.yml +175 -0
  68. data/vcr_cassettes/complaints.yml +175 -0
  69. data/vcr_cassettes/domains.todo.yml +42 -0
  70. data/vcr_cassettes/domains.yml +360 -0
  71. data/vcr_cassettes/email_validation.yml +167 -0
  72. data/vcr_cassettes/events.yml +108 -0
  73. data/vcr_cassettes/exceptions.yml +45 -0
  74. data/vcr_cassettes/list_members.yml +320 -0
  75. data/vcr_cassettes/mailing_list.todo.yml +43 -0
  76. data/vcr_cassettes/mailing_list.yml +390 -0
  77. data/vcr_cassettes/routes.yml +359 -0
  78. data/vcr_cassettes/send_message.yml +107 -0
  79. data/vcr_cassettes/stats.yml +44 -0
  80. data/vcr_cassettes/suppressions.yml +676 -0
  81. data/vcr_cassettes/unsubscribes.yml +191 -0
  82. data/vcr_cassettes/webhooks.yml +276 -0
  83. metadata +263 -0
@@ -0,0 +1,35 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Build Gem'
6
+ task :build do
7
+ system 'gem build mailgun.gemspec'
8
+ end
9
+
10
+ desc 'Run unit specs'
11
+ RSpec::Core::RakeTask.new('spec:unit') do |t|
12
+ t.rspec_opts = %w(--colour --format documentation)
13
+ t.pattern = 'spec/unit/*_spec.rb', 'spec/unit/*/*_spec.rb'
14
+ end
15
+
16
+ desc 'Run integration specs'
17
+ # Before running integration tests, you need to specify
18
+ # a valid API KEY in the spec/spec_helper.rb file.
19
+ RSpec::Core::RakeTask.new('spec:integration') do |t|
20
+ t.rspec_opts = %w(--colour --format documentation)
21
+ t.pattern = 'spec/integration/*_spec.rb'
22
+ end
23
+
24
+ desc 'Run all tests'
25
+ RSpec::Core::RakeTask.new('spec:all') do |t|
26
+ t.rspec_opts = %w(--colour --format documentation)
27
+ t.pattern = 'spec/**/*_spec.rb'
28
+ end
29
+
30
+ task default: 'spec:unit'
31
+ task spec: 'spec:unit'
32
+
33
+ task :console do
34
+ sh 'pry --gem'
35
+ end
@@ -0,0 +1,54 @@
1
+ Mailgun - Domains
2
+ ====================
3
+
4
+ This is the Mailgun Ruby *Domain* utilities.
5
+
6
+ The below assumes you've already installed the Mailgun Ruby SDK in to your
7
+ project. If not, go back to the master README for instructions. It currently supports
8
+ all calls except credentials.
9
+
10
+ Usage - Domains
11
+ -----------------------
12
+
13
+ ```ruby
14
+ # First, instantiate the Mailgun Client with your API key
15
+ mg_client = Mailgun::Client.new('your-api-key')
16
+ domainer = Mailgun::Domains.new(mg_client)
17
+
18
+ # Get a list of current domains.
19
+ domainer.list
20
+
21
+ # View details of a domain
22
+ domainer.info 'my.perfect.domain'
23
+
24
+ # Add a new domain
25
+ domainer.create 'my.new.moreness'
26
+ # or with options
27
+ domainer.create 'my.new.moreness', { some: 'options' }
28
+
29
+ # Remove a domain
30
+ domainer.remove 'this.one.is.not.needed.'
31
+ ```
32
+
33
+ Suppressions for a Domain
34
+ -------------------------
35
+
36
+ You can manage domain suppressions (bounces, unsubscribes, complaints) using the
37
+ [`Mailgun::Suppressions`](/docs/Suppressions.md) client:
38
+
39
+ ```ruby
40
+ # Instantiate the Mailgun Client with your API key
41
+ mg_client = Mailgun::Client.new('your-api-key')
42
+ supp_client = mg_client.suppressions('example.org')
43
+
44
+ # ...
45
+ ```
46
+
47
+ See the [Suppressions](/docs/Suppressions.md) for usage samples and
48
+ [suppressions.rb](/lib/mailgun/suppressions.rb) for suppressions client API.
49
+
50
+
51
+ More Documentation
52
+ ------------------
53
+ See the official [Mailgun Domain Docs](https://documentation.mailgun.com/api-domains.html)
54
+ for more information
@@ -0,0 +1,46 @@
1
+ Mailgun - Events
2
+ ====================
3
+
4
+ This is the Mailgun Ruby *Events* utility.
5
+
6
+ The below assumes you've already installed the Mailgun Ruby SDK in your project.
7
+ If not, go back to the master README for a few quick install steps.
8
+
9
+ Events: Provides methods for traversing the Mailgun Events API.
10
+
11
+
12
+ Usage - Events
13
+ -----------------------------------------------------
14
+ Here's how to use the Events Handler to pull events.
15
+
16
+ ```ruby
17
+ # First, instantiate the SDK with your API credentials, domain, and required parameters for example.
18
+ mg_client = Mailgun::Client.new("your-api-key")
19
+ mg_events = Mailgun::Events.new(mg_client, "your-domain")
20
+
21
+ result = mg_events.get({'limit' => 25,
22
+ 'recipient' => 'joe@example.com'})
23
+
24
+ result.to_h['items'].each do | item |
25
+ # outputs "Delivered - 20140509184016.12571.48844@example.com"
26
+ puts "#{item['event']} - #{item['message']['headers']['message-id']}"
27
+ end
28
+
29
+ # Want more results?
30
+ result = mg_events.next
31
+
32
+ # Go backwards?
33
+ result = mg_events.previous
34
+ ```
35
+
36
+ A few notes:
37
+ 1. Next will use the pagination links to advance the result set.
38
+ Retain the mg_events object to query forward, or reverse, at any time.
39
+ 2. If the result set is less than your limit, do not worry. A
40
+ second query against "next" will return the next 25 results since the
41
+ last time you called "next".
42
+
43
+ More Documentation
44
+ ------------------
45
+ See the official [Mailgun Docs](http://documentation.mailgun.com/api-sending.html)
46
+ for more information.
@@ -0,0 +1,105 @@
1
+ Mailgun - Messages
2
+ ====================
3
+
4
+ This is the Mailgun Ruby *Message* utilities.
5
+
6
+ The below assumes you've already installed the Mailgun Gem. If not, go back to the master README for instructions.
7
+
8
+ There are two utilities included, Message Builder and Batch Message.
9
+
10
+ Message Builder: Allows you to build a message object by calling methods for
11
+ each attribute.
12
+ Batch Message: Inherits Message Builder and allows you to iterate through
13
+ recipients from a list. Messages will fire after the 1,000th recipient has been
14
+ added.
15
+
16
+ Usage - Message Builder
17
+ -----------------------
18
+ Here's how to use Message Builder to build your Message.
19
+
20
+ ```ruby
21
+ # First, instantiate the Mailgun Client with your API key
22
+ mg_client = Mailgun::Client.new("your-api-key")
23
+ mb_obj = Mailgun::MessageBuilder.new()
24
+
25
+ # Define the from address.
26
+ mb_obj.from("me@example.com", {"first"=>"Ruby", "last" => "SDK"});
27
+
28
+ # Define a to recipient.
29
+ mb_obj.add_recipient(:to, "john.doe@example.com", {"first" => "John", "last" => "Doe"});
30
+
31
+ # Define a cc recipient.
32
+ mb_obj.add_recipient(:cc, "sally.doe@example.com", {"first" => "Sally", "last" => "Doe"});
33
+
34
+ # Define the subject.
35
+ mb_obj.subject("A message from the Ruby SDK using Message Builder!");
36
+
37
+ # Define the body of the message.
38
+ mb_obj.body_text("This is the text body of the message!");
39
+
40
+ # Set the Message-Id header, provide a valid Message-Id.
41
+ mb_obj.message_id("<20141014000000.11111.11111@example.com>")
42
+
43
+ # Or clear the Message-Id header, provide nil or empty string.
44
+ mb_obj.message_id(nil)
45
+ mb_obj.message_id('')
46
+
47
+ # Campaigns and headers.
48
+ mb_obj.add_campaign_id("My-Awesome-Campaign");
49
+ mb_obj.header("Customer-Id", "12345");
50
+
51
+ # Custom variables
52
+ mb_obj.variable("Customer-Data", { :first_name => "John", :last_name => "Smith" })
53
+
54
+ # Attach a file and rename it.
55
+ mb_obj.add_attachment "/path/to/file/receipt_123491820.pdf", "Receipt.pdf"
56
+
57
+ # Attach an image inline.
58
+ mb_obj.add_inline_image "/path/to/file/header.png"
59
+
60
+ # Schedule message in the future
61
+ mb_obj.deliver_at("tomorrow 8:00AM PST");
62
+
63
+ # Finally, send your message using the client
64
+ result = mg_client.send_message("sending_domain.com", mb_obj)
65
+
66
+ puts result.body.to_s
67
+ ```
68
+
69
+ Usage - Batch Message
70
+ ---------------------
71
+ Here's how to use Batch Message to easily handle batch sending jobs.
72
+
73
+ ```ruby
74
+ # First, instantiate the Mailgun Client with your API key
75
+ mg_client = Mailgun::Client.new("your-api-key")
76
+
77
+ # Create a Batch Message object, pass in the client and your domain.
78
+ mb_obj = Mailgun::BatchMessage.new(mg_client, "example.com")
79
+
80
+ # Define the from address.
81
+ mb_obj.from("me@example.com", {"first" => "Ruby", "last" => "SDK"});
82
+
83
+ # Define the subject.
84
+ mb_obj.subject("A message from the Ruby SDK using Message Builder!");
85
+
86
+ # Define the body of the message.
87
+ mb_obj.body_text("This is the text body of the message!");
88
+
89
+
90
+ # Loop through all of your recipients
91
+ mb_obj.add_recipient(:to, "john.doe@example.com", {"first" => "John", "last" => "Doe"});
92
+ mb_obj.add_recipient(:to, "jane.doe@example.com", {"first" => "Jane", "last" => "Doe"});
93
+ mb_obj.add_recipient(:to, "bob.doe@example.com", {"first" => "Bob", "last" => "Doe"});
94
+ ...
95
+ mb_obj.add_recipient(:to, "sally.doe@example.com", {"first" => "Sally", "last" => "Doe"});
96
+
97
+ # Call finalize to get a list of message ids and totals.
98
+ message_ids = mb_obj.finalize
99
+ # {'id1234@example.com' => 1000, 'id5678@example.com' => 15}
100
+ ```
101
+
102
+ More Documentation
103
+ ------------------
104
+ See the official [Mailgun Docs](http://documentation.mailgun.com/api-sending.html)
105
+ for more information.
@@ -0,0 +1,107 @@
1
+ Mailgun - Messages
2
+ ====================
3
+
4
+ This is the Mailgun Ruby *Message* utilities.
5
+
6
+ The below assumes you've already installed the Mailgun Ruby SDK in to your
7
+ project. If not, go back to the master README for instructions.
8
+
9
+ There are two utilities included, Message Builder and Batch Message.
10
+
11
+ Message Builder: Allows you to build a message object by calling methods for
12
+ each MIME attribute.
13
+ Batch Message: Inherits Message Builder and allows you to iterate through
14
+ recipients from a list. Messages will fire after the 1,000th recipient has been
15
+ added.
16
+
17
+ Usage - Message Builder
18
+ -----------------------
19
+ Here's how to use Message Builder to build your Message.
20
+
21
+ ```ruby
22
+ # First, instantiate the Mailgun Client with your API key
23
+ mg_client = Mailgun::Client.new("your-api-key")
24
+ mb_obj = Mailgun::MessageBuilder.new
25
+
26
+ # Define the from address.
27
+ mb_obj.set_from_address("me@example.com", {"first"=>"Ruby", "last" => "SDK"})
28
+
29
+ # Define a to recipient.
30
+ mb_obj.add_recipient(:to, "john.doe@example.com", {"first" => "John", "last" => "Doe"})
31
+
32
+ # Define a cc recipient.
33
+ mb_obj.add_recipient(:cc, "sally.doe@example.com", {"first" => "Sally", "last" => "Doe"})
34
+
35
+ # Define the subject.
36
+ mb_obj.set_subject("A message from the Ruby SDK using Message Builder!")
37
+
38
+ # Define the body of the message.
39
+ mb_obj.set_text_body("This is the text body of the message!")
40
+
41
+ # Define the HTML text of the message
42
+ mb_obj.set_html_body("<html><body><p>This is the text body of the message</p></body></html>")
43
+
44
+ # Set the Message-Id header. Pass in a valid Message-Id.
45
+ mb_obj.set_message_id("<20141014000000.11111.11111@example.com>")
46
+
47
+ # Clear the Message-Id header. Pass in nil or empty string.
48
+ mb_obj.set_message_id(nil)
49
+ mb_obj.set_message_id('')
50
+
51
+ # Other Optional Parameters.
52
+ mb_obj.add_campaign_id("My-Awesome-Campaign")
53
+ mb_obj.header("Customer-Id", "12345")
54
+ mb_obj.add_attachment("./tron.jpg")
55
+ mb_obj.set_delivery_time("tomorrow 8:00AM PST")
56
+ mb_obj.set_click_tracking(true)
57
+
58
+ # Send your message through the client
59
+ mg_client.send_message("sending_domain.com", mb_obj)
60
+ ```
61
+
62
+ Usage - Batch Message
63
+ ---------------------
64
+ Here's how to use Batch Message to easily handle batch sending jobs.
65
+
66
+ ```ruby
67
+ # First, instantiate the Mailgun Client with your API key
68
+ mg_client = Mailgun::Client.new("your-api-key")
69
+ mb_obj = Mailgun::BatchMessage.new(mg_client, "example.com")
70
+
71
+ # Define the from address.
72
+ mb_obj.set_from_address("me@example.com", {"first"=>"Ruby", "last" => "SDK"})
73
+
74
+ # Define the subject.
75
+ mb_obj.set_subject("A message from the Ruby SDK using Message Builder!")
76
+
77
+ # Define the body of the message.
78
+ mb_obj.set_text_body("Hello %recipient.first%,
79
+ This is the text body of the message
80
+ using recipient variables!
81
+ If you need to include custom data,
82
+ you could do it like this: %recipient.account-id%.")
83
+
84
+ mb_obj.add_recipient(:to, "john.doe@example.com", {"first" => "John",
85
+ "last" => "Doe",
86
+ "account-id" => 1234})
87
+
88
+ mb_obj.add_recipient(:to, "jane.doe@example.com", {"first" => "Jane",
89
+ "last" => "Doe",
90
+ "account-id" => 5678})
91
+
92
+ mb_obj.add_recipient(:to, "bob.doe@example.com", {"first" => "Bob",
93
+ "last" => "Doe",
94
+ "account-id" => 91011})
95
+ ...
96
+ mb_obj.add_recipient(:to, "sally.doe@example.com", {"first" => "Sally",
97
+ "last" => "Doe",
98
+ "account-id" => 121314})
99
+
100
+ # Send your message through the client
101
+ message_ids = mb_obj.finalize
102
+ ```
103
+
104
+ More Documentation
105
+ ------------------
106
+ See the official [Mailgun Docs](https://documentation.mailgun.com/api-sending.html)
107
+ for more information.
@@ -0,0 +1,103 @@
1
+ Mailgun - Lists
2
+ ====================
3
+
4
+ This is the Mailgun Ruby *Lists* utilities.
5
+
6
+ The below assumes you've already installed the Mailgun Ruby SDK in your project.
7
+ If not, go back to the master README for a few quick install steps.
8
+
9
+ OptInHandler: Provides methods for authenticating an Opt-In Request.
10
+
11
+ The typical flow for using this utility would be as follows:
12
+ **Recipient Requests Subscribe** -> [Validate Recipient Address] -> [Generate Opt-In Link] -> [Email Recipient Opt-In Link]
13
+ **Recipient Clicks Opt-In Link** -> [Validate Opt-In Link] -> [Subscribe User] -> [Send final confirmation]
14
+
15
+ The above flow is modeled below.
16
+
17
+ Usage - Opt-In Handler (Recipient Requests Subscribe)
18
+ -----------------------------------------------------
19
+ Here's how to use Opt-In Handler to validate Opt-In requests.
20
+
21
+ ```ruby
22
+ # First, instantiate the SDK with your API credentials, domain, and required parameters for example.
23
+ mg_client = Mailgun::Client.new("your-api-key")
24
+ mg_validate = Mailgun::Client.new("your-pub-api-key")
25
+
26
+ domain = 'example.com';
27
+
28
+ mailing_list = 'youlist@example.com';
29
+ secret_app_id = 'a_secret_passphrase';
30
+ recipient_address = 'recipient@example.com';
31
+
32
+ # Let's validate the customer's email address, using Mailgun's validation endpoint.
33
+ result = mg_validate.get('address/validate', {:address => recipient_address});
34
+ body = JSON.parse(result.body)
35
+
36
+ if body['is_valid'] == true
37
+ # Next, generate a hash.
38
+ generated_hash = Mailgun::OptInHandler.generate_hash(mailing_list, secret_app_id, recipient_address);
39
+
40
+ # Now, let's send a confirmation to the recipient with our link.
41
+ mg_client.send_message(domain, {:from => 'bob@example.com',
42
+ :to => recipient_address,
43
+ :subject => 'Please Confirm!',
44
+ :html => "<html><body>Hello,<br><br>You have requested to be subscribed
45
+ to the mailing list #{mailing_list}. Please <a
46
+ href=\"http://yourdomain.com/subscribe.php?hash=#{generated_hash}\">
47
+ confirm</a> your subscription.<br><br>Thank you!</body></html>"});
48
+
49
+ # Finally, let's add the subscriber to a Mailing List, as unsubscribed, so we can track non-conversions.
50
+ mg_client.post("lists/#{mailing_list}/members", {:address => recipient_address,
51
+ :subscribed => 'no',
52
+ :upsert => 'yes'});
53
+ end
54
+ ```
55
+
56
+ Usage - Opt-In Handler (Recipient Clicks Opt In Link)
57
+ -----------------------------------------------------
58
+ Here's how to use Opt-In Handler to validate an Opt-In Hash.
59
+
60
+ ```ruby
61
+ # First, instantiate the SDK with your API credentials and domain.
62
+ mg_client = Mailgun::Client.new("your-api-key")
63
+ domain = 'example.com';
64
+
65
+ # Now, validate the captured hash.
66
+ hash_validation = Mailgun::OptInHandler.validate_hash(secret_app_id, inbound_hash);
67
+
68
+ # Lastly, check to see if we have results, parse, subscribe, and send confirmation.
69
+ if !hash_validation.nil?
70
+ validated_list = hash_validation['mailing_list'];
71
+ validated_recipient = hash_validation['recipient_address'];
72
+
73
+ mg_client.put("lists/#{validated_list}/members/#{validated_recipient}",
74
+ {:address => validated_recipient,
75
+ :subscribed => 'yes'})
76
+
77
+ mg_client.send_message(domain, {:from => 'bob@example.com',
78
+ :to => validated_recipient,
79
+ :subject => 'Confirmation Received!',
80
+ :html => "<html><body>Hello,<br><br>We've successfully subscribed you to the list, #{validated_list}!<br><br>Thank you!
81
+ </body></html>"});
82
+ end
83
+ ```
84
+
85
+ A few notes:
86
+ 1. 'secret_app_id' can be anything. It's used as the *key* in hashing,
87
+ since your email address will vary. It should be secured like a password.
88
+ 2. validateHash will return an array containing the recipient address and list
89
+ address.
90
+ 3. You should *always* send an email confirmation before and after the
91
+ subscription request. This is what double-opt in means.
92
+
93
+
94
+ Available Functions
95
+ -----------------------------------------------------
96
+
97
+ `string generate_hash(string mailing_list, string secret_app_id, string recipient_address)`
98
+
99
+ `array validate_hash(string secret_app_id, string unique_hash)`
100
+
101
+ More Documentation
102
+ ------------------
103
+ See the official [Mailgun Docs](https://documentation.mailgun.com/api-sending.html) for more information.