Created
June 8, 2016 16:04
-
-
Save schmijos/05d2f989c7a5854fe2cd31c666f61c39 to your computer and use it in GitHub Desktop.
Retrieve Sparkpost Bounces as CSV
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
require 'net/http' | |
require 'json' | |
require 'csv' | |
uri = URI('https://api.sparkpost.com/api/v1/message-events?events=bounce,out_of_band') | |
req = Net::HTTP::Get.new(uri) | |
req['Content-Type'] = 'application/json' | |
req['Authorization'] = ENV['API_KEY'] || raise('please provide API_KEY env variable') | |
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https| | |
https.request(req) | |
end | |
bounces = JSON.parse(res.body)['results'] | |
puts "#{bounces.count} bounces found" | |
CSV.open("bounces.csv", "wb") do |csv| | |
csv << %w(Timestamp Recipient Reason) | |
bounces.each do |bounce| | |
csv << [bounce['timestamp'], bounce['rcpt_to'], bounce['reason']] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment