Back

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

Aug 17, 20245 minute read

Introduction

Hey there, fellow Ruby enthusiast! Ready to dive into the world of SafetyCulture API integration? You're in for a treat. This guide will walk you through the process of building a robust integration that'll have you pulling inspection data like a pro in no time.

Prerequisites

Before we jump in, make sure you've got:

  • Ruby 2.7+ installed
  • Bundler for managing gems
  • A SafetyCulture API key (if you don't have one, hop over to their developer portal and snag one)

Setting up the project

Let's kick things off by setting up our project:

mkdir safety_culture_integration cd safety_culture_integration bundle init

Now, crack open that Gemfile and add these lines:

gem 'httparty' gem 'dotenv'

Run bundle install, and you're good to go!

Authentication

First things first, let's keep that API key safe. Create a .env file in your project root:

SAFETY_CULTURE_API_KEY=your_api_key_here

Now, let's create our API client:

require 'httparty' require 'dotenv/load' class SafetyCultureClient include HTTParty base_uri 'https://api.safetyculture.io' headers 'Authorization' => "Bearer #{ENV['SAFETY_CULTURE_API_KEY']}" end

Basic API Operations

Time to get our hands dirty with some API calls:

client = SafetyCultureClient.new # Fetch inspections inspections = client.get('/inspections') # Get inspection details inspection_id = inspections['inspections'].first['inspection_id'] details = client.get("/inspections/#{inspection_id}") # Download inspection report report = client.get("/inspections/#{inspection_id}/report") File.write("inspection_#{inspection_id}.pdf", report.body)

Advanced Operations

Feeling adventurous? Let's tackle some advanced operations:

# Create a template template = client.post('/templates', body: { name: 'New Template', description: 'Created via API' }.to_json) # Schedule an inspection scheduled = client.post('/inspections/scheduled', body: { template_id: template['template_id'], scheduled_for: (Time.now + 86400).iso8601 }.to_json) # Manage users users = client.get('/users')

Error Handling and Rate Limiting

Let's add some resilience to our integration:

def api_call(method, endpoint, options = {}) retries = 0 begin response = self.class.send(method, endpoint, options) raise "API Error: #{response.code}" unless response.success? response rescue => e retries += 1 if retries < 3 sleep(2 ** retries) retry else raise e end end end

Testing the Integration

Don't forget to test! Here's a quick example using RSpec:

RSpec.describe SafetyCultureClient do let(:client) { SafetyCultureClient.new } it 'fetches inspections' do VCR.use_cassette('inspections') do inspections = client.get('/inspections') expect(inspections['inspections']).to be_an(Array) end end end

Best Practices

Remember to:

  • Cache responses when appropriate
  • Use pagination for large datasets
  • Keep your API key secure (never commit it to version control!)

Conclusion

And there you have it! You've just built a solid SafetyCulture API integration in Ruby. From basic operations to advanced features, you're now equipped to harness the full power of SafetyCulture in your Ruby projects.

Keep exploring the API docs for more features, and happy coding!