Hey there, fellow Ruby enthusiast! Ready to supercharge your time tracking game? Let's dive into building a Clockify API integration. Clockify's API is a powerhouse for managing time entries, projects, and reports. By the end of this guide, you'll have a slick Ruby integration up and running.
Before we jump in, make sure you've got:
Got those? Great! Let's roll.
First things first, let's get our project set up:
mkdir clockify_integration cd clockify_integration bundle init
Now, crack open that Gemfile and add:
gem 'httparty' gem 'dotenv'
Run bundle install
, and we're off to the races.
Clockify uses API keys for authentication. Let's keep it secure:
.env
file in your project root:
CLOCKIFY_API_KEY=your_api_key_here
require 'dotenv/load' require 'httparty' class ClockifyClient include HTTParty base_uri 'https://api.clockify.me/api/v1' headers 'X-Api-Key' => ENV['CLOCKIFY_API_KEY'] end
Boom! You're authenticated and ready to roll.
Let's fetch your workspaces:
response = ClockifyClient.get('/workspaces') workspaces = JSON.parse(response.body) puts workspaces
Now, let's create a time entry:
workspace_id = workspaces.first['id'] payload = { start: Time.now.iso8601, billable: 'true', description: 'Building Clockify API integration' } response = ClockifyClient.post("/workspaces/#{workspace_id}/time-entries", body: payload.to_json)
Always check your responses:
if response.success? puts "Success! #{response.body}" else puts "Oops! #{response.code}: #{response.body}" end
Here's a quick method to fetch user data:
def get_user_data response = ClockifyClient.get('/user') JSON.parse(response.body) end
For projects, time entries, and reports, follow a similar pattern. The Clockify API docs are your best friend here.
If you're feeling adventurous, set up a webhook endpoint:
require 'sinatra' post '/clockify_webhook' do payload = JSON.parse(request.body.read) # Process the webhook data status 200 end
Don't forget to test! Here's a simple RSpec example:
RSpec.describe ClockifyClient do it 'fetches workspaces successfully' do response = ClockifyClient.get('/workspaces') expect(response.code).to eq(200) end end
And there you have it! You've just built a Ruby integration for Clockify. Pretty cool, right? Remember, this is just the tip of the iceberg. The Clockify API has tons more to offer, so keep exploring and building awesome stuff!
Happy coding, and may your time tracking be ever efficient! 🚀