mailgun-ruby 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d2289f0c188dd1fd14e9560f3e7f9d8b14e9193
4
- data.tar.gz: 35e39b90b7185d846a515c642b7eb2340f09d37d
3
+ metadata.gz: 523ed8383a3318e626e33482a72431daf0453235
4
+ data.tar.gz: 67a635eb9b247d1fdaeaf2cb1e2944d2b85261d5
5
5
  SHA512:
6
- metadata.gz: ad0617bd4042f6dc27561c5ee864ec84f034f40c2b2fde569d996e10adfe830dbc9983d71b1be34dfd236154918c2659d1cebdee0487cb7e70f7685419037503
7
- data.tar.gz: 39002d5ffd24977418dadc676925df6503cd09aa73122bf3c5c2b15fac8575dcf3717e4a11b4d28229b283123c35ccf0b8aaaffb47c63b75dc5b9f201bf5fcde
6
+ metadata.gz: 9f3a6cfd00e2c88bc755411b4953139bd6115062236904edce82334d495b7edc4e529ad01f8e0ed68e35974105d0763f6175dbb5da2be44deb7b488ccc7391ba
7
+ data.tar.gz: df50ed26167e1cf88271ca954a0b084da8b135c006b2a27fe2050e3f10b7b795e6b4b283be159fbee0b5b8507aba899881e8d311ff670e18aba0523c275c0f7f
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.1.1
7
+ - 2.1.2
8
+ - 2.1.3
9
+ - 2.1.4
10
+ - 2.1.5
11
+ - 2.2.0-preview1
12
+ gemfile:
13
+ - Gemfile
14
+ script:
15
+ - bundle exec rake spec
data/Events.md ADDED
@@ -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.
data/MessageBuilder.md CHANGED
@@ -32,7 +32,11 @@ mb_obj.add_recipient(:cc, "sally.doe@example.com", {"first" => "Sally", "last" =
32
32
  mb_obj.set_subject("A message from the Ruby SDK using Message Builder!");
33
33
  # Define the body of the message.
34
34
  mb_obj.set_text_body("This is the text body of the message!");
35
-
35
+ # Set the Message-Id header, provide a valid Message-Id.
36
+ mb_obj.set_message_id("<20141014000000.11111.11111@example.com>")
37
+ # Or clear the Message-Id header, provide nil or empty string.
38
+ mb_obj.set_message_id(nil)
39
+ mb_obj.set_message_id('')
36
40
  # Campaign and other headers.
37
41
  mb_obj.add_campaign_id("My-Awesome-Campaign");
38
42
  mb_obj.add_custom_parameter("h:Customer-Id", "12345");
@@ -57,7 +61,7 @@ Here's how to use Batch Message to easily handle batch sending jobs.
57
61
  # First, instantiate the Mailgun Client with your API key
58
62
  mg_client = Mailgun::Client.new("your-api-key")
59
63
  # Create a Batch Message object, pass in the client and your domain.
60
- mb_obj = Mailgun::BatchMessage.new(@mb_client, "example.com")
64
+ mb_obj = Mailgun::BatchMessage.new(mg_client, "example.com")
61
65
 
62
66
  # Define the from address.
63
67
  mb_obj.set_from_address("me@example.com", {"first"=>"Ruby", "last" => "SDK"});
@@ -75,7 +79,7 @@ mb_obj.add_recipient(:to, "bob.doe@example.com", {"first" => "Bob", "last" => "D
75
79
  mb_obj.add_recipient(:to, "sally.doe@example.com", {"first" => "Sally", "last" => "Doe"});
76
80
 
77
81
  # Call finalize to get a list of message ids and totals.
78
- message_ids = mg_client.finalize
82
+ message_ids = mb_obj.finalize
79
83
  # {'id1234@example.com' => 1000, 'id5678@example.com' => 15}
80
84
  ```
81
85
 
data/Messages.md CHANGED
@@ -21,7 +21,7 @@ Here's how to use Message Builder to build your Message.
21
21
  ```ruby
22
22
  # First, instantiate the Mailgun Client with your API key
23
23
  mg_client = Mailgun::Client.new("your-api-key")
24
- mb_obj = Mailgun::MessageBuilder.new(@mb_client, "example.com")
24
+ mb_obj = Mailgun::MessageBuilder.new
25
25
 
26
26
  # Define the from address.
27
27
  mb_obj.set_from_address("me@example.com", {"first"=>"Ruby", "last" => "SDK"});
@@ -33,7 +33,11 @@ mb_obj.add_recipient(:cc, "sally.doe@example.com", {"first" => "Sally", "last" =
33
33
  mb_obj.set_subject("A message from the Ruby SDK using Message Builder!");
34
34
  # Define the body of the message.
35
35
  mb_obj.set_text_body("This is the text body of the message!");
36
-
36
+ # Set the Message-Id header. Pass in a valid Message-Id.
37
+ mb_obj.set_message_id("<20141014000000.11111.11111@example.com>")
38
+ # Clear the Message-Id header. Pass in nil or empty string.
39
+ mb_obj.set_message_id(nil)
40
+ mb_obj.set_message_id('')
37
41
  # Other Optional Parameters.
38
42
  mb_obj.add_campaign_id("My-Awesome-Campaign");
39
43
  mb_obj.add_custom_parameter("h:Customer-Id", "12345");
@@ -52,23 +56,35 @@ Here's how to use Batch Message to easily handle batch sending jobs.
52
56
  ```ruby
53
57
  # First, instantiate the Mailgun Client with your API key
54
58
  mg_client = Mailgun::Client.new("your-api-key")
55
- mb_obj = Mailgun::BatchMessage.new(@mb_client, "example.com")
59
+ mb_obj = Mailgun::BatchMessage.new(mg_client, "example.com")
56
60
 
57
61
  # Define the from address.
58
62
  mb_obj.set_from_address("me@example.com", {"first"=>"Ruby", "last" => "SDK"});
59
63
  # Define the subject.
60
64
  mb_obj.set_subject("A message from the Ruby SDK using Message Builder!");
61
65
  # Define the body of the message.
62
- mb_obj.set_text_body("This is the text body of the message!");
66
+ mb_obj.set_text_body("Hello %recipient.first%,
67
+ This is the text body of the message
68
+ using recipient variables!
69
+ If you need to include custom data,
70
+ you could do it like this: %account-id%.");
63
71
 
64
- mb_obj.add_recipient(:to, "john.doe@example.com", {"first" => "John", "last" => "Doe"});
65
- mb_obj.add_recipient(:to, "jane.doe@example.com", {"first" => "Jane", "last" => "Doe"});
66
- mb_obj.add_recipient(:to, "bob.doe@example.com", {"first" => "Bob", "last" => "Doe"});
72
+ mb_obj.add_recipient(:to, "john.doe@example.com", {"first" => "John",
73
+ "last" => "Doe",
74
+ "account-id" => 1234});
75
+ mb_obj.add_recipient(:to, "jane.doe@example.com", {"first" => "Jane",
76
+ "last" => "Doe",
77
+ "account-id" => 5678});
78
+ mb_obj.add_recipient(:to, "bob.doe@example.com", {"first" => "Bob",
79
+ "last" => "Doe",
80
+ "account-id" => 91011});
67
81
  ...
68
- mb_obj.add_recipient(:to, "sally.doe@example.com", {"first" => "Sally", "last" => "Doe"});
82
+ mb_obj.add_recipient(:to, "sally.doe@example.com", {"first" => "Sally",
83
+ "last" => "Doe",
84
+ "account-id" => 121314});
69
85
 
70
86
  # Send your message through the client
71
- message_ids = mg_client.finalize
87
+ message_ids = mb_obj.finalize
72
88
  ```
73
89
 
74
90
  More Documentation
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  Mailgun-Ruby
2
2
  ============
3
3
 
4
+ [![Build Status](https://travis-ci.org/mailgun/mailgun-ruby.svg?branch=master)](https://travis-ci.org/mailgun/mailgun-ruby) [![Gem Version](https://badge.fury.io/rb/mailgun-ruby.svg)](http://badge.fury.io/rb/mailgun-ruby)
5
+
4
6
  This is the Mailgun Ruby Library. This library contains methods for easily interacting
5
7
  with the Mailgun API.
6
8
  Below are examples to get you started. For additional examples, please see our
@@ -18,7 +20,7 @@ gem install mailgun-ruby
18
20
  Gemfile:
19
21
 
20
22
  ```ruby
21
- gem 'mailgun-ruby', "~>1.0.2"
23
+ gem 'mailgun-ruby', '~>1.0.3', require: 'mailgun'
22
24
  ```
23
25
 
24
26
  Include
@@ -128,7 +130,7 @@ For usage examples on each API endpoint, head over to our official documentation
128
130
  pages. Or the [Snippets](Snippets.md) file.
129
131
 
130
132
  This SDK includes a [Message Builder](Messages.md),
131
- [Batch Message](Messages.md) and [Opt-In Handler](OptInHandler.md) component.
133
+ [Batch Message](Messages.md), [Opt-In Handler](OptInHandler.md) and [Events](Events.md) component.
132
134
 
133
135
  Message Builder allows you to quickly create the array of parameters, required
134
136
  to send a message, by calling a methods for each parameter.
@@ -146,5 +148,5 @@ information about our API.
146
148
  If you find a bug, please submit the issue in Github directly.
147
149
  [Mailgun-Ruby Issues](https://github.com/mailgun/mailgun-ruby/issues)
148
150
 
149
- As always, if you need additional assistance, drop us a note at
150
- [support@mailgun.com](mailto:support@mailgun.com).
151
+ As always, if you need additional assistance, drop us a note through your Control Panel at
152
+ [https://mailgun.com/cp/support](https://mailgun.com/cp/support).
data/Snippets.md CHANGED
@@ -83,6 +83,7 @@ bm_obj = Mailgun::BatchMessage.new
83
83
 
84
84
  # Build message using Message Builder
85
85
  bm_obj.set_from_address :from, "sender@example.com", {'first' => 'Sending', 'last' => 'User'}
86
+ bm_obj.set_message_id("<20141014000000.11111.11111@example.com>") # optional
86
87
  bm_obj.set_subject :subject, "This is the subject!"
87
88
  bm_obj.set_text_body :text, "This is the text body."
88
89
 
data/lib/mailgun.rb CHANGED
@@ -70,7 +70,7 @@ module Mailgun
70
70
  response = @http_client[resource_path].post(data)
71
71
  Response.new(response)
72
72
  rescue Exception => e
73
- raise CommunicationError.new(e), e.response
73
+ communication_error e
74
74
  end
75
75
  end
76
76
 
@@ -91,7 +91,7 @@ module Mailgun
91
91
  end
92
92
  Response.new(response)
93
93
  rescue Exception => e
94
- raise CommunicationError.new(e), e.response
94
+ communication_error e
95
95
  end
96
96
  end
97
97
 
@@ -108,7 +108,7 @@ module Mailgun
108
108
  response = @http_client[resource_path].put(data)
109
109
  Response.new(response)
110
110
  rescue Exception => e
111
- raise CommunicationError.new(e), e.response
111
+ communication_error e
112
112
  end
113
113
  end
114
114
 
@@ -123,7 +123,7 @@ module Mailgun
123
123
  response = @http_client[resource_path].delete()
124
124
  Response.new(response)
125
125
  rescue Exception => e
126
- raise CommunicationError.new(e), e.response
126
+ communication_error e
127
127
  end
128
128
  end
129
129
 
@@ -156,6 +156,18 @@ module Mailgun
156
156
  "#{scheme}://#{api_host}"
157
157
  end
158
158
  end
159
+
160
+ # Raises CommunicationError and stores response in it if present
161
+ #
162
+ # @param [Exception] e upstream exception object
163
+
164
+ def communication_error(e)
165
+ if e.respond_to? :response
166
+ raise CommunicationError.new(e.message, e.response)
167
+ else
168
+ raise CommunicationError.new(e.message)
169
+ end
170
+ end
159
171
  end
160
172
 
161
173
  # A Mailgun::Response object is instantiated for each response generated
@@ -1,27 +1,20 @@
1
1
  module Mailgun
2
2
 
3
- class ParameterError < RuntimeError
3
+ class Error < RuntimeError
4
4
  attr_reader :object
5
5
 
6
6
  def initialize(message=nil, object=nil)
7
+ @message = message
7
8
  @object = object
8
9
  end
9
- end
10
-
11
- class CommunicationError < RuntimeError
12
- attr_reader :object
13
10
 
14
- def initialize(message=nil, object=nil)
15
- @object = object
11
+ def to_s
12
+ @message || self.class.to_s
16
13
  end
17
14
  end
18
15
 
19
- class ParseError < RuntimeError
20
- attr_reader :object
21
-
22
- def initialize(message=nil, object=nil)
23
- @object = object
24
- end
25
- end
16
+ class ParameterError < Error; end
17
+ class CommunicationError < Error; end
18
+ class ParseError < Error; end
26
19
 
27
20
  end
@@ -1,7 +1,7 @@
1
1
  require 'json'
2
2
  require 'uri'
3
3
  require 'base64'
4
- require 'digest/hmac'
4
+ require 'openssl'
5
5
 
6
6
  module Mailgun
7
7
 
@@ -21,7 +21,8 @@ module Mailgun
21
21
 
22
22
  innerPayloadEncoded = Base64.encode64(JSON.generate(innerPayload))
23
23
 
24
- digest = Digest::HMAC.hexdigest(innerPayloadEncoded, secret_app_id, Digest::SHA1)
24
+ sha1_digest = OpenSSL::Digest.new('sha1')
25
+ digest = OpenSSL::HMAC.hexdigest(sha1_digest, secret_app_id, innerPayloadEncoded)
25
26
 
26
27
  outerPayload = {'h' => digest,
27
28
  'p' => innerPayloadEncoded}
@@ -40,7 +41,8 @@ module Mailgun
40
41
  def self.validate_hash(secret_app_id, unique_hash)
41
42
  outerPayload = JSON.parse(Base64.decode64(URI.unescape(unique_hash)))
42
43
 
43
- generated_hash = Digest::HMAC.hexdigest(outerPayload['p'], secret_app_id, Digest::SHA1)
44
+ sha1_digest = OpenSSL::Digest.new('sha1')
45
+ generated_hash = OpenSSL::HMAC.hexdigest(sha1_digest, secret_app_id, outerPayload['p'])
44
46
 
45
47
  innerPayload = JSON.parse(Base64.decode64(URI.unescape(outerPayload['p'])))
46
48
 
@@ -96,6 +96,9 @@ module Mailgun
96
96
 
97
97
  def send_message(message)
98
98
  simple_setter("recipient-variables", JSON.generate(@recipient_variables))
99
+ if @message.has_key?("recipient-variables")
100
+ @message["recipient-variables"] = @message["recipient-variables"].first
101
+ end
99
102
  response = @client.send_message(@domain, @message).to_h!
100
103
  message_id = response['id'].gsub(/\>|\</, '')
101
104
  @message_ids[message_id] = count_recipients()
@@ -8,11 +8,11 @@ module Mailgun
8
8
  # generation through your code, this class is for you.
9
9
  #
10
10
  # See the Github documentation for full examples.
11
-
12
11
  class MessageBuilder
13
12
 
14
13
  attr_reader :message, :counters
15
14
 
15
+
16
16
  def initialize()
17
17
  @message = Hash.new{|hash, key| hash[key] = []}
18
18
  @counters = {:recipients =>
@@ -26,13 +26,13 @@ module Mailgun
26
26
  :tag => 0}}
27
27
  end
28
28
 
29
+
29
30
  # Adds a specific type of recipient to the message object.
30
31
  #
31
32
  # @param [String] recipient_type The type of recipient. "to", "cc", "bcc" or "h:reply-to".
32
33
  # @param [String] address The email address of the recipient to add to the message object.
33
34
  # @param [Hash] variables A hash of the variables associated with the recipient. We recommend "first" and "last" at a minimum!
34
35
  # @return [void]
35
-
36
36
  def add_recipient(recipient_type, address, variables=nil)
37
37
  if (@counters[:recipients][recipient_type] == 1000)
38
38
  raise ParameterError.new("Too many recipients added to message.", address)
@@ -46,49 +46,49 @@ module Mailgun
46
46
  end
47
47
  end
48
48
 
49
+
49
50
  # Sets the from address for the message
50
51
  #
51
52
  # @param [String] address The address of the sender.
52
53
  # @param [Hash] variables A hash of the variables associated with the recipient. We recommend "first" and "last" at a minimum!
53
54
  # @return [void]
54
-
55
55
  def set_from_address(address, variables=nil)
56
56
  add_recipient("from", address, variables)
57
57
  end
58
58
 
59
+
59
60
  # Set a subject for the message object
60
61
  #
61
62
  # @param [String] subject The subject for the email.
62
63
  # @return [void]
63
-
64
64
  def set_subject(subject=nil)
65
65
  simple_setter(:subject, subject)
66
66
  end
67
67
 
68
+
68
69
  # Set a text body for the message object
69
70
  #
70
71
  # @param [String] text_body The text body for the email.
71
72
  # @return [void]
72
-
73
73
  def set_text_body(text_body=nil)
74
74
  simple_setter(:text, text_body)
75
75
  end
76
76
 
77
+
77
78
  # Set a html body for the message object
78
79
  #
79
80
  # @param [String] html_body The html body for the email.
80
81
  # @return [void]
81
-
82
82
  def set_html_body(html_body=nil)
83
83
  simple_setter(:html, html_body)
84
84
  end
85
85
 
86
+
86
87
  # Adds a series of attachments, when called upon.
87
88
  #
88
89
  # @param [String] attachment A file object for attaching as an attachment.
89
90
  # @param [String] filename The filename you wish the attachment to be.
90
91
  # @return [void]
91
-
92
92
  def add_attachment(attachment, filename=nil)
93
93
  if attachment.is_a?(String)
94
94
  attachment = File.open(attachment, "r")
@@ -102,12 +102,12 @@ module Mailgun
102
102
  complex_setter(:attachment, attachment)
103
103
  end
104
104
 
105
+
105
106
  # Adds an inline image to the mesage object.
106
107
  #
107
108
  # @param [String] inline_image A file object for attaching an inline image.
108
109
  # @param [String] filename The filename you wish the inline image to be.
109
110
  # @return [void]
110
-
111
111
  def add_inline_image(inline_image, filename=nil)
112
112
  if inline_image.is_a?(String)
113
113
  inline_image = File.open(inline_image, "r")
@@ -121,29 +121,29 @@ module Mailgun
121
121
  complex_setter(:inline, inline_image)
122
122
  end
123
123
 
124
+
124
125
  # Send a message in test mode. (The message won't really be sent to the recipient)
125
126
  #
126
127
  # @param [Boolean] test_mode The boolean or string value (will fix itself)
127
128
  # @return [void]
128
-
129
129
  def set_test_mode(test_mode)
130
130
  simple_setter("o:testmode", bool_lookup(test_mode))
131
131
  end
132
132
 
133
+
133
134
  # Turn DKIM on or off per message
134
135
  #
135
136
  # @param [Boolean] dkim The boolean or string value(will fix itself)
136
137
  # @return [void]
137
-
138
138
  def set_dkim(dkim)
139
139
  simple_setter("o:dkim", bool_lookup(dkim))
140
140
  end
141
141
 
142
+
142
143
  # Add campaign IDs to message. Limit of 3 per message.
143
144
  #
144
145
  # @param [String] campaign_id A defined campaign ID to add to the message.
145
146
  # @return [void]
146
-
147
147
  def add_campaign_id(campaign_id)
148
148
  if (@counters[:attributes][:campaign_id] == 3)
149
149
  raise ParameterError.new("Too many campaigns added to message.", campaign_id)
@@ -151,12 +151,12 @@ module Mailgun
151
151
  complex_setter("o:campaign", campaign_id)
152
152
  @counters[:attributes][:campaign_id] += 1
153
153
  end
154
-
154
+
155
+
155
156
  # Add tags to message. Limit of 3 per message.
156
157
  #
157
158
  # @param [String] tag A defined campaign ID to add to the message.
158
159
  # @return [void]
159
-
160
160
  def add_tag(tag)
161
161
  if (@counters[:attributes][:tag] == 3)
162
162
  raise ParameterError.new("Too many tags added to message.", tag)
@@ -165,24 +165,25 @@ module Mailgun
165
165
  @counters[:attributes][:tag] += 1
166
166
  end
167
167
 
168
+
168
169
  # Turn Open Tracking on and off, on a per message basis.
169
170
  #
170
171
  # @param [Boolean] tracking Boolean true or false.
171
172
  # @return [void]
172
-
173
173
  def set_open_tracking(tracking)
174
174
  simple_setter("o:tracking-opens", bool_lookup(tracking))
175
175
  end
176
-
176
+
177
+
177
178
  # Turn Click Tracking on and off, on a per message basis.
178
179
  #
179
180
  # @param [String] tracking True, False, or HTML (for HTML only tracking)
180
181
  # @return [void]
181
-
182
182
  def set_click_tracking(tracking)
183
183
  simple_setter("o:tracking-clicks", bool_lookup(tracking))
184
184
  end
185
185
 
186
+
186
187
  # Enable Delivery delay on message. Specify an RFC2822 date, and Mailgun
187
188
  # will not deliver the message until that date/time. For conversion
188
189
  # options, see Ruby "Time". Example: "October 25, 2013 10:00PM CST" will
@@ -190,19 +191,18 @@ module Mailgun
190
191
  #
191
192
  # @param [String] timestamp A date and time, including a timezone.
192
193
  # @return [void]
193
-
194
194
  def set_delivery_time(timestamp)
195
195
  time_str = DateTime.parse(timestamp)
196
196
  simple_setter("o:deliverytime", time_str.rfc2822)
197
197
  end
198
198
 
199
+
199
200
  # Add custom data to the message. The data should be either a hash or JSON
200
201
  # encoded. The custom data will be added as a header to your message.
201
202
  #
202
203
  # @param [string] name A name for the custom data. (Ex. X-Mailgun-<Name of Data>: {})
203
204
  # @param [Hash] data Either a hash or JSON string.
204
205
  # @return [void]
205
-
206
206
  def set_custom_data(name, data)
207
207
  if data.is_a?(Hash)
208
208
  data = data.to_json
@@ -218,18 +218,37 @@ module Mailgun
218
218
  simple_setter("v:#{name}", data)
219
219
  end
220
220
 
221
+
221
222
  # Add custom parameter to the message. A custom parameter is any parameter that
222
223
  # is not yet supported by the SDK, but available at the API. Note: No validation
223
224
  # is performed. Don't forget to prefix the parameter with o, h, or v.
224
225
  #
225
226
  # @param [string] name A name for the custom parameter.
226
- # @param [string] data A string of data for the paramter.
227
+ # @param [string] data A string of data for the parameter.
227
228
  # @return [void]
228
229
 
229
230
  def add_custom_parameter(name, data)
230
231
  complex_setter(name, data)
231
232
  end
233
+
234
+
235
+ # Set the Message-Id header to a custom value. Don't forget to enclose the
236
+ # Message-Id in angle brackets, and ensure the @domain is present. Doesn't
237
+ # use simple or complex setters because it should not set value in an array.
238
+ #
239
+ # @param [string] data A string of data for the parameter. Passing nil or empty string will delete h:Message-Id key and value from @message hash.
240
+ # @return [void]
241
+ def set_message_id(data)
242
+ key = "h:Message-Id"
243
+
244
+ if data.to_s.empty?
245
+ @message.delete_if { |k, v| k == key }
246
+ else
247
+ @message[key] = data
248
+ end
249
+ end
232
250
 
251
+
233
252
  private
234
253
 
235
254
  # Sets values within the multidict, however, prevents
@@ -238,7 +257,6 @@ module Mailgun
238
257
  # @param [String] parameter The message object parameter name.
239
258
  # @param [String] value The value of the parameter.
240
259
  # @return [void]
241
-
242
260
  def simple_setter(parameter, value)
243
261
  if value.nil?
244
262
  @message[parameter] = ['']
@@ -247,13 +265,13 @@ module Mailgun
247
265
  end
248
266
  end
249
267
 
268
+
250
269
  # Sets values within the multidict, however, allows
251
270
  # duplicate values for keys.
252
271
  #
253
272
  # @param [String] parameter The message object parameter name.
254
273
  # @param [String] value The value of the parameter.
255
274
  # @return [void]
256
-
257
275
  def complex_setter(parameter, value)
258
276
  if value.nil?
259
277
  @message[parameter] << ''
@@ -262,11 +280,11 @@ module Mailgun
262
280
  end
263
281
  end
264
282
 
283
+
265
284
  # Converts boolean type to string
266
285
  #
267
286
  # @param [String] value The item to convert
268
287
  # @return [void]
269
-
270
288
  def bool_lookup(value)
271
289
  if value.is_a?(TrueClass) || value.is_a?(FalseClass)
272
290
  return value ? "yes" : "no"
@@ -288,7 +306,6 @@ module Mailgun
288
306
  #
289
307
  # @param [String] json_ The suspected JSON string.
290
308
  # @return [void]
291
-
292
309
  def valid_json? json_
293
310
  JSON.parse(json_)
294
311
  return true
@@ -303,7 +320,6 @@ module Mailgun
303
320
  # @param [String] address The email address to parse.
304
321
  # @param [Hash] variables A list of recipient variables.
305
322
  # @return [void]
306
-
307
323
  def parse_address(address, variables)
308
324
  if variables.nil?
309
325
  return address
@@ -1,5 +1,5 @@
1
1
  module Mailgun
2
2
 
3
- VERSION = "1.0.2"
3
+ VERSION = "1.0.3"
4
4
 
5
5
  end
data/mailgun.gemspec CHANGED
@@ -22,15 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
23
  spec.require_paths = ["lib"]
24
24
 
25
- spec.post_install_message = %q{
26
- ---------------------------------------------------------------
27
- Congrats, you've successfully installed the Mailgun SDK!
28
- Check out our documentation at http://documentation.mailgun.com
29
-
30
- Contact us at support@mailgunhq.com with any questions.
31
- ----------------------------------------------------------------
32
- }
33
-
34
25
  spec.add_development_dependency "bundler", "~> 1.3"
35
26
  spec.add_development_dependency "rspec", "~> 2.14"
36
27
  spec.add_development_dependency "rake", "~> 10.1"
@@ -152,26 +152,6 @@ module Mailgun
152
152
  end
153
153
  end
154
154
 
155
- def to_h
156
- begin
157
- JSON.parse(@body)
158
- rescue Exception => e
159
- raise ParseError.new(e), e
160
- end
161
- end
162
-
163
- # Replace @body with Ruby Hash
164
- #
165
- # @return [Hash] A standard Ruby Hash containing the HTTP result.
166
-
167
- def to_h!
168
- begin
169
- @body = JSON.parse(@body)
170
- rescue Exception => e
171
- raise ParseError.new(e), e
172
- end
173
- end
174
-
175
155
  # Return response as Yaml
176
156
  #
177
157
  # @return [String] A string containing response as YAML
@@ -180,26 +180,26 @@ describe 'The method set_test_mode' do
180
180
  end
181
181
  it 'turns on test mode with boolean true' do
182
182
  @mb_obj.set_test_mode(true)
183
- @mb_obj.message["o:testmode"][0].should eq ("yes")
183
+ @mb_obj.message["o:testmode"][0].should eq("yes")
184
184
  end
185
185
  it 'turns on test mode with string true' do
186
186
  @mb_obj.set_test_mode("true")
187
- @mb_obj.message["o:testmode"][0].should eq ("yes")
187
+ @mb_obj.message["o:testmode"][0].should eq("yes")
188
188
  end
189
189
  it 'turns off test mode with boolean false' do
190
190
  @mb_obj.set_test_mode(false)
191
- @mb_obj.message["o:testmode"][0].should eq ("no")
191
+ @mb_obj.message["o:testmode"][0].should eq("no")
192
192
  end
193
193
  it 'turns off test mode with string false' do
194
194
  @mb_obj.set_test_mode("false")
195
- @mb_obj.message["o:testmode"][0].should eq ("no")
195
+ @mb_obj.message["o:testmode"][0].should eq("no")
196
196
  end
197
197
  it 'does not allow multiple values' do
198
198
  @mb_obj.set_test_mode("false")
199
199
  @mb_obj.set_test_mode("true")
200
200
  @mb_obj.set_test_mode("false")
201
201
  @mb_obj.message["o:testmode"].length.should eq(1)
202
- @mb_obj.message["o:testmode"][0].should eq ("no")
202
+ @mb_obj.message["o:testmode"][0].should eq("no")
203
203
  end
204
204
  end
205
205
 
@@ -209,26 +209,26 @@ describe 'The method set_dkim' do
209
209
  end
210
210
  it 'turns on dkim with boolean true' do
211
211
  @mb_obj.set_dkim(true)
212
- @mb_obj.message["o:dkim"][0].should eq ("yes")
212
+ @mb_obj.message["o:dkim"][0].should eq("yes")
213
213
  end
214
214
  it 'turns on dkim with string true' do
215
215
  @mb_obj.set_dkim("true")
216
- @mb_obj.message["o:dkim"][0].should eq ("yes")
216
+ @mb_obj.message["o:dkim"][0].should eq("yes")
217
217
  end
218
218
  it 'turns off dkim with boolean false' do
219
219
  @mb_obj.set_dkim(false)
220
- @mb_obj.message["o:dkim"][0].should eq ("no")
220
+ @mb_obj.message["o:dkim"][0].should eq("no")
221
221
  end
222
222
  it 'turns off dkim with string false' do
223
223
  @mb_obj.set_dkim("false")
224
- @mb_obj.message["o:dkim"][0].should eq ("no")
224
+ @mb_obj.message["o:dkim"][0].should eq("no")
225
225
  end
226
226
  it 'does not allow multiple values' do
227
227
  @mb_obj.set_dkim("false")
228
228
  @mb_obj.set_dkim("true")
229
229
  @mb_obj.set_dkim("false")
230
230
  @mb_obj.message["o:dkim"].length.should eq(1)
231
- @mb_obj.message["o:dkim"][0].should eq ("no")
231
+ @mb_obj.message["o:dkim"][0].should eq("no")
232
232
  end
233
233
  end
234
234
 
@@ -244,9 +244,9 @@ describe 'The method add_campaign_id' do
244
244
  @mb_obj.add_campaign_id('My-Campaign-Id-1')
245
245
  @mb_obj.add_campaign_id('My-Campaign-Id-2')
246
246
  @mb_obj.add_campaign_id('My-Campaign-Id-3')
247
- @mb_obj.message["o:campaign"][0].should eq ("My-Campaign-Id-1")
248
- @mb_obj.message["o:campaign"][1].should eq ("My-Campaign-Id-2")
249
- @mb_obj.message["o:campaign"][2].should eq ("My-Campaign-Id-3")
247
+ @mb_obj.message["o:campaign"][0].should eq("My-Campaign-Id-1")
248
+ @mb_obj.message["o:campaign"][1].should eq("My-Campaign-Id-2")
249
+ @mb_obj.message["o:campaign"][2].should eq("My-Campaign-Id-3")
250
250
  end
251
251
  it 'adds too many campaign IDs to the message' do
252
252
  expect{
@@ -262,15 +262,15 @@ describe 'The method add_tag' do
262
262
  end
263
263
  it 'adds a tag to the message' do
264
264
  @mb_obj.add_tag('My-Tag-1')
265
- @mb_obj.message["o:tag"][0].should eq ("My-Tag-1")
265
+ @mb_obj.message["o:tag"][0].should eq("My-Tag-1")
266
266
  end
267
267
  it 'adds a few more tags to the message' do
268
268
  @mb_obj.add_tag('My-Tag-1')
269
269
  @mb_obj.add_tag('My-Tag-2')
270
270
  @mb_obj.add_tag('My-Tag-3')
271
- @mb_obj.message["o:tag"][0].should eq ("My-Tag-1")
272
- @mb_obj.message["o:tag"][1].should eq ("My-Tag-2")
273
- @mb_obj.message["o:tag"][2].should eq ("My-Tag-3")
271
+ @mb_obj.message["o:tag"][0].should eq("My-Tag-1")
272
+ @mb_obj.message["o:tag"][1].should eq("My-Tag-2")
273
+ @mb_obj.message["o:tag"][2].should eq("My-Tag-3")
274
274
  end
275
275
  it 'adds too many tags to the message' do
276
276
  expect{
@@ -286,13 +286,13 @@ describe 'The method set_open_tracking' do
286
286
  end
287
287
  it 'enables/disables open tracking on a per message basis.' do
288
288
  @mb_obj.set_open_tracking('Yes')
289
- @mb_obj.message["o:tracking-opens"][0].should eq ("yes")
289
+ @mb_obj.message["o:tracking-opens"][0].should eq("yes")
290
290
  @mb_obj.set_open_tracking('No')
291
- @mb_obj.message["o:tracking-opens"][0].should eq ("no")
291
+ @mb_obj.message["o:tracking-opens"][0].should eq("no")
292
292
  @mb_obj.set_open_tracking(true)
293
- @mb_obj.message["o:tracking-opens"][0].should eq ("yes")
293
+ @mb_obj.message["o:tracking-opens"][0].should eq("yes")
294
294
  @mb_obj.set_open_tracking(false)
295
- @mb_obj.message["o:tracking-opens"][0].should eq ("no")
295
+ @mb_obj.message["o:tracking-opens"][0].should eq("no")
296
296
  end
297
297
  end
298
298
 
@@ -302,15 +302,15 @@ describe 'The method set_click_tracking' do
302
302
  end
303
303
  it 'enables/disables click tracking on a per message basis.' do
304
304
  @mb_obj.set_click_tracking('Yes')
305
- @mb_obj.message["o:tracking-clicks"][0].should eq ("yes")
305
+ @mb_obj.message["o:tracking-clicks"][0].should eq("yes")
306
306
  @mb_obj.set_click_tracking('No')
307
- @mb_obj.message["o:tracking-clicks"][0].should eq ("no")
307
+ @mb_obj.message["o:tracking-clicks"][0].should eq("no")
308
308
  @mb_obj.set_click_tracking(true)
309
- @mb_obj.message["o:tracking-clicks"][0].should eq ("yes")
309
+ @mb_obj.message["o:tracking-clicks"][0].should eq("yes")
310
310
  @mb_obj.set_click_tracking(false)
311
- @mb_obj.message["o:tracking-clicks"][0].should eq ("no")
311
+ @mb_obj.message["o:tracking-clicks"][0].should eq("no")
312
312
  @mb_obj.set_click_tracking('html')
313
- @mb_obj.message["o:tracking-clicks"][0].should eq ("html")
313
+ @mb_obj.message["o:tracking-clicks"][0].should eq("html")
314
314
  end
315
315
  end
316
316
 
@@ -320,7 +320,7 @@ describe 'The method set_delivery_time' do
320
320
  end
321
321
  it 'defines a time/date to deliver a message in RFC2822 format.' do
322
322
  @mb_obj.set_delivery_time('October 25, 2013 10:00PM CST')
323
- @mb_obj.message["o:deliverytime"][0].should eq ("Fri, 25 Oct 2013 22:00:00 -0600")
323
+ @mb_obj.message["o:deliverytime"][0].should eq("Fri, 25 Oct 2013 22:00:00 -0600")
324
324
  end
325
325
  end
326
326
 
@@ -330,12 +330,12 @@ describe 'The method set_custom_data' do
330
330
  end
331
331
  it 'accepts valid JSON and appends as data to the message.' do
332
332
  @mb_obj.set_custom_data('my-data', '{"key":"value"}')
333
- @mb_obj.message["v:my-data"][0].should eq ("{\"key\":\"value\"}")
333
+ @mb_obj.message["v:my-data"][0].should eq("{\"key\":\"value\"}")
334
334
  end
335
335
  it 'accepts a hash and appends as data to the message.' do
336
336
  data = {'key'=> 'value'}
337
337
  @mb_obj.set_custom_data('my-data', data)
338
- @mb_obj.message["v:my-data"][0].should eq ("{\"key\":\"value\"}")
338
+ @mb_obj.message["v:my-data"][0].should eq("{\"key\":\"value\"}")
339
339
  end
340
340
  it 'throws an exception on broken JSON.' do
341
341
  data = 'This is some crappy JSON.'
@@ -349,6 +349,27 @@ describe 'The method add_custom_parameter' do
349
349
  end
350
350
  it 'adds an undefined parameter to the message.' do
351
351
  @mb_obj.add_custom_parameter('h:my-sweet-header', 'datagoeshere')
352
- @mb_obj.message["h:my-sweet-header"][0].should eq ("datagoeshere")
352
+ @mb_obj.message["h:my-sweet-header"][0].should eq("datagoeshere")
353
+ end
354
+ end
355
+
356
+ describe 'The method set_message_id' do
357
+ before(:each) do
358
+ @mb_obj = Mailgun::MessageBuilder.new
359
+ @the_message_id = '<20141014000000.11111.11111@example.com>'
360
+ end
361
+ it 'correctly sets the Message-Id header' do
362
+ @mb_obj.set_message_id(@the_message_id)
363
+ @mb_obj.message['h:Message-Id'].should eq(@the_message_id)
364
+ end
365
+ it 'correctly clears the Message-Id header when passed nil' do
366
+ @mb_obj.set_message_id(nil)
367
+ @mb_obj.message.has_key?('h:Message-Id').should eq(false)
368
+ end
369
+ it 'correctly sets the Message-Id header when passed an empty string' do
370
+ @mb_obj.set_message_id(@the_message_id)
371
+ @mb_obj.message.has_key?('h:Message-Id').should eq(true)
372
+ @mb_obj.set_message_id('')
373
+ @mb_obj.message.has_key?('h:Message-Id').should eq(false)
353
374
  end
354
375
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailgun-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mailgun
@@ -9,76 +9,76 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-09 00:00:00.000000000 Z
12
+ date: 2014-11-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
20
  version: '1.3'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ~>
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.3'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rspec
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ~>
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
34
  version: '2.14'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ~>
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
41
  version: '2.14'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rake
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ~>
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
48
  version: '10.1'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ~>
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '10.1'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rest-client
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ~>
60
+ - - "~>"
61
61
  - !ruby/object:Gem::Version
62
62
  version: '1.6'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ~>
67
+ - - "~>"
68
68
  - !ruby/object:Gem::Version
69
69
  version: '1.6'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: json
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ~>
74
+ - - "~>"
75
75
  - !ruby/object:Gem::Version
76
76
  version: '1.8'
77
77
  type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ~>
81
+ - - "~>"
82
82
  - !ruby/object:Gem::Version
83
83
  version: '1.8'
84
84
  description: Mailgun's Official Ruby SDK for interacting with the Mailgun API.
@@ -87,7 +87,9 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - .gitignore
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Events.md
91
93
  - Gemfile
92
94
  - LICENSE
93
95
  - MessageBuilder.md
@@ -120,27 +122,23 @@ homepage: http://www.mailgun.com
120
122
  licenses:
121
123
  - Apache
122
124
  metadata: {}
123
- post_install_message: "\n ---------------------------------------------------------------\n
124
- \ Congrats, you've successfully installed the Mailgun SDK!\n Check out our
125
- documentation at http://documentation.mailgun.com\n\n Contact us at support@mailgunhq.com
126
- with any questions.\n ----------------------------------------------------------------\n
127
- \ "
125
+ post_install_message:
128
126
  rdoc_options: []
129
127
  require_paths:
130
128
  - lib
131
129
  required_ruby_version: !ruby/object:Gem::Requirement
132
130
  requirements:
133
- - - '>='
131
+ - - ">="
134
132
  - !ruby/object:Gem::Version
135
133
  version: '0'
136
134
  required_rubygems_version: !ruby/object:Gem::Requirement
137
135
  requirements:
138
- - - '>='
136
+ - - ">="
139
137
  - !ruby/object:Gem::Version
140
138
  version: '0'
141
139
  requirements: []
142
140
  rubyforge_project:
143
- rubygems_version: 2.0.3
141
+ rubygems_version: 2.2.2
144
142
  signing_key:
145
143
  specification_version: 4
146
144
  summary: Mailgun's Official Ruby SDK