class RenoteDac::Mailer::Client
Public Class Methods
new()
click to toggle source
# File lib/renote_dac/mailer.rb, line 50 def initialize @client = Postmark::ApiClient.new( RenoteDac.configuration.postmark_api_key, http_read_timeout: 20, http_open_timeout: 10, secure: true ) end
Public Instance Methods
process_batch(size = 100)
click to toggle source
# File lib/renote_dac/mailer.rb, line 59 def process_batch(size = 100) batch = Mailer.get_batch(size) return 0 if batch.empty? ids = [] messages = [] batch.each do |pending| template = pending[BATCH_TEMPLATE] params = pending[BATCH_PARAMS] meta = Mailer.get_template_meta(template) file_attachment_params = pending[BATCH_ATTACHMENT_PARAMS] ids << pending[BATCH_ID] messages << { from: RenoteDac.configuration.from_address, to: pending[BATCH_EMAIL], template_id: meta[:template_id], template_model: params, tag: template, attachments: file_attachment_params } end if Rails.env.production? responses = @client.deliver_in_batches(messages) handle_resp(ids, responses) else messages.each do |message| puts "Sending message... #{message[:to]}" end # Dummy sending RenoteDac::Email.where(id: ids).update_all(error: 0) # Live sending # # if you must send messages for real in development to make sure the # templates look good, do it with care, because this uses the same # server as in production. Don't send 5,000 emails on accident or # Sidney will personally find you. # # responses = @client.deliver_in_batches(messages) # puts responses.inspect # handle_resp(ids, responses) end batch.size end
Private Instance Methods
handle_resp(ids, responses)
click to toggle source
# File lib/renote_dac/mailer.rb, line 111 def handle_resp(ids, responses) raise 'Postmark response size mis-match' if ids.size != responses.size # http://developer.postmarkapp.com/developer-api-overview.html#error-codes ActiveRecord::Base.transaction do responses.each_with_index do |response, index| RenoteDac::Email.where(id: ids[index]).update_all(error: response[:error_code], sent_at: Time.now.utc) end end end