module ExpressPigeon # Messages # # Sending Transactional emails requires that newsletter templates for these # emails are created prior to sending. Such template can have merge fields, # in a format ${field_name}. This feature allows a high degree of flexibility # for message customization. # # The newsletter to be sent can have a number of merge fields, with data for # merging dynamically provided during a call. class Messages include HTTParty base_uri('https://api.expresspigeon.com/messages') debug_output(nil) def initialize(auth_key) self.class.headers('X-auth-key' => auth_key) end # Sending a single transactional email # # POST https://api.expresspigeon.com/messages # # template_id: newsletter template id to be sent # to: email address to send message to # reply_to: email address tp reply to # from: from name, such as your name or name of your organization # subject: email message subject # merge_fields: values for merge fields # view_online: generates online version of sent message. We will host # this generated message on our servers, default is false # click_tracking: overwrites all URLs in email to point to # http://clicks.expresspigeon.com for click tracking. # Setting it to false will preserve all URLs intact, but # click tracking will not be available, default is true # suppress_address: if true suppresses insertion of sender's physical # address in the email, default is false def send(template_id, to:, reply_to:, from:, subject:, merge_fields: {}, view_online: nil, click_tracking: nil, suppress_address: nil) options = {} options['template_id'] = template_id options['to'] = to options['from'] = from options['reply_to'] = reply_to options['subject'] = subject options['merge_fields'] = merge_fields unless merge_fields.empty? options['view_online'] = view_online unless view_online.nil? options['click_tracking'] = click_tracking unless click_tracking.nil? options['suppress_address'] = suppress_address unless suppress_address.nil? self.class.post( '', body: options.to_json, headers: { 'Content-Type' => 'application/json' } ) end # Report for a single message # # GET https://api.expresspigeon.com/messages/{id} def status(message_id) # NOTE: This appears to be sending a valid request but the response # can contain many status reports. The intent seems to be to # only return the status report for the id requested. We'll pass # along the full response for now. self.class.get('', query: { 'id' => message_id }) end REPORTING_PERIODS = %i(last24hours last_week last_month) # Report for multiple messages # # GET https://api.expresspigeon.com/messages def statuses(from_id: nil, start_date: nil, end_date: nil, period: nil) options = {} options['from_id'] = from_id unless from_id.nil? options['start_date'] = start_date unless start_date.nil? options['end_date'] = end_date unless end_date.nil? options['period'] = period unless period.nil? self.class.get('', query: options) end end end