Skip to content

Instantly share code, notes, and snippets.

@wingchen
Last active December 14, 2015 06:39
Show Gist options
  • Save wingchen/5044614 to your computer and use it in GitHub Desktop.
Save wingchen/5044614 to your computer and use it in GitHub Desktop.
Fliptop API Code Example in Ruby. The 202 retry mechanism is built up into FliptopAPIRetryClass. To run this script, simply type 'ruby fliptop_api.rb' into your terminal.
#!/usr/bin/env ruby
require 'net/http'
require 'thread'
require 'rubygems'
require 'json'
FLIPTOP_API_KEY = 'your fliptop api key'
# this queue represent all the email inputs from your database
# please feel free to replace this queue into any data source that
# you might have.
database_emails = Queue.new
database_emails << 'doug@fliptop.com'
database_emails << 'tom@fliptop.com'
database_emails << 'this_does_not_exist_so_202_1@fliptop.com'
# this queue is going to store the emails that needs to be retried
the_202_retry_queue = Queue.new
# get_social_data_via_email
# it handles the actual fliptop API query,
# and different cases according to the returned
# http status code
def get_social_data_via_email(email_item, the_202_retry_queue, format='json')
if email_item['retry_attempt'].to_i == 0
puts 'Looking up: ' + email_item['email']
elsif email_item['retry_attempt'].to_i > 3
puts 'Retry over 3 times, dropping the email: ' + email_item['email']
return
else
puts 'Retrying: ' + email_item['email']
end
uri = URI('http://api.fliptop.com/beta/person?api_key=%s&email=%s&format=%s' % [FLIPTOP_API_KEY, email_item['email'], format])
res = Net::HTTP.get_response(uri)
if res.code.to_i == 200
# 200, successful, priting the data out
# normally you would like to store the returned data into your
# database, or write it into a desinated file
person_obj = JSON.load(res.body)
puts 'Email: ' + person_obj['email']
puts 'Name: ' + person_obj['name']
puts 'Company: ' + person_obj['company']
puts 'Title: ' + person_obj['title']
if person_obj['memberships'].has_key?('facebook')
puts 'facebook: ' + person_obj['memberships']['facebook']
end
if person_obj['memberships'].has_key?('linkedin')
puts 'linkedin: ' + person_obj['memberships']['linkedin']
end
if person_obj['memberships'].has_key?('twitter')
puts 'twitter: ' + person_obj['memberships']['twitter']
end
elsif res.code.to_i == 202
# 202, put it into the_202_retry_queue
the_202_retry_queue << {'email'=> email_item['email'], 'retry_attempt'=>email_item['retry_attempt'] + 1, 'last_try'=> Time.now()}
elsif res.code.to_i == 400
puts 'Incorrect syntax. email: ' + email_item['email']
elsif res.code.to_i == 500
puts 'Unexpected server error. Please email api@fliptop.com. email: ' + email_item['email']
else
puts 'Other error with email: ' + email_item['email'] + ' status code:' + res.code.to_s + ' error message:' + res.body
end
end
# the method to walk through the entire database emails
def FliptopAPIWalkThroughFunc(database_emails, the_202_retry_queue)
while database_emails.length > 0
item = database_emails.deq()
email_item = {'email'=>item, 'retry_attempt'=>0, 'last_try'=>Time.now()}
get_social_data_via_email(email_item, the_202_retry_queue)
end
puts 'FliptopAPIWalkThroughFunc Finished.'
end
# the method to conduct retries
def FliptopAPIRetryFunc(database_emails, the_202_retry_queue)
while database_emails.length > 0 or the_202_retry_queue.length > 0
sleep(30)
while the_202_retry_queue.length > 0
item = the_202_retry_queue.deq()
diff = Time.now() - item['last_try']
if diff > 30
get_social_data_via_email(item, the_202_retry_queue)
else
the_202_retry_queue << item
end
end
end
puts 'FliptopAPIRetryFunc Finished.'
end
# kick off the threads
# threads are used here to increase the performance
# if performance is not a concern here,
# you can just call FliptopAPIRetryFunc after
# FliptopAPIWalkThroughFunc is done, like the last two commented-out lines.
Thread.new{FliptopAPIWalkThroughFunc(database_emails, the_202_retry_queue)}
Thread.new{FliptopAPIRetryFunc(database_emails, the_202_retry_queue)}
#FliptopAPIWalkThroughFunc(database_emails, the_202_retry_queue)
#FliptopAPIRetryFunc(database_emails, the_202_retry_queue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment