mailjet 1.5.0 → 1.6.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.
@@ -20,6 +20,9 @@ module Mailjet
20
20
  module Resource
21
21
  extend ActiveSupport::Concern
22
22
 
23
+ # define here available options for filtering
24
+ OPTIONS = [:version, :url, :perform_api_call, :api_key, :secret_key, :read_timeout, :open_timeout]
25
+
23
26
  NON_JSON_URLS = ['v3/send/message'] # urls that don't accept JSON input
24
27
 
25
28
  included do
@@ -36,7 +39,10 @@ module Mailjet
36
39
  options[:api_key] || Mailjet.config.api_key,
37
40
  options[:secret_key] || Mailjet.config.secret_key,
38
41
  public_operations: public_operations,
39
- read_only: read_only)
42
+ read_only: read_only,
43
+ perform_api_call: options[:perform_api_call],
44
+ open_timeout: options[:open_timeout],
45
+ read_timeout: options[:read_timeout])
40
46
  end
41
47
 
42
48
  def self.default_headers
@@ -55,26 +61,32 @@ module Mailjet
55
61
  end
56
62
 
57
63
  def all(params = {}, options = {})
58
- opts = change_resource_path(options)
64
+ opts = define_options(options)
59
65
  params = format_params(params)
60
- response = connection(opts).get(default_headers.merge(params: params), opts[:perform_api_call])
66
+ response = connection(opts).get(default_headers.merge(params: params))
61
67
  attribute_array = parse_api_json(response)
62
68
  attribute_array.map{ |attributes| instanciate_from_api(attributes) }
63
69
  end
64
70
 
65
71
  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])
72
+ opts = define_options(options)
73
+ response_json = connection(opts).get(default_headers.merge(params: {limit: 1, countrecords: 1}))
68
74
  response_hash = ActiveSupport::JSON.decode(response_json)
69
75
  response_hash['Total']
70
76
  end
71
77
 
72
78
  def find(id, job_id = nil, options = {})
79
+ normalized_id = if id.is_a? String
80
+ URI.encode_www_form_component(id)
81
+ else
82
+ id
83
+ end
84
+
73
85
  # if action method, ammend url to appropriate id
74
- opts = change_resource_path(options)
75
- self.resource_path = create_action_resource_path(id, job_id) if self.action
86
+ opts = define_options(options)
87
+ self.resource_path = create_action_resource_path(normalized_id, job_id) if self.action
76
88
  #
77
- attributes = parse_api_json(connection(opts)[id].get(default_headers, opts[:perform_api_call])).first
89
+ attributes = parse_api_json(connection(opts)[normalized_id].get(default_headers)).first
78
90
  instanciate_from_api(attributes)
79
91
 
80
92
  rescue Mailjet::ApiError => e
@@ -87,8 +99,8 @@ module Mailjet
87
99
 
88
100
  def create(attributes = {}, options = {})
89
101
  # if action method, ammend url to appropriate id
90
- opts = change_resource_path(options)
91
- self.resource_path = create_action_resource_path(attributes[:id]) if self.action
102
+ opts = define_options(options)
103
+ self.resource_path = create_action_resource_path(attributes[:id]) if (self.action and attributes[:id])
92
104
  attributes.tap { |hs| hs.delete(:id) }
93
105
 
94
106
  if Mailjet.config.default_from and self.resource_path == 'send/'
@@ -102,16 +114,16 @@ module Mailjet
102
114
 
103
115
  self.new(attributes).tap do |resource|
104
116
  resource.save!(opts)
105
- resource.persisted = true
117
+ resource.attributes[:persisted] = true
106
118
  end
107
119
 
108
120
  end
109
121
 
110
122
  def delete(id, options = {})
111
123
  # if action method, ammend url to appropriate id
112
- opts = change_resource_path(options)
124
+ opts = define_options(options)
113
125
  self.resource_path = create_action_resource_path(id) if self.action
114
- connection(options)[id].delete(default_headers)
126
+ connection(opts)[id].delete(default_headers)
115
127
  end
116
128
 
117
129
  def instanciate_from_api(attributes = {})
@@ -120,6 +132,7 @@ module Mailjet
120
132
 
121
133
  def parse_api_json(response_json)
122
134
  response_hash = ActiveSupport::JSON.decode(response_json)
135
+
123
136
  #Take the response from the API and put it through a method -- taken from the ActiveSupport library -- which converts
124
137
  #the date-time from "2014-05-19T15:31:09Z" to "Mon, 19 May 2014 15:31:09 +0000" format.
125
138
  response_hash = convert_dates_from(response_hash)
@@ -133,17 +146,17 @@ module Mailjet
133
146
  end
134
147
 
135
148
  def create_action_resource_path(id, job_id = nil)
136
- url_elements = self.resource_path.split("/")
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
149
+ url_elements = self.resource_path.split("/")
150
+ 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
138
151
 
139
- if !(url_elements[1] == "contacts" && self.action == "managemanycontacts")
140
- url_elements[2] = id.to_s
141
- end
152
+ if !(url_elements[1] == "contacts" && self.action == "managemanycontacts")
153
+ url_elements[2] = id.to_s
154
+ end
142
155
 
143
- url_elements << job_id.to_s if job_id #if job_id exists, amend it to end of the URI
144
- url = url_elements.join("/")
156
+ url_elements << job_id.to_s if job_id #if job_id exists, amend it to end of the URI
157
+ url = url_elements.join("/")
145
158
 
146
- return url
159
+ return url
147
160
  end
148
161
 
149
162
 
@@ -209,26 +222,14 @@ module Mailjet
209
222
  end
210
223
  end
211
224
 
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
225
+ def define_options(options = {})
226
+ # merge default options with given ones on-the-fly
227
+ {
228
+ version: version || Mailjet.config.api_version,
229
+ url: Mailjet.config.end_point,
230
+ perform_api_call: Mailjet.config.perform_api_call
231
+ }
232
+ .merge(options.symbolize_keys.slice(*OPTIONS))
232
233
  end
233
234
 
234
235
  end
@@ -243,24 +244,28 @@ module Mailjet
243
244
  attributes[:persisted]
244
245
  end
245
246
 
246
- def save(options)
247
+ def save(options = {})
248
+ opts = self.class.define_options(options)
249
+
247
250
  if persisted?
248
- response = connection(options)[attributes[:id]].put(formatted_payload, default_headers, options[:perform_api_call])
251
+ # case where the entity is updated
252
+ response = connection(opts)[attributes[:id]].put(formatted_payload, default_headers)
249
253
  else
250
- response = connection(options).post(formatted_payload, default_headers, options[:perform_api_call])
254
+ # case where the entity is created
255
+ response = connection(opts).post(formatted_payload, default_headers)
251
256
  end
252
257
 
253
- if options[:perform_api_call]
254
- if self.resource_path == 'send/'
255
- self.attributes = ActiveSupport::JSON.decode(response)
256
- return true
258
+ if opts[:perform_api_call] && !persisted?
259
+ # get attributes only for entity creation
260
+ self.attributes = if self.resource_path == 'send'
261
+ ActiveSupport::JSON.decode(response)
262
+ else
263
+ parse_api_json(response).first
257
264
  end
258
-
259
- self.attributes = parse_api_json(response).first
260
- return true
261
- else
262
- return true
263
265
  end
266
+
267
+ return true
268
+
264
269
  rescue Mailjet::ApiError => e
265
270
  if e.code.to_s == "304"
266
271
  return true # When you save a record twice it should not raise error
@@ -269,7 +274,7 @@ module Mailjet
269
274
  end
270
275
  end
271
276
 
272
- def save!(options)
277
+ def save!(options = {})
273
278
  save(options) || raise(StandardError.new("Resource not persisted"))
274
279
  end
275
280
 
