Back

Step by Step Guide to Building a CompanyCam API Integration in Ruby

Aug 15, 20246 minute read

Introduction

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!

Prerequisites

Before we start coding, make sure you've got:

  • Ruby 2.7+ installed
  • The httparty and dotenv gems
  • A CompanyCam API key (grab one from your account settings)

Setting up the project

First 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!

Authentication

CompanyCam uses API keys for authentication. Let's keep it secure:

  1. Create a .env file in your project root
  2. Add your API key: COMPANYCAM_API_KEY=your_api_key_here
  3. In your Ruby file, load the key:
require 'dotenv' Dotenv.load API_KEY = ENV['COMPANYCAM_API_KEY']

Making API requests

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

Core API functionalities

Now that we've got the basics, let's implement some key features:

Retrieving projects

def self.get_project(id) get("/projects/#{id}") end

Uploading photos

def self.upload_photo(project_id, file_path) post("/projects/#{project_id}/photos", body: { photo: File.new(file_path) }, multipart: true ) end

Managing tags

def self.create_tag(name) post('/tags', body: { name: name }) end

Error handling

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

Pagination and rate limiting

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

Webhooks

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

Testing the integration

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

Best practices and optimization

To keep your integration running smoothly:

  1. Cache frequently accessed data
  2. Use background jobs for time-consuming operations
  3. Implement exponential backoff for retries

Conclusion

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! 🚀