Skip to content

Instantly share code, notes, and snippets.

@schmijos
Created June 8, 2016 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmijos/05d2f989c7a5854fe2cd31c666f61c39 to your computer and use it in GitHub Desktop.
Save schmijos/05d2f989c7a5854fe2cd31c666f61c39 to your computer and use it in GitHub Desktop.
Retrieve Sparkpost Bounces as CSV
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