sendgrid_actionmailer_adapter 0.2.2 → 0.3.0

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
  SHA256:
3
- metadata.gz: f3459fa2cabe507129253244ce31666e7c613b4761f1d3209e9d42bd7a142b41
4
- data.tar.gz: 7df639a0e08d5f906cdb64f2851b3157120fe253c2819ec293e0d45259858345
3
+ metadata.gz: cd86c4547fe8aa68707f63e6613531e67e0b8f9b08a454f2bb1d6c4977c2ebf3
4
+ data.tar.gz: 24d6b1d4aed6f1f359453e8686fec2cad0af806a9cad03b03624deb01798fa07
5
5
  SHA512:
6
- metadata.gz: 03a37ec28980cafa9aaa7a7175fe7946b43cf8ac3b83ae4ff5ffed70f915b172f4982efe2404d3399199da77eda88f9f27ca7345a19934001b24adf7a2f89a45
7
- data.tar.gz: 7c18f61dc83ca6eb4d8ed50e0de4d5092dd78d1c5de55064ed699c97159ab1d142d6e3010afa30618ad215c5de86f1654ec5bf852621552c4ec8b5d2eb6f8c0d
6
+ metadata.gz: ea3939b006d894a1e29841fd26642f61ac56b3310ad97c7919c7e59d325d3b11e8d0ce6264e25ccb453ddc5078183f603f045f334a9760e4519fe2b12526131a
7
+ data.tar.gz: 4ffb01e435809c556efa5290e0bcc2881cd31770c9df4a79cb2b6494a08e49e9c842524e1cec5ff305f386c09abf8661a6179ca574cbc02439fcdc885675acb8
@@ -7,7 +7,7 @@ module SendGridActionMailerAdapter
7
7
 
8
8
  class << self
9
9
  attr_accessor :api_key, :host, :request_headers, :version, :retry_max_count,
10
- :retry_wait_seconds, :return_response
10
+ :retry_wait_seconds, :return_response, :logger
11
11
 
12
12
  # Set your configuration with block.
13
13
  def configure
@@ -30,6 +30,7 @@ module SendGridActionMailerAdapter
30
30
  wait_seconds: retry_wait_seconds || DEFAULT_RETRY_WAIT_SECONDS,
31
31
  },
32
32
  return_response: return_response,
33
+ logger: logger,
33
34
  }.freeze
34
35
  end
35
36
 
@@ -42,6 +43,7 @@ module SendGridActionMailerAdapter
42
43
  self.retry_max_count = nil
43
44
  self.retry_wait_seconds = nil
44
45
  self.return_response = nil
46
+ self.logger = nil
45
47
  @settings = nil
46
48
  end
47
49
  end
@@ -11,6 +11,7 @@ module SendGridActionMailerAdapter
11
11
  def convert(mail)
12
12
  categories_str = mail['categories']&.value
13
13
  return if categories_str.nil? || categories_str.empty?
14
+
14
15
  categories_str.split(', ').map { |c| ::SendGrid::Category.new(name: c) }
15
16
  end
16
17
 
@@ -11,6 +11,7 @@ module SendGridActionMailerAdapter
11
11
  def convert(mail)
12
12
  addr = mail[:reply_to]&.addrs&.first
13
13
  return if addr.nil?
14
+
14
15
  ::SendGrid::Email.new(email: addr.address, name: addr.display_name)
15
16
  end
16
17
 
@@ -11,6 +11,7 @@ module SendGridActionMailerAdapter
11
11
  def convert(mail)
12
12
  send_at_str = mail['send_at']&.value
13
13
  return if send_at_str.nil? || send_at_str.empty?
14
+
14
15
  send_at_str.to_i
15
16
  end
16
17
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'logger'
3
4
  require 'sendgrid/client'
4
5
  require_relative 'configuration'
5
6
  require_relative 'converter'
@@ -22,6 +23,7 @@ module SendGridActionMailerAdapter
22
23
  # @options settings [Integer, Float] :retry_wait_sec Wait seconds for next retry, Default is 1.
23
24
  def initialize(settings)
24
25
  @settings = ::SendGridActionMailerAdapter::Configuration.settings.merge(settings)
26
+ @logger = @settings[:logger] || ::Logger.new(nil)
25
27
  end
26
28
 
27
29
  # Deliver a mail via SendGrid Web API.
@@ -33,29 +35,54 @@ module SendGridActionMailerAdapter
33
35
  # @raise [SendGridActionMailerAdapter::ApiError] when SendGrid Web API returns error response.
34
36
  def deliver!(mail)
35
37
  sendgrid_mail = ::SendGridActionMailerAdapter::Converter.to_sendgrid_mail(mail)
36
- body = sendgrid_mail.to_json
37
- client = ::SendGrid::API.new(@settings[:sendgrid]).client.mail._('send')
38
+
39
+ if mail[:remove_from_bounces]
40
+ remove_to_addrs_from_bounces(sendgrid_mail)
41
+ end
38
42
 
39
43
  with_retry(@settings[:retry]) do
40
- response = client.post(request_body: body)
44
+ @logger.info("Calling sendMail API, #{sendgrid_mail.inspect}")
45
+ response = sendgrid_client.mail._('send').post(request_body: sendgrid_mail.to_json)
46
+ @logger.info("End calling sendMail API, status_code: #{response.status_code}")
41
47
  handle_response!(response)
42
48
  end
43
49
  end
44
50
 
45
51
  private
46
52
 
53
+ def sendgrid_client
54
+ @sendgrid_client ||= ::SendGrid::API.new(@settings[:sendgrid]).client
55
+ end
56
+
57
+ # @param [::SendGrid::Mail]
58
+ def remove_to_addrs_from_bounces(sendgrid_mail)
59
+ sendgrid_mail.personalizations.each do |personalization|
60
+ personalization['to'].each do |to|
61
+ @logger.info("Calling deleteBounce API, #{to}")
62
+ # success => 204, not_found => 404
63
+ sendgrid_client.suppression.bounces._(to['email']).delete
64
+ @logger.info('End calling deleteBounce API')
65
+ end
66
+ end
67
+ end
68
+
47
69
  def with_retry(max_count:, wait_seconds:)
48
70
  tryable_count = max_count + 1
49
71
  begin
50
72
  tryable_count -= 1
51
73
  yield
52
- rescue ::SendGridActionMailerAdapter::ApiClientError => _e
74
+ rescue ::SendGridActionMailerAdapter::ApiClientError => e
75
+ @logger.error(e)
53
76
  raise
54
- rescue => _e
77
+ rescue StandardError => e
55
78
  if tryable_count > 0
79
+ @logger.warn("Retry mail sending, tryable_count: #{tryable_count}")
80
+ @logger.warn(e)
56
81
  sleep(wait_seconds)
57
82
  retry
58
83
  end
84
+ @logger.error('Give up retrying')
85
+ @logger.error(e)
59
86
  raise
60
87
  end
61
88
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SendGridActionMailerAdapter
4
- VERSION = '0.2.2'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sendgrid_actionmailer_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryu39
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-05 00:00:00.000000000 Z
11
+ date: 2019-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sendgrid-ruby
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: bundler
28
+ name: actionmailer
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,49 +53,49 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: ryu39cop
56
+ name: mail
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 0.49.1.0
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 0.49.1.0
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rspec
70
+ name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '3.0'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '3.0'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: webmock
84
+ name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '3.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '3.0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: mail
98
+ name: ryu39cop
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: actionmailer
112
+ name: webmock
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -163,8 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
165
  requirements: []
166
- rubyforge_project:
167
- rubygems_version: 2.7.3
166
+ rubygems_version: 3.0.3
168
167
  signing_key:
169
168
  specification_version: 4
170
169
  summary: A ActionMailer adapter using SendGrid Web API v3