@@ -280,11 +285,22 @@ module Mailjet
280
285
  end
281
286
 
282
287
  def update_attributes(attribute_hash = {}, options = {})
283
- self.attributes = attribute_hash
284
- save(options)
288
+ self.attributes.deep_merge!(attribute_hash) do |key, old, new|
289
+ if old.is_a?(Array) && new.is_a?(Array)
290
+ # uniqueness of hashes based on their value of "Name"
291
+ (new + old).uniq do |data|
292
+ data["Name"]
293
+ end
294
+ else
295
+ new
296
+ end
297
+ end
298
+
299
+ opts = self.class.define_options(options)
300
+ save(opts)
285
301
  end
286
302
 
287
- def delete(call)
303
+ def delete
288
304
  self.class.delete(id)
289
305
  end
290
306
 
@@ -301,7 +317,7 @@ module Mailjet
301
317
  def formatted_payload
302
318
  payload = attributes.reject { |k,v| v.blank? }
303
319
  if persisted?
304
- payload = payload.slice(*resourceprop)
320
+ payload = payload.slice(*resourceprop.map(&:to_s))
305
321
  end
306
322
  payload = camelcase_keys(payload)
307
323
  payload.tap { |hs| hs.delete("Persisted") }
@@ -329,20 +345,16 @@ module Mailjet
329
345
 
330
346
  def method_missing(method_symbol, *arguments) #:nodoc:
331
347
  method_name = method_symbol.to_s
332
- if method_name =~ /(=|\?)$/
333
- case $1
334
- when "="
335
- attributes[$`] = arguments.first
336
- when "?"
337
- attributes[$`]
338
- end
339
- else
340
- return attributes[method_name] if attributes.include?(method_name)
341
- # not set right now but we know about it
342
- # return nil if known_attributes.include?(method_name)
343
- super
348
+ if method_name.end_with?("=")
349
+ attributes[method_name.chop] = arguments.first
350
+ return
344
351
  end
345
- end
346
352
 
353
+ if attributes.include?(method_name)
354
+ return attributes[method_name]
355
+ end
356
+
357
+ super
358
+ end
347
359
  end
348
360
  end
@@ -1,7 +1,7 @@
1
1
  module Mailjet
2
2
  class Campaigndraft
3
3
  include Mailjet::Resource
4
- self.resource_path = 'v3/REST/campaigndraft'
4
+ self.resource_path = 'REST/campaigndraft'
5
5
  self.public_operations = [:get, :put, :post]
6
6
  self.filters = [:campaign, :contacts_list, :delivered_at, :edit_mode, :is_archived, :is_campaign, :is_deleted, :is_handled, :is_starred, :modified, :news_letter_template, :segmentation, :status, :subject, :template]
7
7
  self.resourceprop = [:campaign, :contacts_list, :created_at, :delivered_at, :edit_mode, :id, :is_starred, :is_text_part_included, :locale, :modified_at, :preset, :reply_email, :segmentation, :sender, :sender_email, :sender_name, :status, :subject, :template_id, :title, :url, :used, 'CampaignID', 'CampaignALT', 'ContactsListID', 'ContactsListALT', 'SegmentationID', 'SegmentationALT', :contacts_list_id,'TemplateID']
@@ -2,7 +2,7 @@ module Mailjet
2
2
  class Campaigndraft_detailcontent
3
3
  include Mailjet::Resource
4
4
  self.action = "detailcontent"
5
- self.resource_path = "v3/REST/campaigndraft/id/#{self.action}"
5
+ self.resource_path = "REST/campaigndraft/id/#{self.action}"
6
6
  self.public_operations = [:get, :post]
7
7
  self.filters = []
8
8
  self.resourceprop = [:text_part, :html_part, :headers, :mjml_content]
@@ -2,7 +2,7 @@ module Mailjet
2
2
  class Campaigndraft_schedule
3
3
  include Mailjet::Resource
4
4
  self.action = "schedule"
