sendgrid_notification 0.1.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5a6289282bff591ae77653ea3172ce498e80c001
4
- data.tar.gz: ff5e384e4a9c50789108192cbb043db1833dd584
2
+ SHA256:
3
+ metadata.gz: e396c43724c97f270821ea8d3e6207e4c646fabcc322622a8c3083d43e92e471
4
+ data.tar.gz: 95233c736ced4094c01141e07fbf30dcaa9644ac356658515934f4df57c7c4eb
5
5
  SHA512:
6
- metadata.gz: abf8bdbfba44a15bf27270f45a2dfc888f08ba41d3ffdd25fe0417885b2ed474df233aacb5d1692258103d585eaef631300a6d4a2a17a713217283781a077d53
7
- data.tar.gz: daa00bc9ebff0b4dd783d0d3e8e8e181ed0278978429268729f224af6eea998612fa7abf345de08e1cb85317e48b5d773ac2db292cc4237a458d189d4acb23c4
6
+ metadata.gz: 644f5639f1a2cef044c0b649f5c034e69035eeff83d4a5be0ff9be6eacdbb6116601d2c3169eab49fe7f3d30591dde9f1115fb16bdf6e6b5557a8af82f231b7e
7
+ data.tar.gz: df37e36a5bdd31f5df56c0478fbb018f3e9fdc9a768a3746d24e24a98573d29ece9ddd9c2180bcc0484b2f045ec0cd044ad0170b676171e85607232d74f0caaf
@@ -1,11 +1,19 @@
1
1
  module SendgridNotification
2
2
  class SendmailJob < ApplicationJob
3
+ class HTTPException < StandardError; end
4
+
3
5
  queue_as :sendmail
4
6
 
5
7
  MailerClass = Rails.application.config.sendgrid_notification.mailer.constantize
6
8
 
7
- def perform(to, notification_mail, params)
8
- MailerClass.new(params).sendmail(to, notification_mail, params)
9
+ def perform(to, notification_mail, params, hash_attachments = [])
10
+
11
+ mailer = MailerClass.new(params)
12
+ obj_attachments = hash_attachments.map{|hash| SendgridNotification::Attachment.wrap(hash.symbolize_keys) }
13
+ mailer.sendmail(to, notification_mail, params, obj_attachments)
14
+ raise HTTPException, mailer.last_result.body unless mailer.last_result_success?
15
+
16
+ mailer.last_result.body
9
17
  end
10
18
  end
11
19
  end
@@ -0,0 +1,37 @@
1
+ require 'sendgrid-ruby'
2
+
3
+ module SendgridNotification
4
+ class Attachment
5
+ include ActiveModel::Model
6
+ include ActiveModel::Attributes # Rails 5.2
7
+
8
+ validates :content_id, :filename, :content, presence: true
9
+
10
+ attribute :content_id, :string
11
+ attribute :filename, :string
12
+ attribute :content, :string
13
+ attribute :mime_type, :string, default: "application/octet-stream"
14
+ attribute :disposition, :string, default: "attachment"
15
+
16
+ alias_method :to_h, :attributes
17
+
18
+ def as_sendgrid
19
+ a = ::SendGrid::Attachment.new
20
+ a.content_id = content_id
21
+ a.filename = filename
22
+ a.content = Base64.strict_encode64(content)
23
+ a.type = mime_type
24
+ a.disposition = disposition
25
+ a
26
+ end
27
+
28
+ def self.wrap(hash_or_attachment)
29
+ case hash_or_attachment
30
+ when Attachment
31
+ self
32
+ else
33
+ Attachment.new(**hash_or_attachment.to_h)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -8,7 +8,12 @@ module SendgridNotification
8
8
  @from_name = _params.fetch(:mail_from_name) { Rails.application.config.sendgrid_notification.mail_from_name }
9
9
  end
10
10
 
11
- def sendmail(to, notification_mail, params = {})
11
+ # Send mail about:
12
+ # +to+ recipent to
13
+ # +notification_mail+ NotificationMail object for mail expression
14
+ # +params+ params for notification_mail
15
+ # +attachments+ attachment in hash or SendgridNotification::Attachment
16
+ def sendmail(to, notification_mail, params, attachments = [])
12
17
  raise NotImplementedError, "you should implement #{self.class}##{__method__}"
13
18
  end
14
19
 
@@ -1,17 +1,32 @@
1
1
  # prepend me
2
2
  module SendgridNotification
3
3
  module MailRecordable
4
- def sendmail(to, notification_mail, params = {})
4
+ def sendmail(to, notification_mail, parmas, attachments = [])
5
5
  result = super
6
6
 
7
- m = MailHistory.create(
7
+ m = MailHistory.new(
8
8
  key: notification_mail.key,
9
9
  to: to,
10
10
  sent_at: Time.now,
11
11
  )
12
+ m.attachment_summary = attachment_summary(attachments) if m.respond_to?(:attachment_summary=)
13
+ m.save
12
14
  Rails.logger.error m.errors.full_messages if m.invalid? # and ignore
13
15
 
14
16
  result
15
17
  end
18
+
19
+ private
20
+
21
+ def attachment_summary(attachments)
22
+ attachments.map do |a|
23
+ {
24
+ content_id: a.content_id,
25
+ mime_type: a.mime_type,
26
+ filename: a.filename,
27
+ size: a.content.bytesize,
28
+ }
29
+ end.to_json
30
+ end
16
31
  end
17
32
  end
@@ -12,15 +12,16 @@ module SendgridNotification
12
12
  VARIABLE_REGEXP_DEFAULT = /\{\{\s*(\w+)\s*\}\}/
13
13
  self.variable_regexp = VARIABLE_REGEXP_DEFAULT
14
14
 
15
-
16
- def sendmail_later(to, params = {})
17
- SendmailJob.perform_later(to, self, params)
15
+ def sendmail_later(to, params, attachments = [])
16
+ hash_attachments = attachments.map(&:to_h)
17
+ SendmailJob.perform_later(to, self, params, hash_attachments)
18
18
  end
19
19
 
20
20
  alias_method :sendmail, :sendmail_later
21
21
 
22
- def sendmail_now(to, params = {})
23
- SendmailJob.perform_now(to, self, params)
22
+ def sendmail_now(to, params, attachments = [])
23
+ hash_attachments = attachments.map(&:to_h)
24
+ SendmailJob.perform_now(to, self, params, hash_attachments)
24
25
  end
25
26
 
26
27
  def apply(params)
@@ -1,14 +1,14 @@
1
1
  module SendgridNotification
2
2
  class NullMailer < BaseMailer
3
3
  prepend MailRecordable
4
- Result = Struct.new(:from, :to, :subject, :body, :sent_at)
4
+ Result = Struct.new(:from, :to, :subject, :body, :sent_at, :attachments)
5
5
 
6
6
  @@results = []
7
7
  cattr_reader :results
8
8
 
9
- def sendmail(to, notification_mail, params = {})
9
+ def sendmail(to, notification_mail, params, attachments = [])
10
10
  body = notification_mail.apply(params)
11
- results << Result.new(from, to, notification_mail.subject, body, Time.now)
11
+ results << Result.new(from, to, notification_mail.subject, body, Time.now, attachments)
12
12
  end
13
13
 
14
14
  def last_result
@@ -50,8 +50,12 @@ module SendgridNotification
50
50
 
51
51
  # TODO: status_code が 500 系およびタイムアウトのときにリトライできるようにする
52
52
  # POST /mail/send
53
- def mail_send(from:, from_name:, to:, subject:, body:)
53
+ def mail_send(from:, from_name:, to:, subject:, body:, attachments: [])
54
54
  mail = build_mail(from, from_name, to, subject, body)
55
+ attachments.each do |a|
56
+ a = SendgridNotification::Attachment.new(a) if a.is_a?(Hash)
57
+ mail.add_attachment(a.as_sendgrid)
58
+ end
55
59
  res = client.mail._('send').post(request_body: mail.to_json)
56
60
  @last_response = Response.new(res)
57
61
  end
@@ -3,9 +3,9 @@ module SendgridNotification
3
3
  prepend MailRecordable
4
4
  class Error < StandardError; end
5
5
 
6
- def sendmail(to, notification_mail, params = {})
6
+ def sendmail(to, notification_mail, params, attachments = [])
7
7
  body = notification_mail.apply(params)
8
- _sendmail(to, notification_mail, body)
8
+ _sendmail(to, notification_mail, body, attachments: attachments)
9
9
  end
10
10
 
11
11
  def last_result
@@ -24,13 +24,14 @@ module SendgridNotification
24
24
  @sendgrid_client ||= SendgridClient.new
25
25
  end
26
26
 
27
- def _sendmail(to, notification_mail, body)
27
+ def _sendmail(to, notification_mail, body, attachments: )
28
28
  sendgrid_client.mail_send(
29
29
  from: from,
30
30
  from_name: from_name,
31
31
  to: to,
32
32
  subject: notification_mail.subject,
33
- body: body
33
+ body: body,
34
+ attachments: attachments,
34
35
  )
35
36
  end
36
37
  end
@@ -17,19 +17,33 @@ module SendgridNotification
17
17
  end
18
18
  end
19
19
 
20
- def self.auto_update
21
- last = SendgridStatusUpdateHistory.last
22
- start_time = last ? last.end_time : 1.hour.ago.to_i
20
+ def self.auto_update(ignore_errors: false)
21
+ last_end_time = SendgridNotification::SendgridStatusUpdateHistory.order(:id).reverse_order.limit(1).pluck(:end_time).first
22
+ start_time = last_end_time || 1.hour.ago.to_i
23
23
  end_time = Time.now.to_i
24
24
 
25
25
  suppressions = update(start_time, end_time)
26
26
 
27
- SendgridStatusUpdateHistory.create!(
28
- start_time: start_time,
29
- end_time: end_time,
30
- count: suppressions.size,
31
- body: suppressions.map(&:to_s).join("\n")
32
- )
27
+ begin
28
+ SendgridStatusUpdateHistory.create!(
29
+ start_time: start_time,
30
+ end_time: end_time,
31
+ count: suppressions.size,
32
+ body: suppressions.map(&:to_s).join("\n")
33
+ )
34
+ rescue => e
35
+ if ignore_errors
36
+ warn "#{e} (ignored)"
37
+ SendgridStatusUpdateHistory.create(
38
+ start_time: start_time,
39
+ end_time: end_time,
40
+ count: suppressions.size,
41
+ body: "" # ignored
42
+ )
43
+ else
44
+ raise
45
+ end
46
+ end
33
47
  end
34
48
 
35
49
  # TODO: 前回の保存日時からの内容を記録
@@ -0,0 +1,14 @@
1
+ class AlterUpdateHistoriesBodyToLongtext < ActiveRecord::Migration[5.1]
2
+ def change
3
+ longtextlimit = 4294967295
4
+ reversible do |dir|
5
+ dir.up do
6
+ change_column :sendgrid_status_update_histories, :body, :text, limit: longtextlimit
7
+ end
8
+
9
+ dir.down do
10
+ change_column :sendgrid_status_update_histories, :body, :text
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ class AlterNotificationBodyToMediumtext < ActiveRecord::Migration[5.1]
2
+ def change
3
+ mediumtext = 16777215
4
+ reversible do |dir|
5
+ dir.up do
6
+ change_column :notification_mails, :content, :text, limit: mediumtext
7
+ end
8
+
9
+ dir.down do
10
+ change_column :notification_mails, :content, :text
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ class AddMailHistoriesIndexes < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_index :mail_histories, :to
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddAttachmentSummaryToMailHistories < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :mail_histories, :attachment_summary, :text
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module SendgridNotification
2
- VERSION = "0.1.2".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -42,7 +42,8 @@ This is test mail.
42
42
 
43
43
  desc 'update status after previous autoupdate'
44
44
  task autoupdate: :environment do
45
- SendgridNotification::SendgridStatus.auto_update
45
+ ignore_errors = !!ENV["IGNORE_ERRORS"]
46
+ SendgridNotification::SendgridStatus.auto_update(ignore_errors: ignore_errors)
46
47
  end
47
48
 
48
49
  desc 'only retrieve suppression statuses (start=datetime end=datetime)'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sendgrid_notification
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HORII Keima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-06 00:00:00.000000000 Z
11
+ date: 2019-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.1'
19
+ version: '5.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.1'
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sendgrid-ruby
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -141,6 +141,7 @@ files:
141
141
  - app/jobs/sendgrid_notification/sendmail_job.rb
142
142
  - app/mailers/sendgrid_notification/application_mailer.rb
143
143
  - app/models/sendgrid_notification/application_record.rb
144
+ - app/models/sendgrid_notification/attachment.rb
144
145
  - app/models/sendgrid_notification/base_mailer.rb
145
146
  - app/models/sendgrid_notification/mail_history.rb
146
147
  - app/models/sendgrid_notification/mail_recordable.rb
@@ -156,6 +157,10 @@ files:
156
157
  - db/migrate/20170208085706_create_notification_mails.rb
157
158
  - db/migrate/20170208113843_create_mail_histories.rb
158
159
  - db/migrate/20170217072256_create_sendgrid_status_update_histories.rb
160
+ - db/migrate/20191120042452_alter_update_histories_body_to_longtext.rb
161
+ - db/migrate/20191120042453_alter_notification_body_to_mediumtext.rb
162
+ - db/migrate/20191120070545_add_mail_histories_indexes.rb
163
+ - db/migrate/20191125125857_add_attachment_summary_to_mail_histories.rb
159
164
  - lib/sendgrid_notification.rb
160
165
  - lib/sendgrid_notification/engine.rb
161
166
  - lib/sendgrid_notification/version.rb
@@ -179,8 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
184
  - !ruby/object:Gem::Version
180
185
  version: '0'
181
186
  requirements: []
182
- rubyforge_project:
183
- rubygems_version: 2.6.13
187
+ rubygems_version: 3.0.3
184
188
  signing_key:
185
189
  specification_version: 4
186
190
  summary: notification mail sender via sendgrid API