Skip to content

Instantly share code, notes, and snippets.

@dangerouse
Created March 4, 2015 18:02
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 dangerouse/30c43bc94fff0b116dad to your computer and use it in GitHub Desktop.
Save dangerouse/30c43bc94fff0b116dad to your computer and use it in GitHub Desktop.
Basic CloudSponge API import program
#!/usr/bin/env ruby
require "httparty" # Yes, I do like to party
# constants
host = "https://api.cloudsponge.com"
debug = 1
# data to send to the server
service = ARGV[0] || "gmail"
body = "service=#{service}"
domain_key = "COMPLETE ME"
password = "COMPLETE ME TOO"
authorization = {
:username => domain_key,
:password => password,
}
resp = HTTParty.post("#{host}/begin_import/user_consent.json", {
:body => body,
:basic_auth => authorization,
}).parsed_response
puts resp if debug
import_id = resp["import_id"].to_i
# display the consent url
`open '#{resp["url"]}'`
# and then check for events
loop {
# be nice to the server and wait before asking for new data
sleep 2
events = HTTParty.get("#{host}/events/#{import_id}.json",
:basic_auth => authorization).parsed_response["events"]
puts events if debug
# Stop in the event of errors
if events.detect{ |e| e["status"] == "error" }
puts e
break
end
if events.detect{ |e| [e["event_type"], e["status"]] == ["COMPLETE", "COMPLETED"] }
puts HTTParty.get("#{host}/contacts/#{import_id}.json",
:basic_auth => authorization).parsed_response
break
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment