-
-
Save SethCalkins/a119e9df68fe796b100b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby 1.8.7 | |
source "https://rubygems.org" | |
gem "json_pure" | |
group :test do | |
gem "rspec" | |
gem "vcr" | |
gem "webmock" | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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