Hey there, fellow Ruby enthusiast! Ready to supercharge your email validation game? Let's dive into integrating NeverBounce's powerful API into your Ruby project. We'll be using the neverbounce-api
gem, which makes our lives a whole lot easier. Buckle up!
Before we jump in, make sure you've got:
First things first, let's get that gem installed:
gem install neverbounce-api
Easy peasy, right?
Now, let's get our NeverBounce client up and running:
require 'neverbounce' client = NeverBounce::API::Client.new(api_key: 'your_api_key_here')
Pro tip: Keep that API key safe! Consider using environment variables to store it.
Let's start with verifying a single email:
result = client.single_check(email: '[email protected]') puts result.status
Got a bunch of emails? No sweat:
job = client.jobs_create(input: ['[email protected]', '[email protected]']) puts job.id
Keep tabs on your bulk job:
status = client.jobs_status(job_id: job.id) puts status.job_status
Once the job's done, grab those results:
results = client.jobs_results(job_id: job.id) results.results.each do |result| puts "#{result.email}: #{result.verification.result}" end
NeverBounce has your back with built-in rate limiting. The gem will automatically retry if you hit the limit. Cool, huh?
Nobody's perfect, so let's catch those errors:
begin result = client.single_check(email: '[email protected]') rescue NeverBounce::API::AuthError => e puts "Oops! Check your API key: #{e.message}" rescue NeverBounce::API::GeneralError => e puts "Something went wrong: #{e.message}" end
post '/register' do email = params[:email] result = client.single_check(email: email) if result.status == 'valid' # Proceed with registration else # Show error message end end
emails = ['[email protected]', '[email protected]', '[email protected]'] job = client.jobs_create(input: emails) # Wait for job to complete (you might want to use a background job for this) loop do status = client.jobs_status(job_id: job.id) break if status.job_status == 'complete' sleep 5 end results = client.jobs_results(job_id: job.id) valid_emails = results.results.select { |r| r.verification.result == 'valid' }.map(&:email)
And there you have it! You're now equipped to integrate NeverBounce into your Ruby projects like a pro. Remember, clean email lists lead to better deliverability and happier users. Now go forth and validate those emails!
Need more info? Check out the NeverBounce API docs and the neverbounce-api gem documentation. Happy coding!