Class: Nuntius::Message
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Nuntius::Message
- Includes:
- Concerns::MetadataScoped
- Defined in:
- app/models/nuntius/message.rb
Overview
Stores individual messages to individual recipients
Nuntius will have messages in states:
pending - nothing done yet
sent - we've sent it on to the provider
delivered - have delivery confirmation
undelivered - have confirmation of non-delivery
Not all transports may provide all states
Instance Method Summary collapse
- #add_attachment(options) ⇒ Object
-
#cleanup! ⇒ Object
Removes only pending child messages.
- #cleanup_attachments ⇒ Object
- #clicked? ⇒ Boolean
-
#deliver_as(transport) ⇒ Object
Convenience method to easily send messages without having a template.
- #delivered_or_blocked? ⇒ Boolean
- #link_tracking_enabled? ⇒ Boolean
- #nuntius_provider(message) ⇒ Object
- #open_tracking_enabled? ⇒ Boolean
- #opened? ⇒ Boolean
- #resend ⇒ Object
Instance Method Details
#add_attachment(options) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/models/nuntius/message.rb', line 87 def () = {} uri = [:url] && URI.parse([:url]) if uri&.scheme == "file" # FIXME: This is a possible security problem [:io] = File.open(uri.path) elsif uri client = Faraday.new do |builder| builder.response :follow_redirects builder.adapter Faraday.default_adapter end response = client.get([:url]) content_disposition = response.headers["Content-Disposition"] || "" [:filename] ||= content_disposition[/filename="([^"]+)"/, 1] [:content_type] = response.headers["Content-Type"] [:io] = if response.body.is_a? String StringIO.new(response.body) else # Assume IO object response.body end elsif [:content].respond_to?(:read) [:content_type] = [:content_type] [:io] = [:content] else raise "Cannot add attachment without url or content" end # Set the filename [:filename] = [:filename] || uri.path.split("/").last || "attachment" # (Try to) add file extension if it is missing file_extension = File.extname([:filename]).delete(".") [:filename] += ".#{Mime::Type.lookup([:content_type].split(";").first).to_sym}" if file_extension.blank? && [:content_type] # Fix content type if file extension known but content type blank [:content_type] ||= Mime::Type.lookup_by_extension(file_extension)&.to_s if file_extension if [:auto_zip] && [:io].size > 1024 * 1024 zip_stream = Zip::OutputStream.write_buffer do |zio| zio.put_next_entry [:file_name] zio.write [:io].read end [:content_type] = "application/zip" [:io] = zip_stream end = Nuntius::Attachment.new .content.attach(io: [:io], filename: [:filename], content_type: [:content_type]) .push() rescue => e Nuntius.config.logger.error "Message: Could not attach #{[:filename]} #{e.}" end |
#cleanup! ⇒ Object
Removes only pending child messages
83 84 85 |
# File 'app/models/nuntius/message.rb', line 83 def cleanup! Nuntius::Message.where(state: "pending").where(parent_message: self).destroy_all end |
#cleanup_attachments ⇒ Object
148 149 150 151 152 |
# File 'app/models/nuntius/message.rb', line 148 def .each do || .destroy if ..where.not(id: id).blank? end end |
#clicked? ⇒ Boolean
78 79 80 |
# File 'app/models/nuntius/message.rb', line 78 def clicked? clicked_at.present? end |
#deliver_as(transport) ⇒ Object
Convenience method to easily send messages without having a template
170 171 172 173 174 |
# File 'app/models/nuntius/message.rb', line 170 def deliver_as(transport) klass = BaseTransport.class_from_name(transport).new klass.deliver(self) self end |
#delivered_or_blocked? ⇒ Boolean
70 71 72 |
# File 'app/models/nuntius/message.rb', line 70 def delivered_or_blocked? delivered? || blocked? end |
#link_tracking_enabled? ⇒ Boolean
176 177 178 |
# File 'app/models/nuntius/message.rb', line 176 def link_tracking_enabled? template&.link_tracking? || campaign&.link_tracking? end |
#nuntius_provider(message) ⇒ Object
154 155 156 157 158 |
# File 'app/models/nuntius/message.rb', line 154 def nuntius_provider() klass = Nuntius::BaseProvider.class_from_name(provider, transport) klass ||= Nuntius::BaseProvider klass.new() end |
#open_tracking_enabled? ⇒ Boolean
180 181 182 |
# File 'app/models/nuntius/message.rb', line 180 def open_tracking_enabled? template&.open_tracking? || campaign&.open_tracking? end |
#opened? ⇒ Boolean
74 75 76 |
# File 'app/models/nuntius/message.rb', line 74 def opened? opened_at.present? end |
#resend ⇒ Object
160 161 162 163 164 165 |
# File 'app/models/nuntius/message.rb', line 160 def resend return if pending? return unless transport deliver_as(transport) end |