class Agents::MailgunAgent
Public Instance Methods
check()
click to toggle source
# File lib/huginn_mailgun_agent/mailgun_agent.rb, line 89 def check handle(interpolated) end
default_options()
click to toggle source
# File lib/huginn_mailgun_agent/mailgun_agent.rb, line 51 def default_options { 'connection_url' => 'mysql2://user:pass@localhost/database', 'sql' => 'select email, name from emails order by id desc', 'merge_event' => 'false', 'mailgun_apikey' => '', 'from_address' => 'test@example.com', 'testing_mode' => 'false', 'mailgun_domain' => '', 'mailgun_tracking' => 'false', } end
receive(incoming_events)
click to toggle source
# File lib/huginn_mailgun_agent/mailgun_agent.rb, line 93 def receive(incoming_events) incoming_events.each do |event| handle(interpolated(event), event.payload) end end
validate_options()
click to toggle source
# File lib/huginn_mailgun_agent/mailgun_agent.rb, line 74 def validate_options if options['merge_event'].present? && !%[true false].include?(options['merge_event'].to_s) errors.add(:base, "Oh no!!! if provided, merge_event must be 'true' or 'false'") end errors.add(:base, "Mailgun API Key Missing") unless options['mailgun_apikey'].present? errors.add(:base, "Missing From Address") unless options['from_address'].present? errors.add(:base, "Missing Mailgun Domain") unless options['mailgun_domain'].present? end
working?()
click to toggle source
# File lib/huginn_mailgun_agent/mailgun_agent.rb, line 85 def working? !recent_error_logs? end
Private Instance Methods
handle(opts, event = Event.new)
click to toggle source
# File lib/huginn_mailgun_agent/mailgun_agent.rb, line 101 def handle(opts, event = Event.new) t1 = Time.now connection_url = opts["connection_url"] sql = opts["sql"] begin conn = Mysql2AgentConnection.establish_connection(connection_url).connection mg_client = Mailgun::Client.new(opts['mailgun_apikey']) bm_obj = Mailgun::BatchMessage.new(mg_client, opts['mailgun_domain']) bm_obj.test_mode(opts['testing_mode']) bm_obj.track_opens(opts['mailgun_tracking']) bm_obj.track_clicks(opts['mailgun_tracking']) bm_obj.subject(event['subject']) if event['msg-tag'].respond_to?('each') event['msg-tag'].each do | tag | bm_obj.add_tag(tag) end else bm_obj.add_tag(event['msg-tag']) end if event['from_address'].present? bm_obj.from(event['from_address']) else bm_obj.from(opts['from_address']) end bm_obj.body_html(event['message']) results = conn.exec_query(sql) results.each do |row| toaddr = row['name'] + " <" + row['email'] + ">" log("Sending to #{toaddr}") bm_obj.add_recipient(:to, toaddr) end if results.present? conn.close end message_ids = bm_obj.finalize if (opts['merge_event'] == 'true') event['message_ids'] = message_ids create_event payload: event else create_event payload: message_ids end log("Time: #{(Time.now - t1).round(2)}s, Sent: #{results.length if results.present?}, #{pp message_ids}") rescue => error error "Error connection: #{error.inspect} - #{error.backtrace}" return end end