Created
June 1, 2015 22:08
-
-
Save panchew/3e107dde2c09c85b4ce8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PostMark | |
class API | |
include HTTParty | |
base_uri 'https://api.postmarkapp.com' | |
attr_accessor :token | |
# X-Postmark-Server-Token | |
def initialize(token) | |
@auth = { "X-Postmark-Server-Token" => token } | |
end | |
def get_stats | |
self.class.get("/delivery_stats", options) | |
rescue | |
nil | |
end | |
def get_bounce_list(bounce_type = nil) | |
url = "/bounces" | |
url << "?type=#{bounce_type}" if bounce_type | |
self.class.get(url, options) | |
rescue | |
nil | |
end | |
def activate_bounce(message_id) | |
self.class.put("/bounces/#{message_id}/activate", options) | |
end | |
private | |
def options | |
{ headers: @auth } | |
end | |
end | |
class BounceHandler | |
def initialize | |
@api_client = PostMark::API.new(ENV["POSTMARK_SERVER_TOKEN"]) | |
end | |
def process | |
bounces = @api_client.get_stats['Bounces'] | |
if bounces | |
bounces.each do |bounce_type| | |
if bounce_type['Count'] > 0 | |
bounce_list = @api_client.get_bounce_list(bounce_type['Type'])['Bounces'] | |
if bounce_list | |
bounce_list.each do |message| | |
Bounce.create do |new_bounce| | |
new_bounce.bounce_type = bounce_type['Type'] | |
new_bounce.bounce_id = message['ID'] | |
new_bounce.message_id = message['MessageID'] | |
new_bounce.bounced_at = message['BouncedAt'] | |
new_bounce.can_activate = message['CanActivate'] | |
new_bounce.subject = message['Subject'] | |
new_bounce.email = message['Email'] | |
new_bounce.error_details = message['Details'] | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
def reactivate(message_id) | |
@api_client.reactivate message_id | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment