Hey there, fellow Ruby enthusiast! Ready to supercharge your app with some AI goodness? Let's dive into integrating the Seamless AI API into your Ruby project. This powerful API will help you find contacts, enrich lead data, and gather company information like a pro. Buckle up, because we're about to make your Ruby app a whole lot smarter!
Before we jump in, make sure you've got:
Let's kick things off by creating a new Ruby project:
mkdir seamless_ai_integration cd seamless_ai_integration bundle init
Now, let's add the gems we'll need. Open up your Gemfile
and add:
gem 'faraday' gem 'json'
Run bundle install
, and we're ready to rock!
Time to set up our API client. Create a new file called seamless_ai_client.rb
:
require 'faraday' require 'json' class SeamlessAIClient BASE_URL = 'https://api.seamless.ai/v1/' def initialize(api_key) @api_key = api_key @conn = Faraday.new(url: BASE_URL) do |faraday| faraday.headers['Authorization'] = "Bearer #{api_key}" faraday.headers['Content-Type'] = 'application/json' faraday.adapter Faraday.default_adapter end end # We'll add more methods here soon! end
Now for the fun part! Let's add some methods to search for contacts, get company info, and enrich lead data:
class SeamlessAIClient # ... previous code ... def search_contacts(query) response = @conn.get('search/contacts', q: query) JSON.parse(response.body) end def get_company_info(domain) response = @conn.get('company', domain: domain) JSON.parse(response.body) end def enrich_lead(email) response = @conn.get('lead/enrich', email: email) JSON.parse(response.body) end end
Let's add some error handling and respect those rate limits:
class SeamlessAIClient # ... previous code ... def make_request(method, endpoint, params = {}) retries = 0 begin response = @conn.send(method, endpoint, params) handle_rate_limit(response) JSON.parse(response.body) rescue Faraday::ClientError => e retries += 1 retry if retries < 3 raise e end end private def handle_rate_limit(response) if response.status == 429 sleep_time = response.headers['Retry-After'].to_i sleep(sleep_time) end end end
Let's wrap it all up in a nice, easy-to-use class:
class SeamlessAI def initialize(api_key) @client = SeamlessAIClient.new(api_key) end def find_contacts(query) @client.make_request(:get, 'search/contacts', q: query) end def company_info(domain) @client.make_request(:get, 'company', domain: domain) end def enrich_lead(email) @client.make_request(:get, 'lead/enrich', email: email) end end
Don't forget to test! Here's a quick example using RSpec:
require 'rspec' require_relative 'seamless_ai' RSpec.describe SeamlessAI do let(:api_key) { 'your_api_key_here' } let(:seamless_ai) { SeamlessAI.new(api_key) } it 'finds contacts' do VCR.use_cassette('find_contacts') do results = seamless_ai.find_contacts('Ruby developer') expect(results).to be_an(Array) expect(results.first).to have_key('name') end end # Add more tests for other methods end
To take your integration to the next level:
And there you have it! You've just built a slick Seamless AI integration in Ruby. With this power at your fingertips, you can now find contacts, enrich leads, and gather company info like a champ.
Remember, with great power comes great responsibility. Use this integration wisely, and may your Ruby apps be forever awesome!
Now go forth and code, you magnificent Ruby developer!