mailjet 1.4.12 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -10
  3. data/lib/mailjet/configuration.rb +9 -4
  4. data/lib/mailjet/connection.rb +15 -13
  5. data/lib/mailjet/mailer.rb +170 -22
  6. data/lib/mailjet/message_delivery.rb +2 -3
  7. data/lib/mailjet/resource.rb +82 -47
  8. data/lib/mailjet/resources/aggregategraphstatistics.rb +1 -3
  9. data/lib/mailjet/resources/apikey.rb +1 -3
  10. data/lib/mailjet/resources/apikeyaccess.rb +1 -3
  11. data/lib/mailjet/resources/apikeytotals.rb +1 -3
  12. data/lib/mailjet/resources/apitoken.rb +1 -3
  13. data/lib/mailjet/resources/axtesting.rb +1 -3
  14. data/lib/mailjet/resources/batchjob.rb +1 -3
  15. data/lib/mailjet/resources/bouncestatistics.rb +1 -3
  16. data/lib/mailjet/resources/campaign.rb +1 -3
  17. data/lib/mailjet/resources/campaignaggregate.rb +1 -3
  18. data/lib/mailjet/resources/campaigndraft.rb +0 -2
  19. data/lib/mailjet/resources/campaigndraft_detailcontent.rb +0 -2
  20. data/lib/mailjet/resources/campaigndraft_schedule.rb +1 -3
  21. data/lib/mailjet/resources/campaigndraft_send.rb +0 -2
  22. data/lib/mailjet/resources/campaigndraft_status.rb +0 -2
  23. data/lib/mailjet/resources/campaigndraft_test.rb +0 -2
  24. data/lib/mailjet/resources/campaigngraphstatistics.rb +1 -3
  25. data/lib/mailjet/resources/campaignoverview.rb +1 -3
  26. data/lib/mailjet/resources/campaignstatistics.rb +1 -3
  27. data/lib/mailjet/resources/clickstatistics.rb +1 -3
  28. data/lib/mailjet/resources/contact.rb +1 -3
  29. data/lib/mailjet/resources/contact_getcontactslists.rb +1 -3
  30. data/lib/mailjet/resources/contact_managecontactslists.rb +1 -3
  31. data/lib/mailjet/resources/contact_managemanycontacts.rb +1 -3
  32. data/lib/mailjet/resources/contactdata.rb +1 -3
  33. data/lib/mailjet/resources/contactfilter.rb +1 -3
  34. data/lib/mailjet/resources/contacthistorydata.rb +1 -3
  35. data/lib/mailjet/resources/contactmetadata.rb +1 -3
  36. data/lib/mailjet/resources/contactslist.rb +1 -3
  37. data/lib/mailjet/resources/contactslist_managecontact.rb +1 -3
  38. data/lib/mailjet/resources/contactslist_managemanycontacts.rb +1 -3
  39. data/lib/mailjet/resources/contactslistsignup.rb +1 -3
  40. data/lib/mailjet/resources/contactstatistics.rb +1 -3
  41. data/lib/mailjet/resources/csvimport.rb +1 -3
  42. data/lib/mailjet/resources/dns.rb +2 -4
  43. data/lib/mailjet/resources/dns_check.rb +2 -4
  44. data/lib/mailjet/resources/domainstatistics.rb +1 -3
  45. data/lib/mailjet/resources/eventcallbackurl.rb +1 -3
  46. data/lib/mailjet/resources/geostatistics.rb +1 -3
  47. data/lib/mailjet/resources/graphstatistics.rb +1 -3
  48. data/lib/mailjet/resources/listrecipient.rb +1 -3
  49. data/lib/mailjet/resources/listrecipientstatistics.rb +1 -3
  50. data/lib/mailjet/resources/liststatistics.rb +1 -3
  51. data/lib/mailjet/resources/manycontacts.rb +1 -3
  52. data/lib/mailjet/resources/message.rb +1 -3
  53. data/lib/mailjet/resources/messagehistory.rb +1 -3
  54. data/lib/mailjet/resources/messageinformation.rb +1 -3
  55. data/lib/mailjet/resources/messagesentstatistics.rb +1 -3
  56. data/lib/mailjet/resources/messagestate.rb +1 -3
  57. data/lib/mailjet/resources/messagestatistics.rb +1 -3
  58. data/lib/mailjet/resources/metadata.rb +1 -3
  59. data/lib/mailjet/resources/metasender.rb +1 -3
  60. data/lib/mailjet/resources/myprofile.rb +1 -3
  61. data/lib/mailjet/resources/newsletter.rb +1 -3
  62. data/lib/mailjet/resources/newsletter_detailcontent.rb +1 -3
  63. data/lib/mailjet/resources/newsletter_schedule.rb +2 -4
  64. data/lib/mailjet/resources/newsletter_send.rb +1 -3
  65. data/lib/mailjet/resources/newsletter_status.rb +1 -3
  66. data/lib/mailjet/resources/newsletter_test.rb +1 -3
  67. data/lib/mailjet/resources/newsletterblock.rb +1 -3
  68. data/lib/mailjet/resources/newsletterproperties.rb +1 -3
  69. data/lib/mailjet/resources/newslettertemplate.rb +1 -3
  70. data/lib/mailjet/resources/newslettertemplateblock.rb +1 -3
  71. data/lib/mailjet/resources/newslettertemplatecategory.rb +1 -3
  72. data/lib/mailjet/resources/newslettertemplateproperties.rb +1 -3
  73. data/lib/mailjet/resources/openinformation.rb +1 -3
  74. data/lib/mailjet/resources/openstatistics.rb +1 -3
  75. data/lib/mailjet/resources/parseroute.rb +1 -3
  76. data/lib/mailjet/resources/preferences.rb +1 -3
  77. data/lib/mailjet/resources/send.rb +2 -3
  78. data/lib/mailjet/resources/sender.rb +1 -3
  79. data/lib/mailjet/resources/sender_validate.rb +1 -4
  80. data/lib/mailjet/resources/senderstatistics.rb +1 -3
  81. data/lib/mailjet/resources/template.rb +1 -3
  82. data/lib/mailjet/resources/template_detailcontent.rb +1 -4
  83. data/lib/mailjet/resources/toplinkclicked.rb +1 -3
  84. data/lib/mailjet/resources/trigger.rb +1 -3
  85. data/lib/mailjet/resources/user.rb +1 -3
  86. data/lib/mailjet/resources/useragentstatistics.rb +1 -3
  87. data/lib/mailjet/resources/widget.rb +1 -3
  88. data/lib/mailjet/resources/widgetcustomvalue.rb +1 -3
  89. data/lib/mailjet/version.rb +1 -1
  90. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 128c8f6b78690ae7cb6513b72a6b077f55225f68
4
- data.tar.gz: d42aba90bc50df72366976e15e779f51ff229aa6
3
+ metadata.gz: 97d0fd939a2c9ce129ce2dc69ceacfa1e74927b0
4
+ data.tar.gz: 6fe2384a9d5142a6641e6246fa4cd78456464efe
5
5
  SHA512:
6
- metadata.gz: ddf2f855a2744c08151ee3b41c491ba1fff143fe1e26023be86cf328cb7e5a4b24053f7654c07e4a2884f8738c42fb0940f2e935993e4547eecf83e683c91c1f
7
- data.tar.gz: c520b425d3b886db0351478a9a80631cc99efcab907dcac1d92ed51421d4d3439effdaf9e58e7d90cf1dc718170e32459ba06282ec4846beec0ab5eeb233354b
6
+ metadata.gz: 915c0bcff51e94b47fffb16699326a765fb186a850f0763a0684c35c3def3288a87df4a859d885bf2d7cd5f187285f09aa552dd1e2af87ca0e8a95d018c887d5
7
+ data.tar.gz: 219fd219da5b8bdf8d3dae8931e21f3d4c600e71e2240d76e1bb02b100897646d05a8eaa85fac7e38a30923011112cf416c4028a49e7bf0419e6de5e855159af
data/README.md CHANGED
@@ -9,6 +9,7 @@
9
9
  [![Build Status](https://secure.travis-ci.org/jbescoyez/mailjet.png?branch=master)][travis]
10
10
  [![Dependency Status](https://gemnasium.com/jbescoyez/mailjet.png)][gemnasium]
11
11
  [![Maintainance status](http://stillmaintained.com/jbescoyez/mailjet.png)][stillmaintained]
12
+ ![Current Version](https://img.shields.io/badge/version-1.5.0-green.svg)
12
13
 
13
14
  -->
14
15
 
@@ -42,8 +43,7 @@ This gem helps you to:
42
43
 
43
44
  Compatibility:
44
45
 
45
- - Ruby 1.9.X
46
- - Ruby 2.X.X
46
+ - Ruby 2.2.X
47
47
 
48
48
  Rails ActionMailer integration designed for Rails 3.X and 4.X
49
49
 
@@ -101,15 +101,13 @@ Mailjet.configure do |config|
101
101
  end
102
102
  ```
103
103
 
104
-
105
104
  `default_from` is optional if you send emails with `:mailjet`'s SMTP (below)
106
105
 
107
- ### Send emails via the SendAPI
106
+ ### Send emails via the Send API
108
107
 
109
- Find more about the Mailjet SendAPI in the [official guides][send-api-doc]
108
+ Find more about the Mailjet Send API in the [official guides](http://dev.mailjet.com/guides/?ruby#choose-sending-method)
110
109
 
111
110
  ``` ruby
112
-
113
111
  email = { :from_email => "your email",
114
112
  :from_name => "Your name",
115
113
  :subject => "Hello",
@@ -120,7 +118,6 @@ test = Mailjet::Send.create(email)
120
118
 
121
119
  # retrieve the API response
122
120
  p test.attributes['Sent']
123
-
124
121
  ```
125
122
 
126
123
  ### Send emails with ActionMailer
@@ -139,10 +136,25 @@ Or if you prefer sending messages through [Mailjet Send API](http://dev.mailjet.
139
136
  config.action_mailer.delivery_method = :mailjet_api
140
137
  ```
141
138
 
142
- You can use mailjet specific options with `delivery_method_options` as detailed in the official [ActionMailer doc][http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-dynamic-delivery-options].
139
+ You can use mailjet specific options with `delivery_method_options` as detailed in the official [ActionMailer doc](http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-dynamic-delivery-options):
140
+
141
+ ```ruby
142
+ class AwesomeMailer < ApplicationMailer
143
+
144
+ def awesome_mail(user)
145
+
146
+ mail(
147
+ to: user.email,
148
+ delivery_method_options: { api_key: 'your-api-key', secret_key: 'your-secret-key' }
149
+ )
150
+ end
151
+ end
152
+ ```
143
153
 
144
154
  Supported options are:
145
155
  ```ruby
156
+ * :api_key
157
+ * :secret_key
146
158
  * :'mj-prio'
147
159
  * :'mj-campaign'
148
160
  * :'mj-deduplicatecampaign'
@@ -369,8 +381,6 @@ In these modifiers, there is now the ability to add a Mailjet custom-id or Mailj
369
381
 
370
382
  For more information on custom properties and available params, see the [official doc][send-api-doc].
371
383
 
372
- ##
373
-
374
384
  ## Track email delivery
375
385
 
376
386
  You can setup your Rack application in order to receive feedback on emails you sent (clicks, etc.)
@@ -4,11 +4,16 @@ module Mailjet
4
4
  module Configuration
5
5
  mattr_accessor :api_key
6
6
  mattr_accessor :secret_key
7
- mattr_accessor :end_point
8
7
  mattr_accessor :default_from
9
-
10
- # @@end_point = 'https://api.preprod.mailjet.com'
11
- @@end_point = 'https://api.mailjet.com'
8
+ mattr_accessor :api_version do
9
+ 'v3'
10
+ end
11
+ mattr_accessor :end_point do
12
+ 'https://api.mailjet.com'
13
+ end
14
+ mattr_accessor :perform_api_call do
15
+ true
16
+ end
12
17
 
13
18
  end
14
19
  end
@@ -39,32 +39,34 @@ module Mailjet
39
39
  self.adapter = adapter_class.new(end_point, options.merge(user: api_key, password: secret_key, content_type: 'application/json'))
40
40
  end
41
41
 
42
- def get(additional_headers = {}, &block)
43
- handle_api_call(:get, additional_headers, &block)
42
+ def get(additional_headers = {}, perform_api_call, &block)
43
+ handle_api_call(:get, additional_headers, perform_api_call, &block)
44
44
  end
45
45
 
46
- def post(payload, additional_headers = {}, &block)
47
- handle_api_call(:post, additional_headers, payload, &block)
46
+ def post(payload, additional_headers = {}, perform_api_call, &block)
47
+ handle_api_call(:post, additional_headers, payload, perform_api_call, &block)
48
48
  end
49
49
 
50
- def put(payload, additional_headers = {}, &block)
51
- handle_api_call(:put, additional_headers, payload, &block)
50
+ def put(payload, additional_headers = {}, perform_api_call, &block)
51
+ handle_api_call(:put, additional_headers, payload, perform_api_call, &block)
52
52
  end
53
53
 
54
- def delete(additional_headers = {}, &block)
55
- handle_api_call(:delete, additional_headers, &block)
54
+ def delete(additional_headers = {}, perform_api_call, &block)
55
+ handle_api_call(:delete, additional_headers, perform_api_call, &block)
56
56
  end
57
57
 
58
58
  private
59
59
 
60
- def handle_api_call(method, additional_headers = {}, payload = {}, &block)
60
+ def handle_api_call(method, additional_headers = {}, payload = {}, perform_api_call, &block)
61
61
  formatted_payload = (additional_headers[:content_type] == :json) ? payload.to_json : payload
62
62
  raise Mailjet::MethodNotAllowed unless method_allowed(method)
63
63
 
64
- if [:get, :delete].include?(method)
65
- @adapter.send(method, additional_headers, &block)
66
- else
67
- @adapter.send(method, formatted_payload, additional_headers, &block)
64
+ if perform_api_call
65
+ if [:get, :delete].include?(method)
66
+ @adapter.send(method, additional_headers, &block)
67
+ else
68
+ @adapter.send(method, formatted_payload, additional_headers, &block)
69
+ end
68
70
  end
69
71
  rescue RestClient::Exception => e
70
72
  handle_exception(e, additional_headers, formatted_payload)
@@ -12,8 +12,8 @@ class Mailjet::Mailer < ::Mail::SMTP
12
12
  address: 'in-v3.mailjet.com',
13
13
  port: 587,
14
14
  authentication: 'plain',
15
- user_name: Mailjet.config.api_key,
16
- password: Mailjet.config.secret_key,
15
+ user_name: options.delete(:api_key) || Mailjet.config.api_key,
16
+ password: options.delete(:secret_key) || Mailjet.config.secret_key,
17
17
  enable_starttls_auto: true
18
18
  }.merge(options))
19
19
  end
@@ -27,27 +27,178 @@ ActionMailer::Base.add_delivery_method :mailjet, Mailjet::Mailer
27
27
  # The deliver methods maps the Mail::Message attributes to the MailjetSend API JSON expected structure
28
28
  class Mailjet::APIMailer
29
29
  def initialize(options = {})
30
- @delivery_method_options = options.slice(
30
+ # send = Mailjet.Send.new
31
+ # if send.version.exists
32
+ # version = send.version
33
+ # else
34
+ @version = Mailjet.config.api_version
35
+ # end
36
+ @delivery_method_options_v3_0 = options.slice(
37
+ :api_key, :secret_key,
31
38
  :recipients, :'mj-prio', :'mj-campaign', :'mj-deduplicatecampaign',
32
39
  :'mj-templatelanguage', :'mj-templateerrorreporting', :'mj-templateerrordeliver', :'mj-templateid',
33
40
  :'mj-trackopen', :'mj-trackclick',
34
- :'mj-customid', :'mj-eventpayload', :vars, :headers
41
+ :'mj-customid', :'mj-eventpayload', :vars, :headers,
42
+ )
43
+ @delivery_method_options_v3_1 = options.slice(
44
+ :api_key, :secret_key,
45
+ :'Priority', :'CustomCampaign', :'DeduplicateCampaign',
46
+ :'TemplateLanguage', :'TemplateErrorReporting', :'TemplateErrorDeliver', :'TemplateID',
47
+ :'TrackOpens', :'TrackClicks',
48
+ :'CustomID', :'EventPayload', :'Variables', :'Headers',
35
49
  )
36
50
  end
37
51
 
38
- def deliver!(mail)
52
+ def deliver!(mail, options = nil)
53
+ # p mail.header.fields
54
+
55
+ if (options && options.kind_of?(Object) && options['version'].present?)
56
+ @version = options['version']
57
+ end
58
+
59
+ if (!options.kind_of?(Object))
60
+ options = []
61
+ end
62
+
63
+ # Mailjet Send API does not support full from. Splitting the from field into two: name and email address
64
+ if mail[:from].nil? && Mailjet.config.default_from.present?
65
+ mail[:from] = Mailjet.config.default_from
66
+ end
67
+
68
+ if (@version == 'v3.1')
69
+ Mailjet::Send.create({:Messages => [setContentV3_1(mail)]})
70
+ else
71
+ Mailjet::Send.create(setContentV3_0(mail))
72
+ end
73
+ end
74
+
75
+ def setContentV3_1(mail)
39
76
  content = {}
77
+ content[:TextPart] = mail.text_part.try(:decoded) if !mail.text_part.blank?
78
+ content[:HTMLPart] = mail.html_part.try(:decoded) if !mail.html_part.blank?
79
+
80
+ if mail.attachments.any?
81
+ content[:Attachments] = []
82
+ content[:InlineAttachments] = []
83
+
84
+ mail.attachments.each do |attachment|
85
+ mailjet_attachment = {
86
+ 'ContentType' => attachment.content_type.split(';')[0],
87
+ 'Filename' => attachment.filename,
88
+ 'Base64Content' => Base64.encode64(attachment.body.decoded)
89
+ }
40
90
 
41
- if mail.text_part
42
- content[:text_part] = mail.text_part.try(:decoded)
43
- content[:text] = mail.text_part.try(:decoded)
91
+ if attachment.inline?
92
+ mailjet_attachment['ContentId'] = attachment.content_id
93
+ content[:InlineAttachments].push(mailjet_attachment)
94
+ else
95
+ content[:Attachments].push(mailjet_attachment)
96
+ end
97
+ end
44
98
  end
45
99
 
46
- if mail.html_part
47
- content[:html_part] = mail.html_part.try(:decoded)
48
- content[:html] = mail.html_part.try(:decoded)
100
+ # We do the next part in purpose to accept only custom header from the user, but without accepting the mailjet related ones
101
+ # The mailjet related ones are stocked in another variable and directly added to the body
102
+ if mail.header && mail.header.fields.any?
103
+ content[:Headers] = {}
104
+ mail.header.fields.each do |header|
105
+ if header.name.start_with?('X-') && !header.name.start_with?('X-MJ') && !header.name.start_with?('X-Mailjet')
106
+ content[:Headers][header.name] = header.value
107
+ end
108
+ end
109
+ end
110
+
111
+ # Reply-To is not a property in Mailjet Send API
112
+ # Passing it as an header if mail.reply_to
113
+
114
+ if mail.reply_to
115
+ if mail.reply_to.display_names.first
116
+ content[:Headers]['Reply-To'] = {:Email=> mail[:reply_to].addresses.first, :Name=> mail[:reply_to].display_names.first}
117
+ else
118
+ content[:Headers]['Reply-To'] = {:Email=> mail[:reply_to].addresses.first}
119
+ end
49
120
  end
50
121
 
122
+ if (mail[:to])
123
+ if (mail[:to].is_a? String)
124
+ if mail[:to].display_names.first
125
+ to = [{:Email=>mail[:to].addresses.first, :Name=>mail[:to].display_names.first}]
126
+ else
127
+ to = [{:Email=>mail[:to].addresses.first}]
128
+ end
129
+ else
130
+ to = []
131
+ mail[:to].each do |t|
132
+ if (t.display_name)
133
+ to << { :Email => t.address, :Name => t.display_name }
134
+ else
135
+ to << { :Email => t.address }
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ base_from = {
142
+ From:{
143
+ :Email=> mail[:from].addresses.first
144
+ }
145
+ }
146
+ if (mail[:from].display_names.first)
147
+ base_from[:From][:Name] = mail[:from].display_names.first
148
+ end
149
+
150
+ if (mail[:cc])
151
+ if (mail[:cc].is_a? String)
152
+ if mail[:cc].display_name.first
153
+ ccs =[{:Email=>mail[:cc].address.first, :Name=>mail[:cc].display_name.first}]
154
+ else
155
+ ccs =[{:Email=>mail[:cc].address.first}]
156
+ end
157
+ else
158
+ mail[:cc].each do |cc|
159
+ ccs << {:Email=> cc.address, :Name=>cc.display_name}
160
+ end
161
+ end
162
+ end
163
+
164
+ if (mail[:bcc])
165
+ if (mail[:bcc].formatted.is_a? String)
166
+ if mail[:bcc].display_name.first
167
+ payload[:Bcc] = [{:Email=>mail[:bcc].address.first, :Name=>mail[:bcc].display_name.first}]
168
+ else
169
+ payload[:Bcc] = [{:Email=>mail[:bcc].address.first}]
170
+ end
171
+ else
172
+ mail[:bcc].formatted.each do |bcc|
173
+ if bcc.display_name
174
+ bccs << {:Email=> bcc.address, :Name=>bcc.display_name}
175
+ else
176
+ bccs << {:Email=> bcc.address}
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ payload = {
183
+ :To=> to,
184
+ }.merge(content)
185
+ .merge(base_from)
186
+ .merge(@delivery_method_options_v3_1)
187
+
188
+ payload[:Subject] = mail.subject if !mail.subject.blank?
189
+ payload[:Sender] = mail[:sender] if !mail[:sender].blank?
190
+ payload[:Cc] = ccs if mail[:cc]
191
+ payload[:Bcc] = bccs if mail[:bcc]
192
+
193
+ payload
194
+ end
195
+
196
+ def setContentV3_0(mail)
197
+ content = {}
198
+
199
+ content[:text_part] = mail.text_part.try(:decoded) if !mail.text_part.blank?
200
+ content[:html_part] = mail.html_part.try(:decoded) if !mail.html_part.blank?
201
+
51
202
  # Formatting attachments (inline + regular)
52
203
  unless mail.attachments.empty?
53
204
  content[:attachments] = []
@@ -68,12 +219,12 @@ class Mailjet::APIMailer
68
219
  end
69
220
  end
70
221
 
222
+ # We do the next part in purpose to accept only custom header from the user, but without accepting the mailjet related ones
223
+ # The mailjet related ones are stocked in another variable and directly added to the body
71
224
  if mail.header && !mail.header.fields.empty?
72
225
  content[:headers] = {}
73
226
  mail.header.fields.each do |header|
74
- if header.name.start_with?('X-MJ') || header.name.start_with?('X-Mailjet')
75
- content[:headers][header.name] = header.value
76
- end
227
+ content[:headers][header.name] = header.value if header.name =~ /^X-(?!\b(MJ|Mailjet)\b).*$/
77
228
  end
78
229
  end
79
230
 
@@ -82,9 +233,7 @@ class Mailjet::APIMailer
82
233
  content[:headers]['Reply-To'] = mail.reply_to.join(', ') if mail.reply_to
83
234
 
84
235
  # Mailjet Send API does not support full from. Splitting the from field into two: name and email address
85
- if mail[:from].nil? && Mailjet.config.default_from.present?
86
- mail[:from] = Mailjet.config.default_from
87
- end
236
+ mail[:from] ||= Mailjet.config.default_from
88
237
 
89
238
  base_from = { from_name: mail[:from].display_names.first,
90
239
  from_email: mail[:from].addresses.first }
@@ -94,17 +243,16 @@ class Mailjet::APIMailer
94
243
  sender: mail[:sender],
95
244
  subject: mail.subject
96
245
  }
97
- .merge(content)
98
- .merge(base_from)
99
- .merge(@delivery_method_options)
100
246
 
101
247
  payload[:cc] = mail[:cc].formatted.join(', ') if mail[:cc]
102
248
  payload[:reply_to] = mail[:reply_to].formatted.join(', ') if mail[:reply_to]
103
249
  payload[:bcc] = mail[:bcc].formatted.join(', ') if mail[:bcc]
104
250
 
105
251
  # Send the final payload to Mailjet Send API
106
- Mailjet::Send.create(payload)
107
- end
252
+ payload.merge(content)
253
+ .merge(base_from)
254
+ .merge(@delivery_method_options_v3_0)
255
+ end
108
256
  end
109
257
 
110
258
  ActionMailer::Base.add_delivery_method :mailjet_api, Mailjet::APIMailer
@@ -1,9 +1,8 @@
1
- require 'mailjet/resource'
2
-
3
1
  module Mailjet
4
2
  class MessageDelivery
5
3
  include Mailjet::Resource
6
- self.resource_path = 'v3/send/message'
4
+ # self.version = 'v3.1/'
5
+ self.resource_path = 'send/message'
7
6
  self.public_operations = [:post]
8
7
  self.resourceprop = [:from, :sender, :to, :cc, :bcc, :subject, :text, :html, :attachment, :inline_attachments, :headers]
9
8
  end
@@ -1,5 +1,4 @@
1
1
  require 'mailjet/connection'
2
- require 'mailjet/resource'
3
2
  require 'active_support/hash_with_indifferent_access'
4
3
  require 'active_support/core_ext/class'
5
4
  require 'active_support/core_ext/hash'
@@ -7,7 +6,7 @@ require 'active_support/core_ext/string'
7
6
  require 'active_support/core_ext/module/delegation'
8
7
  require 'active_support/concern'
9
8
  require 'active_support/json/decoding'
10
- require 'mail'
9
+ #require 'mail'
11
10
  require 'json'
12
11
 
13
12
 
@@ -21,27 +20,28 @@ module Mailjet
21
20
  module Resource
22
21
  extend ActiveSupport::Concern
23
22
 
23
+ NON_JSON_URLS = ['v3/send/message'] # urls that don't accept JSON input
24
+
24
25
  included do
25
- cattr_accessor :resource_path, :public_operations, :read_only, :filters, :resourceprop, :action, :non_json_urls
26
+ cattr_accessor :resource_path, :public_operations, :read_only, :filters, :resourceprop, :action, :non_json_urls, :version
26
27
  cattr_writer :connection
27
28
 
28
- def self.connection
29
- @non_json_urls = ["v3/send/message"] #urls that don't accept JSON input
30
- class_variable_get(:@@connection) || default_connection
29
+ def self.connection(options = {})
30
+ class_variable_get(:@@connection) || default_connection(options)
31
31
  end
32
32
 
33
- def self.default_connection
33
+ def self.default_connection(options = {})
34
34
  Mailjet::Connection.new(
35
- "#{Mailjet.config.end_point}/#{resource_path}",
36
- Mailjet.config.api_key,
37
- Mailjet.config.secret_key,
35
+ "#{options[:url]}/#{options[:version]}/#{resource_path}",
36
+ options[:api_key] || Mailjet.config.api_key,
37
+ options[:secret_key] || Mailjet.config.secret_key,
38
38
  public_operations: public_operations,
39
39
  read_only: read_only)
40
40
  end
41
41
 
42
42
  def self.default_headers
43
- if @non_json_urls.include?(self.resource_path)#don't use JSON if Send API
44
- default_headers = { accept: :json, accept_encoding: :deflate }
43
+ if NON_JSON_URLS.include?(self.resource_path) # don't use JSON if Send API
44
+ default_headers = { accept: :json, accept_encoding: :deflate }
45
45
  else
46
46
  default_headers = { accept: :json, accept_encoding: :deflate, content_type: :json } #use JSON if *not* Send API
47
47
  end
@@ -50,28 +50,31 @@ module Mailjet
50
50
  end
51
51
 
52
52
  module ClassMethods
53
- def first(params = {})
54
- all(params.merge!(limit: 1)).first
53
+ def first(params = {}, options = {})
54
+ all(params.merge!(limit: 1), options).first
55
55
  end
56
56
 
57
- def all(params = {})
57
+ def all(params = {}, options = {})
58
+ opts = change_resource_path(options)
58
59
  params = format_params(params)
59
- response = connection.get(default_headers.merge(params: params))
60
+ response = connection(opts).get(default_headers.merge(params: params), opts[:perform_api_call])
60
61
  attribute_array = parse_api_json(response)
61
62
  attribute_array.map{ |attributes| instanciate_from_api(attributes) }
62
63
  end
63
64
 
64
- def count
65
- response_json = connection.get(default_headers.merge(params: {limit: 1, countrecords: 1}))
65
+ def count(options = {})
66
+ opts = change_resource_path(options)
67
+ response_json = connection(opts).get(default_headers.merge(params: {limit: 1, countrecords: 1}), opts[:perform_api_call])
66
68
  response_hash = ActiveSupport::JSON.decode(response_json)
67
69
  response_hash['Total']
68
70
  end
69
71
 
70
- def find(id, job_id = nil)
72
+ def find(id, job_id = nil, options = {})
71
73
  # if action method, ammend url to appropriate id
74
+ opts = change_resource_path(options)
72
75
  self.resource_path = create_action_resource_path(id, job_id) if self.action
73
76
  #
74
- attributes = parse_api_json(connection[id].get(default_headers)).first
77
+ attributes = parse_api_json(connection(opts)[id].get(default_headers, opts[:perform_api_call])).first
75
78
  instanciate_from_api(attributes)
76
79
 
77
80
  rescue Mailjet::ApiError => e
@@ -82,14 +85,15 @@ module Mailjet
82
85
  end
83
86
  end
84
87
 
85
- def create(attributes = {})
88
+ def create(attributes = {}, options = {})
86
89
  # if action method, ammend url to appropriate id
90
+ opts = change_resource_path(options)
87
91
  self.resource_path = create_action_resource_path(attributes[:id]) if self.action
88
92
  attributes.tap { |hs| hs.delete(:id) }
89
93
 
90
- if Mailjet.config.default_from and self.resource_path == 'v3/send/'
91
- address = Mail::AddressList.new(Mailjet.config.default_from).addresses[0]
92
- default_attributes = { :from_email => address.address, :from_name => address.display_name}
94
+ if Mailjet.config.default_from and self.resource_path == 'send/'
95
+ address = Mail::AddressList.new(Mailjet.config.default_from).addresses[0]
96
+ default_attributes = { :from_email => address.address, :from_name => address.display_name}
93
97
  else
94
98
  default_attributes = {}
95
99
  end
@@ -97,17 +101,17 @@ module Mailjet
97
101
  attributes = default_attributes.merge(attributes)
98
102
 
99
103
  self.new(attributes).tap do |resource|
100
- resource.save!
104
+ resource.save!(opts)
101
105
  resource.persisted = true
102
106
  end
103
107
 
104
108
  end
105
109
 
106
- def delete(id)
110
+ def delete(id, options = {})
107
111
  # if action method, ammend url to appropriate id
112
+ opts = change_resource_path(options)
108
113
  self.resource_path = create_action_resource_path(id) if self.action
109
- #
110
- connection[id].delete(default_headers)
114
+ connection(options)[id].delete(default_headers)
111
115
  end
112
116
 
113
117
  def instanciate_from_api(attributes = {})
@@ -131,11 +135,14 @@ module Mailjet
131
135
  def create_action_resource_path(id, job_id = nil)
132
136
  url_elements = self.resource_path.split("/")
133
137
  url_elements.delete_at(url_elements.length-1) if url_elements.last.to_i > 0 #if there is a trailing number for the job id from last call, delete it
134
- if self.action != "managemanycontacts" || (self.action == "managemanycontacts" && url_elements[2] == "contactslist")
135
- url_elements[3] = id.to_s
136
- end
137
- url_elements << job_id.to_s if job_id #if job_id exists, ammend it to end of the URI
138
+
139
+ if !(url_elements[1] == "contacts" && self.action == "managemanycontacts")
140
+ url_elements[2] = id.to_s
141
+ end
142
+
143
+ url_elements << job_id.to_s if job_id #if job_id exists, amend it to end of the URI
138
144
  url = url_elements.join("/")
145
+
139
146
  return url
140
147
  end
141
148
 
@@ -201,6 +208,29 @@ module Mailjet
201
208
  _hash
202
209
  end
203
210
  end
211
+
212
+ def change_resource_path(options = {})
213
+ ver = choose_version(options)
214
+ url = Mailjet.config.end_point
215
+ perform_api_call = Mailjet.config.perform_api_call
216
+ if options.any?
217
+ if options.key?(:perform_api_call)
218
+ perform_api_call = options[:perform_api_call]
219
+ end
220
+ if options.key?(:url)
221
+ url = options['url']
222
+ end
223
+ end
224
+ ret = {version: ver, url: url, perform_api_call: perform_api_call}
225
+ ret
226
+ end
227
+
228
+ def choose_version(options = {})
229
+ ver = options['version'] || Mailjet.config.api_version || version
230
+
231
+ ver
232
+ end
233
+
204
234
  end
205
235
 
206
236
  attr_accessor :attributes, :persisted
@@ -213,20 +243,24 @@ module Mailjet
213
243
  attributes[:persisted]
214
244
  end
215
245
 
216
- def save
246
+ def save(options)
217
247
  if persisted?
218
- response = connection[attributes[:id]].put(formatted_payload, default_headers)
248
+ response = connection(options)[attributes[:id]].put(formatted_payload, default_headers, options[:perform_api_call])
219
249
  else
220
- response = connection.post(formatted_payload, default_headers)
250
+ response = connection(options).post(formatted_payload, default_headers, options[:perform_api_call])
221
251
  end
222
252
 
223
- if self.resource_path == 'v3/send/'
224
- self.attributes = ActiveSupport::JSON.decode(response)
253
+ if options[:perform_api_call]
254
+ if self.resource_path == 'send/'
255
+ self.attributes = ActiveSupport::JSON.decode(response)
256
+ return true
257
+ end
258
+
259
+ self.attributes = parse_api_json(response).first
260
+ return true
261
+ else
225
262
  return true
226
263
  end
227
-
228
- self.attributes = parse_api_json(response).first
229
- return true
230
264
  rescue Mailjet::ApiError => e
231
265
  if e.code.to_s == "304"
232
266
  return true # When you save a record twice it should not raise error
@@ -235,8 +269,8 @@ module Mailjet
235
269
  end
236
270
  end
237
271
 
238
- def save!
239
- save || raise(StandardError.new("Resource not persisted"))
272
+ def save!(options)
273
+ save(options) || raise(StandardError.new("Resource not persisted"))
240
274
  end
241
275
 
242
276
  def attributes=(attribute_hash = {})
@@ -245,19 +279,19 @@ module Mailjet
245
279
  end
246
280
  end
247
281
 
248
- def update_attributes(attribute_hash = {})
282
+ def update_attributes(attribute_hash = {}, options = {})
249
283
  self.attributes = attribute_hash
250
- save
284
+ save(options)
251
285
  end
252
286
 
253
- def delete
287
+ def delete(call)
254
288
  self.class.delete(id)
255
289
  end
256
290
 
257
291
  private
258
292
 
259
- def connection
260
- self.class.connection
293
+ def connection(options)
294
+ self.class.connection(options)
261
295
  end
262
296
 
263
297
  def default_headers
@@ -309,5 +343,6 @@ module Mailjet
309
343
  super
310
344
  end
311
345
  end
346
+
312
347
  end
313
348
  end