5
- self.resource_path = "v3/REST/campaigndraft/id/#{self.action}"
5
+ self.resource_path = "REST/campaigndraft/id/#{self.action}"
6
6
  self.public_operations = [:post, :delete , :get]
7
7
  self.filters = []
8
8
  self.resourceprop = [:date]
@@ -2,7 +2,7 @@ module Mailjet
2
2
  class Campaigndraft_send
3
3
  include Mailjet::Resource
4
4
  self.action = "send"
5
- self.resource_path = "v3/REST/campaigndraft/id/#{self.action}"
5
+ self.resource_path = "REST/campaigndraft/id/#{self.action}"
6
6
  self.public_operations = [:post]
7
7
  self.filters = []
8
8
  self.resourceprop = []
@@ -2,7 +2,7 @@ module Mailjet
2
2
  class Campaigndraft_status
3
3
  include Mailjet::Resource
4
4
  self.action = 'status'
5
- self.resource_path = "v3/REST/campaigndraft/id/#{self.action}"
5
+ self.resource_path = "REST/campaigndraft/id/#{self.action}"
6
6
  self.public_operations = [:get]
7
7
  self.filters = []
8
8
  self.resourceprop = []
@@ -2,7 +2,7 @@ module Mailjet
2
2
  class Campaigndraft_test
3
3
  include Mailjet::Resource
4
4
  self.action = "test"
5
- self.resource_path = "v3/REST/campaigndraft/id/#{self.action}"
5
+ self.resource_path = "REST/campaigndraft/id/#{self.action}"
6
6
  self.public_operations = [:post]
7
7
  self.filters = []
8
8
  self.resourceprop = [:email, :name]
@@ -6,5 +6,22 @@ module Mailjet
6
6
  self.public_operations = [:get]
7
7
  self.filters = []
8
8
  self.resourceprop = []
9
+
10
+ def self.find(id, job_id = nil, options = {})
11
+ opts = define_options(options)
12
+ self.resource_path = create_action_resource_path(id, job_id) if self.action
13
+
14
+ raw_data = parse_api_json(connection(opts).get(default_headers))
15
+
16
+ raw_data.map do |entity|
17
+ instanciate_from_api(entity)
18
+ end
19
+ rescue Mailjet::ApiError => e
20
+ if e.code == 404
21
+ nil
22
+ else
23
+ raise e
24
+ end
25
+ end
9
26
  end
10
27
  end
@@ -8,5 +8,21 @@ module Mailjet
8
8
 
9
9
  self.read_only = true
10
10
 
11
+ def self.find(id, job_id = nil, options = {})
12
+ opts = define_options(options)
13
+ self.resource_path = create_action_resource_path(id, job_id) if self.action
14
+
15
+ raw_data = parse_api_json(connection(opts)[id].get(default_headers))
16
+
17
+ raw_data.map do |entity|
18
+ instanciate_from_api(entity)
19
+ end
20
+ rescue Mailjet::ApiError => e
21
+ if e.code == 404
22
+ nil
23
+ else
24
+ raise e
25
+ end
26
+ end
11
27
  end
12
28
  end
@@ -4,7 +4,6 @@ module Mailjet
4
4
  self.resource_path = 'REST/newsletter'
5
5
  self.public_operations = [:get, :put, :post]
6
6
  self.filters = [:campaign, :contacts_list, :delivered_at, :edit_mode, :is_archived, :is_campaign, :is_deleted, :is_handled, :is_starred, :modified, :news_letter_template, :segmentation, :status, :subject]
7
- self.resourceprop = [:callback, :campaign, :contacts_list, :created_at, :delivered_at, :edit_mode, :edit_type, :footer, :footer_address, :footer_wysiwyg_type, :header_filename, :header_link, :header_text, :header_url, :id, :ip, :is_handled, :is_starred, :is_text_part_included, :locale, :modified_at, :permalink, :permalink_host, :permalink_wysiwyg_type, :politeness_mode, :reply_email, :segmentation, :sender, :sender_email, :sender_name, :status, :subject, :template, :test_address, :title, :url, 'CampaignID', 'CampaignALT', 'ContactsListID', 'ContactsListALT', 'SegmentationID', 'SegmentationALT', :contacts_list_id]
8
-
7
+ self.resourceprop = [:callback, :campaign, :contacts_list, :created_at, :delivered_at, :edit_mode, :edit_type, :footer, :footer_address, :footer_wysiwyg_type, :header_filename, :header_link, :header_text, :header_url, :id, :ip, :is_handled, :is_starred, :is_text_part_included, :locale, :modified_at, :permalink, :permalink_host, :permalink_wysiwyg_type, :politeness_mode, :reply_email, :segmentation, :segmentation_id, :sender, :sender_email, :sender_name, :status, :subject, :template, :test_address, :title, :url, 'CampaignID', 'CampaignALT', 'ContactsListID', 'ContactsListALT', 'SegmentationID', 'SegmentationALT', :contacts_list_id]
9
8
  end
10
9
  end
@@ -3,7 +3,7 @@ module Mailjet
3
3
  include Mailjet::Resource
4
4
  self.action = "schedule"
5
5
  self.resource_path = "REST/newsletter/id/#{self.action}"
6
- self.public_operations = [:post, :delete]
6
+ self.public_operations = [:get, :post, :delete]
7
7
  self.filters = []
8
8
  self.resourceprop = [:date]
9
9
 
@@ -0,0 +1,33 @@
1
+ module Mailjet
2
+ class Statcounters
3
+ include Mailjet::Resource
4
+ self.resource_path = 'REST/statcounters'
5
+ self.public_operations = [:get]
6
+ self.filters = [:source_id, :counter_resolution, :counter_source, :counter_timing]
7
+ self.resourceprop = [
8
+ :api_key_id,
9
+ :event_click_delay,
10
+ :event_clicked_count,
11
+ :event_open_delay,
12
+ :event_opened_count,
13
+ :event_spam_count,
14
+ :event_unsubscribed_count,
15
+ :event_workflow_exited_count,
16
+ :message_blocked_count,
17
+ :message_clicked_count,
18
+ :message_deferred_count,
19
+ :message_hard_bounced_count,
20
+ :message_opened_count,
21
+ :message_queued_count,
22
+ :message_sent_count,
23
+ :message_soft_bounced_count,
24
+ :message_spam_count,
25
+ :message_unsubscribed_count,
26
+ :message_work_flow_exited_count,
27
+ :source_id,
28
+ :timeslice,
29
+ :total,
30
+ ]
31
+ self.read_only = true
32
+ end
33
+ end
@@ -1,10 +1,26 @@
1
1
  module Mailjet
2
2
  class Template_detailcontent
3
3
  include Mailjet::Resource
4
- self.resource_path = 'REST/template/id/${self.action}'
5
- self.public_operations = [:get, :put, :post, :delete]
4
+ self.action = 'detailcontent'
5
+ self.resource_path = "REST/template/id/#{self.action}"
6
+ self.public_operations = [:get,:post]
6
7
  self.filters = [:api_key, :categories, :categories_selection_method, :edit_mode, :name, :owner_type, :purposes, :purposes_selection_method, :user]
7
8
  self.resourceprop = [:author, :categories, :copyright, :description, :edit_mode, :is_starred, :name, :owner_type, :presets, :purposes]
8
9
 
10
+ def self.find(id, options = {})
11
+ self.resource_path = create_action_resource_path(id)
12
+
13
+ opts = define_options(options)
14
+ response = connection(opts).get(default_headers)
15
+ attributes = parse_api_json(response).first
16
+
17
+ instanciate_from_api(attributes)
18
+ rescue Mailjet::ApiError => e
19
+ if e.code == 404
20
+ nil
21
+ else
22
+ raise e
23
+ end
24
+ end
9
25
  end
10
26
  end