Skip to content

Instantly share code, notes, and snippets.

@SethCalkins
Forked from sdball/Gemfile
Last active August 29, 2015 14:10
Show Gist options
  • Save SethCalkins/a119e9df68fe796b100b to your computer and use it in GitHub Desktop.
Save SethCalkins/a119e9df68fe796b100b to your computer and use it in GitHub Desktop.
# Ruby 1.8.7
source "https://rubygems.org"
gem "json_pure"
group :test do
gem "rspec"
gem "vcr"
gem "webmock"
end
require 'open-uri'
require 'json'
class JigsawAPI
def self.contacts_for_company(company_name, levels=nil)
done = false
offset=0
contacts = {}
until done do
response = search_contact(company_name, offset, levels)
json = JSON.parse(response)
json['contacts'].each do |contact|
contacts[contact['contactId']] = contact
end
offset += 500
done = (contacts.count >= json['totalHits'])
end
contacts.values
end
def self.search_contact(company_name, offset, levels)
url = "https://sandbox.jigsaw.com/rest/searchContact.json?token=u9o2zjmpk4vy&companyName=#{company_name}&offset=#{offset}"
url << "&levels=#{levels}" if levels
open(url).read
end
end
require 'jigsaw_api'
require 'vcr'
VCR.configure do |c|
c.cassette_library_dir = 'fixtures/vcr_cassettes'
c.hook_into :webmock
end
describe JigsawAPI do
it "fetches contacts for Hershey" do
VCR.use_cassette('hershey-contacts') do
JigsawAPI.contacts_for_company("Hershey").count.should == 479
end
end
it "fetches C-Level contacts for Hershey" do
VCR.use_cassette('hershey-c-level') do
JigsawAPI.contacts_for_company("Hershey", "C-Level").count.should == 12
end
end
it "fetches contacts for Oracle" do
VCR.use_cassette('oracle-contacts') do
JigsawAPI.contacts_for_company("Oracle").count.should == 8995
end
end
it "fetched contacts have data" do
VCR.use_cassette('hershey-contacts') do
contacts = JigsawAPI.contacts_for_company("Hershey")
%w(email firstname lastname companyName).each do |field|
contacts.first.keys.should include field
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment