mailjet 1.5.3 → 1.7.0
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.
- checksums.yaml +5 -5
- data/README.md +301 -190
- data/Rakefile +23 -14
- data/lib/generators/mailjet/initializer_generator.rb +35 -0
- data/lib/generators/mailjet/templates/mailjet.rb.erb +8 -0
- data/lib/mailjet/api_error.rb +19 -20
- data/lib/mailjet/configuration.rb +56 -4
- data/lib/mailjet/connection.rb +23 -6
- data/lib/mailjet/mailer.rb +75 -41
- data/lib/mailjet/rack/endpoint.rb +2 -3
- data/lib/mailjet/resource.rb +80 -85
- data/lib/mailjet/resources/campaigndraft.rb +1 -1
- data/lib/mailjet/resources/campaigndraft_detailcontent.rb +17 -1
- data/lib/mailjet/resources/campaigndraft_schedule.rb +1 -1
- data/lib/mailjet/resources/campaigndraft_send.rb +1 -1
- data/lib/mailjet/resources/campaigndraft_status.rb +1 -1
- data/lib/mailjet/resources/campaigndraft_test.rb +1 -1
- data/lib/mailjet/resources/contact_getcontactslists.rb +17 -0
- data/lib/mailjet/resources/messagehistory.rb +16 -0
- data/lib/mailjet/resources/messageinformation.rb +17 -0
- data/lib/mailjet/resources/newsletter.rb +1 -2
- data/lib/mailjet/resources/openinformation.rb +17 -0
- data/lib/mailjet/resources/send.rb +1 -1
- data/lib/mailjet/resources/statcounters.rb +33 -0
- data/lib/mailjet/resources/template_detailcontent.rb +18 -2
- data/lib/mailjet/version.rb +1 -1
- data/lib/mailjet.rb +2 -2
- metadata +20 -120
@@ -0,0 +1,8 @@
|
|
1
|
+
# kindly generated by appropriated Rails generator
|
2
|
+
Mailjet.configure do |config|
|
3
|
+
config.api_key = '<%= @api_key %>'
|
4
|
+
config.secret_key = '<%= @secret_key %>'
|
5
|
+
config.default_from = '<%= @default_from %>'
|
6
|
+
<% if @api_v3_1 %><%= @api_v3_1_notice.split("\n").reject(&:empty?).map{ |l| ' # ' + l }.join("\n") %>
|
7
|
+
config.api_version = 'v3.1'<% end %>
|
8
|
+
end
|
data/lib/mailjet/api_error.rb
CHANGED
@@ -1,29 +1,28 @@
|
|
1
|
-
|
2
|
-
require 'active_support'
|
1
|
+
require "json"
|
3
2
|
|
4
3
|
module Mailjet
|
5
4
|
class ApiError < StandardError
|
5
|
+
attr_reader :code, :reason
|
6
6
|
|
7
|
-
|
7
|
+
# @param code [Integer] HTTP response status code
|
8
|
+
# @param body [String] JSON response body
|
9
|
+
# @param request [Object] any request object
|
10
|
+
# @param url [String] request URL
|
11
|
+
# @param params [Hash] request headers and parameters
|
12
|
+
def initialize(code, body, request, url, params)
|
13
|
+
@code = code
|
14
|
+
@reason = begin
|
15
|
+
resdec = JSON.parse(body)
|
16
|
+
resdec['ErrorMessage']
|
17
|
+
rescue JSON::ParserError
|
18
|
+
body
|
19
|
+
end
|
8
20
|
|
21
|
+
message = "error #{code} while sending #{request.inspect} to #{url} with #{params.inspect}"
|
22
|
+
error_details = body.inspect
|
23
|
+
hint = "Please see https://dev.mailjet.com/guides/#status-codes for more informations on error numbers."
|
9
24
|
|
10
|
-
|
11
|
-
self.code = code
|
12
|
-
self.reason = ""
|
13
|
-
unless res.blank?
|
14
|
-
resdec = ActiveSupport::JSON.decode(res)
|
15
|
-
self.reason = resdec['ErrorMessage']
|
16
|
-
end
|
17
|
-
# code is ugly, output is pretty
|
18
|
-
super("error #{code} while sending #{request.inspect} to #{request_path} with #{params.inspect}\n\n" +
|
19
|
-
if res['errors'].present?
|
20
|
-
[(res['errors'] || [])].flatten.map do |param, text|
|
21
|
-
[param, text].map(&:to_s).reject(&:blank?).join(': ')
|
22
|
-
end.join("\n")
|
23
|
-
else
|
24
|
-
res.inspect
|
25
|
-
end + "\n\nPlease see https://dev.mailjet.com/guides/#status-codes for more informations on error numbers.\n\n"
|
26
|
-
)
|
25
|
+
super("#{message}\n\n#{error_details}\n\n#{hint}\n\n")
|
27
26
|
end
|
28
27
|
end
|
29
28
|
end
|
@@ -1,17 +1,69 @@
|
|
1
|
-
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
-
|
3
1
|
module Mailjet
|
4
2
|
module Configuration
|
5
|
-
|
3
|
+
def self.api_key
|
4
|
+
@api_key
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.api_key=(api_key)
|
8
|
+
@api_key = api_key
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.secret_key
|
12
|
+
@secret_key
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.secret_key=(secret_key)
|
16
|
+
@secret_key = secret_key
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.default_from
|
20
|
+
@default_from
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.default_from=(default_from)
|
24
|
+
@default_from = default_from
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.api_version
|
28
|
+
@api_version
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.api_version=(api_version)
|
32
|
+
@api_version = api_version
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.sandbox_mode
|
36
|
+
@sandbox_mode
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.sandbox_mode=(sandbox_mode)
|
40
|
+
@sandbox_mode = sandbox_mode
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.end_point
|
44
|
+
@end_point
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.end_point=(end_point)
|
48
|
+
@end_point = end_point
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.perform_api_call
|
52
|
+
@perform_api_call
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.perform_api_call=(perform_api_call)
|
56
|
+
@perform_api_call = perform_api_call
|
57
|
+
end
|
6
58
|
|
7
59
|
DEFAULT = {
|
8
60
|
api_version: 'v3',
|
61
|
+
sandbox_mode: false,
|
9
62
|
end_point: 'https://api.mailjet.com',
|
10
63
|
perform_api_call: true,
|
11
64
|
}
|
12
65
|
|
13
66
|
DEFAULT.each do |param, default_value|
|
14
|
-
mattr_accessor param
|
15
67
|
self.send("#{param}=", default_value)
|
16
68
|
end
|
17
69
|
end
|
data/lib/mailjet/connection.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
require 'rest_client'
|
2
2
|
require 'mailjet/gem_extensions/rest_client'
|
3
|
-
require 'active_support/core_ext/module/delegation'
|
4
3
|
require 'json'
|
5
4
|
|
6
5
|
module Mailjet
|
7
6
|
class Connection
|
8
7
|
|
9
|
-
attr_accessor :adapter, :public_operations, :read_only, :perform_api_call
|
8
|
+
attr_accessor :adapter, :public_operations, :read_only, :perform_api_call, :read_timeout, :open_timeout
|
10
9
|
alias :read_only? :read_only
|
11
10
|
|
12
|
-
delegate :options, :concat_urls, :url, to: :adapter
|
13
|
-
|
14
11
|
def [](suburl, &new_block)
|
15
12
|
broken_url = url.split("/")
|
16
13
|
if broken_url.include?("contactslist") && broken_url.include?("managemanycontacts") && broken_url.last.to_i > 0
|
@@ -35,8 +32,10 @@ module Mailjet
|
|
35
32
|
adapter_class = options[:adapter_class] || RestClient::Resource
|
36
33
|
self.public_operations = options[:public_operations] || []
|
37
34
|
self.read_only = options[:read_only]
|
35
|
+
self.read_timeout = options[:read_timeout]
|
36
|
+
self.open_timeout = options[:open_timeout]
|
38
37
|
# self.adapter = adapter_class.new(end_point, options.merge(user: api_key, password: secret_key, :verify_ssl => false, content_type: 'application/json'))
|
39
|
-
self.adapter = adapter_class.new(end_point, options.merge(user: api_key, password: secret_key, content_type: 'application/json'))
|
38
|
+
self.adapter = adapter_class.new(end_point, options.merge(user: api_key, password: secret_key, content_type: 'application/json', read_timeout: self.read_timeout, open_timeout: self.open_timeout))
|
40
39
|
self.perform_api_call = options.key?(:perform_api_call) ? options[:perform_api_call] : true
|
41
40
|
end
|
42
41
|
|
@@ -56,6 +55,18 @@ module Mailjet
|
|
56
55
|
handle_api_call(:delete, additional_headers, &block)
|
57
56
|
end
|
58
57
|
|
58
|
+
def options
|
59
|
+
self.adapter.options
|
60
|
+
end
|
61
|
+
|
62
|
+
def concat_urls(*options)
|
63
|
+
self.adapter.concat_urls(*options)
|
64
|
+
end
|
65
|
+
|
66
|
+
def url
|
67
|
+
self.adapter.url
|
68
|
+
end
|
69
|
+
|
59
70
|
private
|
60
71
|
|
61
72
|
def handle_api_call(method, additional_headers = {}, payload = {}, &block)
|
@@ -85,7 +96,13 @@ module Mailjet
|
|
85
96
|
formatted_payload = (additional_headers[:content_type] == :json) ? JSON.parse(payload) : payload
|
86
97
|
params = params.merge(formatted_payload)
|
87
98
|
|
88
|
-
|
99
|
+
http_body = if e.http_headers[:content_type] == "application/json"
|
100
|
+
e.http_body
|
101
|
+
else
|
102
|
+
"{}"
|
103
|
+
end
|
104
|
+
|
105
|
+
raise Mailjet::ApiError.new(e.http_code, http_body, @adapter, @adapter.url, params)
|
89
106
|
end
|
90
107
|
|
91
108
|
end
|
data/lib/mailjet/mailer.rb
CHANGED
@@ -26,46 +26,55 @@ ActionMailer::Base.add_delivery_method :mailjet, Mailjet::Mailer
|
|
26
26
|
# Mailjet sends API expects a JSON payload as the input.
|
27
27
|
# The deliver methods maps the Mail::Message attributes to the MailjetSend API JSON expected structure
|
28
28
|
class Mailjet::APIMailer
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
29
|
+
V3_0_PERMITTED_OPTIONS = [
|
30
|
+
:recipients, :'mj-prio', :'mj-campaign', :'mj-deduplicatecampaign',
|
31
|
+
:'mj-templatelanguage', :'mj-templateerrorreporting', :'mj-templateerrordeliver', :'mj-templateid',
|
32
|
+
:'mj-trackopen', :'mj-trackclick',
|
33
|
+
:'mj-customid', :'mj-eventpayload', :vars, :headers,
|
34
|
+
]
|
35
|
+
|
36
|
+
V3_1_PERMITTED_OPTIONS = [
|
37
|
+
:'Priority', :'CustomCampaign', :'DeduplicateCampaign',
|
38
|
+
:'TemplateLanguage', :'TemplateErrorReporting', :'TemplateErrorDeliver', :'TemplateID',
|
39
|
+
:'TrackOpens', :'TrackClicks',
|
40
|
+
:'CustomID', :'EventPayload', :'Variables', :'Headers',
|
41
|
+
]
|
42
|
+
|
43
|
+
CONNECTION_PERMITTED_OPTIONS = [:api_key, :secret_key]
|
44
|
+
|
45
|
+
HEADER_BLACKLIST = [
|
46
|
+
'from', 'sender', 'subject', 'to', 'cc', 'bcc', 'return-path', 'delivered-to', 'dkim-signature',
|
47
|
+
'domainkey-status', 'received-spf', 'authentication-results', 'received', 'user-agent', 'x-mailer',
|
48
|
+
'x-feedback-id', 'list-id', 'date', 'x-csa-complaints', 'message-id', 'reply-to', 'content-type',
|
49
|
+
'mime-version', 'content-transfer-encoding'
|
50
|
+
]
|
51
|
+
|
52
|
+
def initialize(opts = {})
|
53
|
+
options = HashWithIndifferentAccess.new(opts)
|
54
|
+
|
55
|
+
@version = options[:version]
|
56
|
+
@delivery_method_options_v3_0 = options.slice(*V3_0_PERMITTED_OPTIONS)
|
57
|
+
@delivery_method_options_v3_1 = options.slice(*V3_1_PERMITTED_OPTIONS)
|
58
|
+
@connection_options = options.slice(*CONNECTION_PERMITTED_OPTIONS)
|
50
59
|
end
|
51
60
|
|
52
|
-
def deliver!(mail,
|
61
|
+
def deliver!(mail, opts = {})
|
62
|
+
options = HashWithIndifferentAccess.new(opts)
|
53
63
|
|
54
|
-
|
55
|
-
|
56
|
-
end
|
64
|
+
# Mailjet Send API does not support full from. Splitting the from field into two: name and email address
|
65
|
+
mail[:from] ||= Mailjet.config.default_from if Mailjet.config.default_from
|
57
66
|
|
58
|
-
if (
|
59
|
-
|
60
|
-
end
|
67
|
+
# add `@connection_options` in `options` only if not exist yet (values in `options` prime)
|
68
|
+
options.reverse_merge!(@connection_options)
|
61
69
|
|
62
|
-
#
|
63
|
-
|
64
|
-
|
65
|
-
|
70
|
+
# add `@version` in options if set
|
71
|
+
options[:version] = @version if @version
|
72
|
+
|
73
|
+
# `options[:version]` primes on global config
|
74
|
+
version = options[:version] || Mailjet.config.api_version
|
66
75
|
|
67
|
-
if (
|
68
|
-
Mailjet::Send.create({:Messages => [setContentV3_1(mail)]}, options)
|
76
|
+
if (version == 'v3.1')
|
77
|
+
Mailjet::Send.create({ :Messages => [setContentV3_1(mail)], SandboxMode: Mailjet.config.sandbox_mode }, options)
|
69
78
|
else
|
70
79
|
Mailjet::Send.create(setContentV3_0(mail), options)
|
71
80
|
end
|
@@ -108,7 +117,7 @@ class Mailjet::APIMailer
|
|
108
117
|
if mail.header && mail.header.fields.any?
|
109
118
|
content[:Headers] = {}
|
110
119
|
mail.header.fields.each do |header|
|
111
|
-
if header.name.start_with?('X-') && !header.name.start_with?('X-
|
120
|
+
if !header.name.start_with?('X-MJ') && !header.name.start_with?('X-Mailjet') && !HEADER_BLACKLIST.include?(header.name.downcase)
|
112
121
|
content[:Headers][header.name] = header.value
|
113
122
|
end
|
114
123
|
end
|
@@ -117,8 +126,8 @@ class Mailjet::APIMailer
|
|
117
126
|
# ReplyTo property was added in v3.1
|
118
127
|
# Passing it as an header if mail.reply_to
|
119
128
|
|
120
|
-
if mail
|
121
|
-
if mail
|
129
|
+
if mail[:reply_to]
|
130
|
+
if mail[:reply_to].respond_to?(:display_names) && mail[:reply_to].display_names.first
|
122
131
|
content[:ReplyTo] = {:Email=> mail[:reply_to].addresses.first, :Name=> mail[:reply_to].display_names.first}
|
123
132
|
else
|
124
133
|
content[:ReplyTo] = {:Email=> mail[:reply_to].addresses.first}
|
@@ -161,8 +170,13 @@ class Mailjet::APIMailer
|
|
161
170
|
ccs =[{:Email=>mail[:cc].address.first}]
|
162
171
|
end
|
163
172
|
else
|
173
|
+
ccs = []
|
164
174
|
mail[:cc].each do |cc|
|
165
|
-
|
175
|
+
if cc.display_name
|
176
|
+
ccs << {:Email=> cc.address, :Name=>cc.display_name}
|
177
|
+
else
|
178
|
+
ccs << {:Email=> cc.address}
|
179
|
+
end
|
166
180
|
end
|
167
181
|
end
|
168
182
|
end
|
@@ -175,7 +189,8 @@ class Mailjet::APIMailer
|
|
175
189
|
payload[:Bcc] = [{:Email=>mail[:bcc].address.first}]
|
176
190
|
end
|
177
191
|
else
|
178
|
-
|
192
|
+
bccs = []
|
193
|
+
mail[:bcc].each do |bcc|
|
179
194
|
if bcc.display_name
|
180
195
|
bccs << {:Email=> bcc.address, :Name=>bcc.display_name}
|
181
196
|
else
|
@@ -196,7 +211,26 @@ class Mailjet::APIMailer
|
|
196
211
|
payload[:Cc] = ccs if mail[:cc]
|
197
212
|
payload[:Bcc] = bccs if mail[:bcc]
|
198
213
|
|
199
|
-
payload
|
214
|
+
decode_emails_V3_1!(payload)
|
215
|
+
end
|
216
|
+
|
217
|
+
def decode_emails_V3_1!(payload)
|
218
|
+
# ActionMailer may have handed us encoded email
|
219
|
+
# addresses, mailjet will reject. Therefore we
|
220
|
+
# walk through the payload to decode them back.
|
221
|
+
payload.each do |key, value|
|
222
|
+
if key == :Email
|
223
|
+
payload[key] = Mail::Encodings.value_decode(value)
|
224
|
+
elsif value.is_a?(Hash)
|
225
|
+
decode_emails_V3_1! value
|
226
|
+
elsif value.is_a?(Array)
|
227
|
+
value.each do |item|
|
228
|
+
if item.is_a?(Hash)
|
229
|
+
decode_emails_V3_1! item
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
200
234
|
end
|
201
235
|
|
202
236
|
def setContentV3_0(mail)
|
@@ -263,7 +297,7 @@ class Mailjet::APIMailer
|
|
263
297
|
payload.merge(content)
|
264
298
|
.merge(base_from)
|
265
299
|
.merge(@delivery_method_options_v3_0)
|
266
|
-
|
300
|
+
end
|
267
301
|
end
|
268
302
|
|
269
303
|
ActionMailer::Base.add_delivery_method :mailjet_api, Mailjet::APIMailer
|
@@ -1,6 +1,5 @@
|
|
1
|
-
require 'active_support'
|
2
1
|
require 'rack/request'
|
3
|
-
|
2
|
+
require 'json'
|
4
3
|
|
5
4
|
module Mailjet
|
6
5
|
module Rack
|
@@ -13,7 +12,7 @@ module Mailjet
|
|
13
12
|
|
14
13
|
def call(env)
|
15
14
|
if env['PATH_INFO'] == @path && (content = env['rack.input'].read)
|
16
|
-
@block.call(
|
15
|
+
@block.call(JSON.parse(content))
|
17
16
|
[200, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
|
18
17
|
else
|
19
18
|
@app.call(env)
|
data/lib/mailjet/resource.rb
CHANGED
@@ -1,11 +1,7 @@
|
|
1
1
|
require 'mailjet/connection'
|
2
|
-
require 'active_support/hash_with_indifferent_access'
|
3
|
-
require 'active_support/core_ext/class'
|
4
|
-
require 'active_support/core_ext/hash'
|
5
2
|
require 'active_support/core_ext/string'
|
6
3
|
require 'active_support/core_ext/module/delegation'
|
7
|
-
require 'active_support/
|
8
|
-
require 'active_support/json/decoding'
|
4
|
+
require 'active_support/hash_with_indifferent_access'
|
9
5
|
#require 'mail'
|
10
6
|
require 'json'
|
11
7
|
|
@@ -18,35 +14,42 @@ require 'json'
|
|
18
14
|
|
19
15
|
module Mailjet
|
20
16
|
module Resource
|
21
|
-
|
17
|
+
|
18
|
+
# define here available options for filtering
|
19
|
+
OPTIONS = [:version, :url, :perform_api_call, :api_key, :secret_key, :read_timeout, :open_timeout]
|
22
20
|
|
23
21
|
NON_JSON_URLS = ['v3/send/message'] # urls that don't accept JSON input
|
24
22
|
|
25
|
-
included
|
26
|
-
|
27
|
-
|
23
|
+
def self.included(base)
|
24
|
+
base.extend ClassMethods
|
25
|
+
base.class_eval do
|
26
|
+
cattr_accessor :resource_path, :public_operations, :read_only, :filters, :resourceprop, :action, :non_json_urls, :version
|
27
|
+
cattr_writer :connection
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
def self.connection(options = {})
|
30
|
+
class_variable_get(:@@connection) || default_connection(options)
|
31
|
+
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
def self.default_connection(options = {})
|
34
|
+
Mailjet::Connection.new(
|
35
|
+
"#{options[:url]}/#{options[:version]}/#{resource_path}",
|
36
|
+
options[:api_key] || Mailjet.config.api_key,
|
37
|
+
options[:secret_key] || Mailjet.config.secret_key,
|
38
|
+
public_operations: public_operations,
|
39
|
+
read_only: read_only,
|
40
|
+
perform_api_call: options[:perform_api_call],
|
41
|
+
open_timeout: options[:open_timeout],
|
42
|
+
read_timeout: options[:read_timeout])
|
43
|
+
end
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
def self.default_headers
|
46
|
+
if NON_JSON_URLS.include?(self.resource_path) # don't use JSON if Send API
|
47
|
+
default_headers = { accept: :json, accept_encoding: :deflate }
|
48
|
+
else
|
49
|
+
default_headers = { accept: :json, accept_encoding: :deflate, content_type: :json } #use JSON if *not* Send API
|
50
|
+
end
|
51
|
+
return default_headers.merge(user_agent: "mailjet-api-v3-ruby/#{Gem.loaded_specs["mailjet"].version}")
|
48
52
|
end
|
49
|
-
return default_headers.merge(user_agent: "mailjet-api-v3-ruby/#{Gem.loaded_specs["mailjet"].version}")
|
50
53
|
end
|
51
54
|
end
|
52
55
|
|
@@ -56,7 +59,7 @@ module Mailjet
|
|
56
59
|
end
|
57
60
|
|
58
61
|
def all(params = {}, options = {})
|
59
|
-
opts =
|
62
|
+
opts = define_options(options)
|
60
63
|
params = format_params(params)
|
61
64
|
response = connection(opts).get(default_headers.merge(params: params))
|
62
65
|
attribute_array = parse_api_json(response)
|
@@ -64,18 +67,24 @@ module Mailjet
|
|
64
67
|
end
|
65
68
|
|
66
69
|
def count(options = {})
|
67
|
-
opts =
|
70
|
+
opts = define_options(options)
|
68
71
|
response_json = connection(opts).get(default_headers.merge(params: {limit: 1, countrecords: 1}))
|
69
|
-
response_hash =
|
72
|
+
response_hash = JSON.parse(response_json)
|
70
73
|
response_hash['Total']
|
71
74
|
end
|
72
75
|
|
73
76
|
def find(id, job_id = nil, options = {})
|
77
|
+
normalized_id = if id.is_a? String
|
78
|
+
URI.encode_www_form_component(id)
|
79
|
+
else
|
80
|
+
id
|
81
|
+
end
|
82
|
+
|
74
83
|
# if action method, ammend url to appropriate id
|
75
|
-
opts =
|
76
|
-
self.resource_path = create_action_resource_path(
|
84
|
+
opts = define_options(options)
|
85
|
+
self.resource_path = create_action_resource_path(normalized_id, job_id) if self.action
|
77
86
|
#
|
78
|
-
attributes = parse_api_json(connection(opts)[
|
87
|
+
attributes = parse_api_json(connection(opts)[normalized_id].get(default_headers)).first
|
79
88
|
instanciate_from_api(attributes)
|
80
89
|
|
81
90
|
rescue Mailjet::ApiError => e
|
@@ -88,8 +97,8 @@ module Mailjet
|
|
88
97
|
|
89
98
|
def create(attributes = {}, options = {})
|
90
99
|
# if action method, ammend url to appropriate id
|
91
|
-
opts =
|
92
|
-
self.resource_path = create_action_resource_path(attributes[:id]) if self.action
|
100
|
+
opts = define_options(options)
|
101
|
+
self.resource_path = create_action_resource_path(attributes[:id]) if (self.action and attributes[:id])
|
93
102
|
attributes.tap { |hs| hs.delete(:id) }
|
94
103
|
|
95
104
|
if Mailjet.config.default_from and self.resource_path == 'send/'
|
@@ -103,14 +112,14 @@ module Mailjet
|
|
103
112
|
|
104
113
|
self.new(attributes).tap do |resource|
|
105
114
|
resource.save!(opts)
|
106
|
-
resource.persisted = true
|
115
|
+
resource.attributes[:persisted] = true
|
107
116
|
end
|
108
117
|
|
109
118
|
end
|
110
119
|
|
111
120
|
def delete(id, options = {})
|
112
121
|
# if action method, ammend url to appropriate id
|
113
|
-
opts =
|
122
|
+
opts = define_options(options)
|
114
123
|
self.resource_path = create_action_resource_path(id) if self.action
|
115
124
|
connection(opts)[id].delete(default_headers)
|
116
125
|
end
|
@@ -120,7 +129,7 @@ module Mailjet
|
|
120
129
|
end
|
121
130
|
|
122
131
|
def parse_api_json(response_json)
|
123
|
-
response_hash =
|
132
|
+
response_hash = JSON.parse(response_json)
|
124
133
|
|
125
134
|
#Take the response from the API and put it through a method -- taken from the ActiveSupport library -- which converts
|
126
135
|
#the date-time from "2014-05-19T15:31:09Z" to "Mon, 19 May 2014 15:31:09 +0000" format.
|
@@ -135,17 +144,17 @@ module Mailjet
|
|
135
144
|
end
|
136
145
|
|
137
146
|
def create_action_resource_path(id, job_id = nil)
|
138
|
-
|
139
|
-
|
147
|
+
url_elements = self.resource_path.split("/")
|
148
|
+
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
|
140
149
|
|
141
|
-
|
142
|
-
|
143
|
-
|
150
|
+
if !(url_elements[1] == "contacts" && self.action == "managemanycontacts")
|
151
|
+
url_elements[2] = id.to_s
|
152
|
+
end
|
144
153
|
|
145
|
-
|
146
|
-
|
154
|
+
url_elements << job_id.to_s if job_id #if job_id exists, amend it to end of the URI
|
155
|
+
url = url_elements.join("/")
|
147
156
|
|
148
|
-
|
157
|
+
return url
|
149
158
|
end
|
150
159
|
|
151
160
|
|
@@ -211,26 +220,14 @@ module Mailjet
|
|
211
220
|
end
|
212
221
|
end
|
213
222
|
|
214
|
-
def
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
if options.key?(:url)
|
223
|
-
url = options[:url]
|
224
|
-
end
|
225
|
-
end
|
226
|
-
ret = {version: ver, url: url, perform_api_call: perform_api_call}
|
227
|
-
ret
|
228
|
-
end
|
229
|
-
|
230
|
-
def choose_version(options = {})
|
231
|
-
ver = options['version'] || Mailjet.config.api_version || version
|
232
|
-
|
233
|
-
ver
|
223
|
+
def define_options(options = {})
|
224
|
+
# merge default options with given ones on-the-fly
|
225
|
+
{
|
226
|
+
version: version || Mailjet.config.api_version,
|
227
|
+
url: Mailjet.config.end_point,
|
228
|
+
perform_api_call: Mailjet.config.perform_api_call
|
229
|
+
}
|
230
|
+
.merge(options.symbolize_keys.slice(*OPTIONS))
|
234
231
|
end
|
235
232
|
|
236
233
|
end
|
@@ -246,7 +243,7 @@ module Mailjet
|
|
246
243
|
end
|
247
244
|
|
248
245
|
def save(options = {})
|
249
|
-
opts = self.class.
|
246
|
+
opts = self.class.define_options(options)
|
250
247
|
|
251
248
|
if persisted?
|
252
249
|
# case where the entity is updated
|
@@ -259,7 +256,7 @@ module Mailjet
|
|
259
256
|
if opts[:perform_api_call] && !persisted?
|
260
257
|
# get attributes only for entity creation
|
261
258
|
self.attributes = if self.resource_path == 'send'
|
262
|
-
|
259
|
+
JSON.parse(response)
|
263
260
|
else
|
264
261
|
parse_api_json(response).first
|
265
262
|
end
|
@@ -297,11 +294,11 @@ module Mailjet
|
|
297
294
|
end
|
298
295
|
end
|
299
296
|
|
300
|
-
opts = self.class.
|
297
|
+
opts = self.class.define_options(options)
|
301
298
|
save(opts)
|
302
299
|
end
|
303
300
|
|
304
|
-
def delete
|
301
|
+
def delete
|
305
302
|
self.class.delete(id)
|
306
303
|
end
|
307
304
|
|
@@ -318,12 +315,14 @@ module Mailjet
|
|
318
315
|
def formatted_payload
|
319
316
|
payload = attributes.reject { |k,v| v.blank? }
|
320
317
|
if persisted?
|
321
|
-
payload = payload.slice(*resourceprop)
|
318
|
+
payload = payload.slice(*resourceprop.map(&:to_s))
|
322
319
|
end
|
323
320
|
payload = camelcase_keys(payload)
|
324
321
|
payload.tap { |hs| hs.delete("Persisted") }
|
325
322
|
payload.inject({}) do |h, (k, v)|
|
326
|
-
|
323
|
+
if v.respond_to? :utc
|
324
|
+
v = v.utc.to_s
|
325
|
+
end
|
327
326
|
h.merge!({k => v})
|
328
327
|
end
|
329
328
|
end
|
@@ -346,20 +345,16 @@ module Mailjet
|
|
346
345
|
|
347
346
|
def method_missing(method_symbol, *arguments) #:nodoc:
|
348
347
|
method_name = method_symbol.to_s
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
attributes[$`] = arguments.first
|
353
|
-
when "?"
|
354
|
-
attributes[$`]
|
355
|
-
end
|
356
|
-
else
|
357
|
-
return attributes[method_name] if attributes.include?(method_name)
|
358
|
-
# not set right now but we know about it
|
359
|
-
# return nil if known_attributes.include?(method_name)
|
360
|
-
super
|
348
|
+
if method_name.end_with?("=")
|
349
|
+
attributes[method_name.chop] = arguments.first
|
350
|
+
return
|
361
351
|
end
|
362
|
-
end
|
363
352
|
|
353
|
+
if attributes.include?(method_name)
|
354
|
+
return attributes[method_name]
|
355
|
+
end
|
356
|
+
|
357
|
+
super
|
358
|
+
end
|
364
359
|
end
|
365
360
|
end
|