Hey there, fellow developer! Ready to dive into the world of CompanyCam API integration? You're in for a treat. CompanyCam's API is a powerful tool that lets you tap into their photo-centric project management platform. In this guide, we'll walk through building a solid integration in Ruby. Let's get cracking!
Before we start coding, make sure you've got:
httparty
and dotenv
gemsFirst things first, let's set up our project:
mkdir companycam_integration cd companycam_integration bundle init
Now, add these to your Gemfile:
gem 'httparty' gem 'dotenv'
Run bundle install
, and you're good to go!
CompanyCam uses API keys for authentication. Let's keep it secure:
.env
file in your project rootCOMPANYCAM_API_KEY=your_api_key_here
require 'dotenv' Dotenv.load API_KEY = ENV['COMPANYCAM_API_KEY']
Time to make our first request! We'll use HTTParty to keep things simple:
require 'httparty' class CompanyCamAPI include HTTParty base_uri 'https://api.companycam.com/v2' headers 'Authorization' => "Bearer #{API_KEY}" def self.get_projects get('/projects') end end response = CompanyCamAPI.get_projects puts response.body
Now that we've got the basics, let's implement some key features:
def self.get_project(id) get("/projects/#{id}") end
def self.upload_photo(project_id, file_path) post("/projects/#{project_id}/photos", body: { photo: File.new(file_path) }, multipart: true ) end
def self.create_tag(name) post('/tags', body: { name: name }) end
Always expect the unexpected! Let's add some error handling:
def self.make_request(method, endpoint, options = {}) response = send(method, endpoint, options) case response.code when 200..299 response when 401 raise "Unauthorized: Check your API key" when 404 raise "Not found: #{endpoint}" else raise "Error #{response.code}: #{response.message}" end end
CompanyCam uses cursor-based pagination. Here's how to handle it:
def self.get_all_projects projects = [] cursor = nil loop do response = get('/projects', query: { cursor: cursor }) projects += response['projects'] cursor = response['meta']['next_cursor'] break if cursor.nil? sleep(1) # Respect rate limits end projects end
If you're using webhooks, here's a quick Sinatra endpoint to get you started:
require 'sinatra' post '/webhook' do payload = JSON.parse(request.body.read) # Process the webhook payload status 200 end
Don't forget to test! Here's a simple RSpec example:
RSpec.describe CompanyCamAPI do describe '.get_projects' do it 'returns a list of projects' do VCR.use_cassette('get_projects') do response = CompanyCamAPI.get_projects expect(response.code).to eq(200) expect(response['projects']).to be_an(Array) end end end end
To keep your integration running smoothly:
And there you have it! You've just built a solid CompanyCam API integration in Ruby. Remember, this is just the beginning – there's so much more you can do with the API. Keep exploring, keep coding, and most importantly, have fun with it!
Got questions? Hit up the CompanyCam API docs or reach out to their support team. Now go forth and build something awesome! 🚀