Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save railsfactory-kumaresan/003ebfad3164ccd2dcbb6b2f63483a7c to your computer and use it in GitHub Desktop.
Save railsfactory-kumaresan/003ebfad3164ccd2dcbb6b2f63483a7c to your computer and use it in GitHub Desktop.
How to query LinkedIn People Search by Company Name from Ruby with the LinkedIn Gem
# Call me with linked_in_by_company('Disney')
LINKEDIN = {
api_key: ENV['LINKEDIN_API_KEY'],
secret_key: ENV['LINKEDIN_SECRET_KEY'],
consumers: {
test_person: {
consumer_token: ENV['LINKEDIN_TESTPERSON_CONSUMER_TOKEN'],
consumer_secret: ENV['LINKEDIN_TESTPERSON_CONSUMER_SECRET']
}
}
}
LINKED_IN_PROFILE_COLUMNS = %w(distance id first-name last-name headline location relation-to-viewer current-share summary specialties associations positions phone-numbers im-accounts twitter-accounts primary-twitter-account main-address picture-url site-standard-profile-request)
def linkedin_client
li = LinkedIn::Client.new(LINKEDIN[:api_key], LINKEDIN[:secret_key])
li.authorize_from_access(LINKEDIN[:consumers][:test_person][:consumer_token], LINKEDIN[:consumers][:test_person][:consumer_secret])
li
end
def linked_in_by_company(company_name)
cache_key = "linkedin_company_search-#{company_name}"
# Cache it for a couple days (if desired)
result = Rails.cache.fetch(cache_key, :expires_in => 2.days) do
Rails.logger.info "+++ Cache missed for #{cache_key} +++"
per_page = 25
start = 0
collected_profiles = []
# Loop until LinkedIn doesn't have anything to return
new_profiles = [true]
while !new_profiles.nil?
new_profiles = linkedin_client.search({
company_name: company_name,
count: per_page,
start: start,
sort: 'connections',
fields: ["people:(#{LINKED_IN_PROFILE_COLUMNS.join(',')})"]
}, 'people').people.all
unless new_profiles.nil?
collected_profiles = collected_profiles.concat(new_profiles)
end
start = start + per_page
end
collected_profiles
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment