From a65aa5b1ab07b69e4b1ba29c34ee15ba76b2678e Mon Sep 17 00:00:00 2001 From: Lorel Date: Fri, 26 May 2017 12:38:35 +0200 Subject: [PATCH 1/7] fix issue #97 --- lib/mailjet/resource.rb | 2 +- spec/mailjet-spec.rb | 1 + spec/mailjet/mailer_spec.rb | 53 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/mailjet/resource.rb b/lib/mailjet/resource.rb index ea24d44..d1b9518 100644 --- a/lib/mailjet/resource.rb +++ b/lib/mailjet/resource.rb @@ -251,7 +251,7 @@ def save(options) end if options[:perform_api_call] - if self.resource_path == 'send/' + if self.resource_path.include? 'send' self.attributes = ActiveSupport::JSON.decode(response) return true end diff --git a/spec/mailjet-spec.rb b/spec/mailjet-spec.rb index e4503c0..a93a7fa 100644 --- a/spec/mailjet-spec.rb +++ b/spec/mailjet-spec.rb @@ -10,6 +10,7 @@ Mailjet.configure do |config| config.api_key = API_KEY config.secret_key = API_SECRET + config.api_version = "v3" end end diff --git a/spec/mailjet/mailer_spec.rb b/spec/mailjet/mailer_spec.rb index 283c539..3ef57ff 100644 --- a/spec/mailjet/mailer_spec.rb +++ b/spec/mailjet/mailer_spec.rb @@ -252,5 +252,58 @@ module Mailjet }] ) end + + it 'should return data in attribute "Sent" using API v3' do + + Mailjet.configure do |config| + config.api_key = ENV['MJ_APIKEY_PUBLIC'] + config.secret_key = ENV['MJ_APIKEY_PRIVATE'] + config.api_version = "v3" + end + + from_email = 'albert@bar.com' + recipient = { 'Email': 'test@test.com' } + + message = Mailjet::Send.create( + from_email: from_email, + from_name: 'Mailjet Pilot', + subject: 'Your email flight plan!', + text_part: 'Dear passenger, welcome to Mailjet! May the delivery force be with you!', + html_part: '

Dear passenger, welcome to Mailjet!


May the delivery force be with you!', + recipients: [recipient] + ) + + expect(message.attributes['Sent'].first).to include(recipient) + end + + # TODO set a test environment for API v3.1 + # it 'should return data in attribute "Sent" using API v3.1' do + # + # Mailjet.configure do |config| + # config.api_key = ENV['MJ_APIKEY_PUBLIC'] + # config.secret_key = ENV['MJ_APIKEY_PRIVATE'] + # config.api_version = "v3.1" + # end + # + # message = Mailjet::Send.create( + # messages: [{ + # 'From' => { + # 'Email' => 'albert@bar.com', + # 'Name' => 'Albert' + # }, + # 'To' => [ + # { + # 'Email' => 'test@test.com', + # 'Name' => 'test' + # } + # ], + # 'Subject' => 'Your email flight plan!', + # 'TextPart' => 'Dear passenger 1, welcome to Mailjet! May the delivery force be with you!', + # 'HTMLPart' => '

Dear passenger 1, welcome to Mailjet!


May the delivery force be with you!' + # }] + # ) + # + # expect(message.attributes['Sent'].first).to include(recipient) + # end end end From 32f32a0211a33fae31aa6dce8d57df824a95a5cb Mon Sep 17 00:00:00 2001 From: Arnaud Breton Date: Fri, 26 May 2017 14:08:14 +0200 Subject: [PATCH 2/7] List account sender spec --- spec/mailjet/mailer_spec.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/mailjet/mailer_spec.rb b/spec/mailjet/mailer_spec.rb index 3ef57ff..40ef8bb 100644 --- a/spec/mailjet/mailer_spec.rb +++ b/spec/mailjet/mailer_spec.rb @@ -253,7 +253,16 @@ module Mailjet ) end - it 'should return data in attribute "Sent" using API v3' do + it 'should allow to list valid sender' do + Mailjet.configure do |config| + config.api_key = ENV['MJ_APIKEY_PUBLIC'] + config.secret_key = ENV['MJ_APIKEY_PRIVATE'] + end + + p Mailjet::Sender.all(limit: 0) + end + + it 'should return data in attribute "Sent" using Send API v3.0' do Mailjet.configure do |config| config.api_key = ENV['MJ_APIKEY_PUBLIC'] From 374174f50aaaa3ae53809af9c8d19cad7f256ac2 Mon Sep 17 00:00:00 2001 From: Arnaud Breton Date: Fri, 26 May 2017 14:12:30 +0200 Subject: [PATCH 3/7] Using "TEST_EMAIL" env var in tests --- spec/mailjet/mailer_spec.rb | 65 ++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/spec/mailjet/mailer_spec.rb b/spec/mailjet/mailer_spec.rb index 40ef8bb..876009a 100644 --- a/spec/mailjet/mailer_spec.rb +++ b/spec/mailjet/mailer_spec.rb @@ -269,50 +269,47 @@ module Mailjet config.secret_key = ENV['MJ_APIKEY_PRIVATE'] config.api_version = "v3" end - - from_email = 'albert@bar.com' - recipient = { 'Email': 'test@test.com' } - + message = Mailjet::Send.create( - from_email: from_email, + from_email: ENV['TEST_EMAIL'], from_name: 'Mailjet Pilot', subject: 'Your email flight plan!', text_part: 'Dear passenger, welcome to Mailjet! May the delivery force be with you!', html_part: '

Dear passenger, welcome to Mailjet!


May the delivery force be with you!', - recipients: [recipient] + recipients: [{ 'Email': ENV['TEST_EMAIL'] }] ) expect(message.attributes['Sent'].first).to include(recipient) end - # TODO set a test environment for API v3.1 - # it 'should return data in attribute "Sent" using API v3.1' do - # - # Mailjet.configure do |config| - # config.api_key = ENV['MJ_APIKEY_PUBLIC'] - # config.secret_key = ENV['MJ_APIKEY_PRIVATE'] - # config.api_version = "v3.1" - # end - # - # message = Mailjet::Send.create( - # messages: [{ - # 'From' => { - # 'Email' => 'albert@bar.com', - # 'Name' => 'Albert' - # }, - # 'To' => [ - # { - # 'Email' => 'test@test.com', - # 'Name' => 'test' - # } - # ], - # 'Subject' => 'Your email flight plan!', - # 'TextPart' => 'Dear passenger 1, welcome to Mailjet! May the delivery force be with you!', - # 'HTMLPart' => '

Dear passenger 1, welcome to Mailjet!


May the delivery force be with you!' - # }] - # ) - # - # expect(message.attributes['Sent'].first).to include(recipient) + TODO set a test environment for API v3.1 + it 'should return data in attribute "Sent" using API v3.1' do + + Mailjet.configure do |config| + config.api_key = ENV['MJ_APIKEY_PUBLIC'] + config.secret_key = ENV['MJ_APIKEY_PRIVATE'] + config.api_version = "v3.1" + end + + message = Mailjet::Send.create( + messages: [{ + 'From' => { + 'Email' => ENV['TEST_EMAIL'], + 'Name' => 'Albert' + }, + 'To' => [ + { + 'Email' => ENV['TEST_EMAIL'], + 'Name' => 'test' + } + ], + 'Subject' => 'Your email flight plan!', + 'TextPart' => 'Dear passenger 1, welcome to Mailjet! May the delivery force be with you!', + 'HTMLPart' => '

Dear passenger 1, welcome to Mailjet!


May the delivery force be with you!' + }] + ) + + expect(message.attributes['Sent'].first).to include(recipient) # end end end From fa9ab92cae8e6f23e09929ea05ad788e5b006352 Mon Sep 17 00:00:00 2001 From: Arnaud Breton Date: Fri, 26 May 2017 14:12:46 +0200 Subject: [PATCH 4/7] Remove list senders --- spec/mailjet/mailer_spec.rb | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/spec/mailjet/mailer_spec.rb b/spec/mailjet/mailer_spec.rb index 876009a..03411fd 100644 --- a/spec/mailjet/mailer_spec.rb +++ b/spec/mailjet/mailer_spec.rb @@ -253,15 +253,6 @@ module Mailjet ) end - it 'should allow to list valid sender' do - Mailjet.configure do |config| - config.api_key = ENV['MJ_APIKEY_PUBLIC'] - config.secret_key = ENV['MJ_APIKEY_PRIVATE'] - end - - p Mailjet::Sender.all(limit: 0) - end - it 'should return data in attribute "Sent" using Send API v3.0' do Mailjet.configure do |config| @@ -269,7 +260,7 @@ module Mailjet config.secret_key = ENV['MJ_APIKEY_PRIVATE'] config.api_version = "v3" end - + message = Mailjet::Send.create( from_email: ENV['TEST_EMAIL'], from_name: 'Mailjet Pilot', From cff5d8eada5dd461638836d08dacfa9c8bbaf051 Mon Sep 17 00:00:00 2001 From: Arnaud Breton Date: Fri, 26 May 2017 14:16:21 +0200 Subject: [PATCH 5/7] Fix syntax --- spec/mailjet/mailer_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/mailjet/mailer_spec.rb b/spec/mailjet/mailer_spec.rb index 03411fd..7a50525 100644 --- a/spec/mailjet/mailer_spec.rb +++ b/spec/mailjet/mailer_spec.rb @@ -273,7 +273,6 @@ module Mailjet expect(message.attributes['Sent'].first).to include(recipient) end - TODO set a test environment for API v3.1 it 'should return data in attribute "Sent" using API v3.1' do Mailjet.configure do |config| @@ -301,6 +300,6 @@ module Mailjet ) expect(message.attributes['Sent'].first).to include(recipient) - # end + end end end From 5c75df16bffbdb0f15cd668aa00b5a60679320fc Mon Sep 17 00:00:00 2001 From: Arnaud Breton Date: Fri, 26 May 2017 14:20:14 +0200 Subject: [PATCH 6/7] Restore recipient variable in spec --- spec/mailjet/mailer_spec.rb | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/spec/mailjet/mailer_spec.rb b/spec/mailjet/mailer_spec.rb index 7a50525..a5f4b7d 100644 --- a/spec/mailjet/mailer_spec.rb +++ b/spec/mailjet/mailer_spec.rb @@ -261,13 +261,15 @@ module Mailjet config.api_version = "v3" end + recipient = { 'Email': ENV['TEST_EMAIL'] } + message = Mailjet::Send.create( from_email: ENV['TEST_EMAIL'], - from_name: 'Mailjet Pilot', - subject: 'Your email flight plan!', - text_part: 'Dear passenger, welcome to Mailjet! May the delivery force be with you!', - html_part: '

Dear passenger, welcome to Mailjet!


May the delivery force be with you!', - recipients: [{ 'Email': ENV['TEST_EMAIL'] }] + from_name: 'Mailjet Ruby Wrapper CI', + subject: 'Mailjet Ruby Wrapper CI Send API v3.0 spec', + text_part: 'Mailjet Ruby Wrapper CI content', + html_part: '

Mailjet Ruby Wrapper CI content

', + recipients: [recipient] ) expect(message.attributes['Sent'].first).to include(recipient) @@ -281,25 +283,27 @@ module Mailjet config.api_version = "v3.1" end + recipient = { + 'Email' => ENV['TEST_EMAIL'], + 'Name' => 'test' + } + message = Mailjet::Send.create( messages: [{ 'From' => { 'Email' => ENV['TEST_EMAIL'], - 'Name' => 'Albert' + 'Name' => 'Mailjet Ruby Wrapper CI' }, 'To' => [ - { - 'Email' => ENV['TEST_EMAIL'], - 'Name' => 'test' - } + recipient ], - 'Subject' => 'Your email flight plan!', - 'TextPart' => 'Dear passenger 1, welcome to Mailjet! May the delivery force be with you!', - 'HTMLPart' => '

Dear passenger 1, welcome to Mailjet!


May the delivery force be with you!' + 'Subject' => 'Mailjet Ruby Wrapper CI Send API v3.1 spec', + 'TextPart' => 'Mailjet Ruby Wrapper CI content', + 'HTMLPart' => '

Mailjet Ruby Wrapper CI content

' }] ) - expect(message.attributes['Sent'].first).to include(recipient) + expect(message.attributes['Messages'].first['To'].first['Email']).to eq(ENV['TEST_EMAIL']) end end end From 2fc4bccc6f016adb3b30e0984dae59b35497bdd9 Mon Sep 17 00:00:00 2001 From: Lorel Date: Fri, 26 May 2017 16:37:27 +0200 Subject: [PATCH 7/7] fix-issue-96 --- lib/mailjet/resource.rb | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/mailjet/resource.rb b/lib/mailjet/resource.rb index ea24d44..dc7d79f 100644 --- a/lib/mailjet/resource.rb +++ b/lib/mailjet/resource.rb @@ -245,22 +245,24 @@ def persisted? def save(options) if persisted? + # case where the entity is updated response = connection(options)[attributes[:id]].put(formatted_payload, default_headers, options[:perform_api_call]) else + # case where the entity is created response = connection(options).post(formatted_payload, default_headers, options[:perform_api_call]) end - if options[:perform_api_call] - if self.resource_path == 'send/' - self.attributes = ActiveSupport::JSON.decode(response) - return true + if options[:perform_api_call] && !persisted? + # get attributes only for entity creation + self.attributes = if self.resource_path == 'send/' + ActiveSupport::JSON.decode(response) + else + parse_api_json(response).first end - - self.attributes = parse_api_json(response).first - return true - else - return true end + + return true + rescue Mailjet::ApiError => e if e.code.to_s == "304" return true # When you save a record twice it should not raise error @@ -280,8 +282,19 @@ def attributes=(attribute_hash = {}) end def update_attributes(attribute_hash = {}, options = {}) - self.attributes = attribute_hash - save(options) + self.attributes.deep_merge!(attribute_hash) do |key, old, new| + if old.is_a?(Array) && new.is_a?(Array) + # uniqueness of hashes based on their value of "Name" + (new + old).uniq do |data| + data["Name"] + end + else + new + end + end + + opts = self.class.change_resource_path(options) + save(opts) end def delete(call)