Hey there, fellow developer! Ready to dive into the world of Housecall Pro API integration? You're in for a treat. This guide will walk you through building a robust integration using Ruby. We'll cover everything from setup to advanced features, so buckle up!
Before we jump in, make sure you've got:
Let's get this show on the road:
mkdir housecall_pro_integration cd housecall_pro_integration bundle init
Now, open up that Gemfile
and add these gems:
gem 'faraday' gem 'dotenv'
Run bundle install
, and we're off to the races!
Housecall Pro uses OAuth 2.0. Here's how to implement it:
require 'faraday' require 'dotenv/load' client_id = ENV['HOUSECALL_CLIENT_ID'] client_secret = ENV['HOUSECALL_CLIENT_SECRET'] conn = Faraday.new(url: 'https://api.housecallpro.com') response = conn.post('/oauth/token') do |req| req.body = { grant_type: 'client_credentials', client_id: client_id, client_secret: client_secret } end token = JSON.parse(response.body)['access_token']
Now that we've got our token, let's make some requests:
api = Faraday.new( url: 'https://api.housecallpro.com', headers: { 'Authorization' => "Bearer #{token}", 'Content-Type' => 'application/json' } ) # Fetch customers customers = api.get('/v1/customers').body
Let's implement some key features:
# Create a job job = api.post('/v1/jobs', JSON.generate({ customer_id: '123', scheduled_at: Time.now.iso8601 })).body # Update job status api.patch("/v1/jobs/#{job['id']}", JSON.generate({ status: 'completed' })) # Retrieve invoices invoices = api.get('/v1/invoices').body
Always be prepared for the unexpected:
def make_request(method, path, body = nil) retries = 0 begin response = api.send(method, path, body) raise 'Rate limited' if response.status == 429 response.body rescue => e retries += 1 if retries < 3 sleep(2 ** retries) retry else raise e end end end
Parse that JSON and store it if needed:
require 'json' require 'sqlite3' customers = JSON.parse(make_request(:get, '/v1/customers')) db = SQLite3::Database.new 'housecall.db' db.execute("CREATE TABLE IF NOT EXISTS customers (id TEXT, name TEXT)") customers.each do |customer| db.execute("INSERT INTO customers VALUES (?, ?)", [customer['id'], customer['name']]) end
Want to interact with your integration? Here's a quick CLI:
loop do print "Enter command (list, create, quit): " command = gets.chomp case command when 'list' puts JSON.pretty_generate(make_request(:get, '/v1/customers')) when 'create' print "Enter customer name: " name = gets.chomp puts make_request(:post, '/v1/customers', JSON.generate({name: name})) when 'quit' break else puts "Unknown command" end end
Don't forget to test! Here's a simple example using RSpec:
require 'rspec' RSpec.describe 'Housecall Pro Integration' do it 'fetches customers successfully' do customers = make_request(:get, '/v1/customers') expect(customers).to be_an(Array) expect(customers.first).to have_key('id') end end
To keep things speedy:
And there you have it! You've just built a solid Housecall Pro API integration in Ruby. Remember, this is just the beginning - there's always room to expand and improve. Keep exploring the API docs, and happy coding!