Hey there, fellow developer! Ready to supercharge your productivity with Todoist? Let's dive into building a Ruby integration for the Todoist API. This powerhouse of task management is about to become your new best friend.
Before we jump in, make sure you've got:
httparty
gem (for making API requests a breeze)First things first, let's get our project off the ground:
mkdir todoist_integration cd todoist_integration bundle init
Now, add this line to your Gemfile:
gem 'httparty'
And run:
bundle install
Time to get cozy with the Todoist API. Create a new file called todoist_client.rb
:
require 'httparty' class TodoistClient include HTTParty base_uri 'https://api.todoist.com/rest/v2' def initialize(token) @options = { headers: { 'Authorization' => "Bearer #{token}" } } end # We'll add more methods here soon! end
Let's add some meat to our client:
class TodoistClient # ... previous code ... def fetch_tasks self.class.get('/tasks', @options) end def create_task(content) self.class.post('/tasks', @options.merge(body: { content: content })) end def update_task(task_id, updates) self.class.post("/tasks/#{task_id}", @options.merge(body: updates)) end def delete_task(task_id) self.class.delete("/tasks/#{task_id}", @options) end end
Want to level up? Let's add project and label management:
class TodoistClient # ... previous code ... def fetch_projects self.class.get('/projects', @options) end def create_project(name) self.class.post('/projects', @options.merge(body: { name: name })) end def fetch_labels self.class.get('/labels', @options) end def create_label(name) self.class.post('/labels', @options.merge(body: { name: name })) end end
Let's not let those pesky errors catch us off guard:
class TodoistClient # ... previous code ... def handle_response response = yield case response.code when 200..299 response when 429 raise 'Rate limit exceeded. Take a breather!' else raise "API error: #{response.code} - #{response.message}" end end def fetch_tasks handle_response { self.class.get('/tasks', @options) } end # Apply this pattern to other methods end
Don't forget to test! Here's a quick example using RSpec:
require 'rspec' require_relative 'todoist_client' RSpec.describe TodoistClient do let(:client) { TodoistClient.new('fake_token') } it 'fetches tasks' do stub_request(:get, 'https://api.todoist.com/rest/v2/tasks') .to_return(status: 200, body: '[{"id": 1, "content": "Test task"}]') tasks = client.fetch_tasks expect(tasks.first['content']).to eq('Test task') end end
And there you have it! You've just built a robust Todoist API integration in Ruby. The world of automated task management is now at your fingertips. What will you build next?
Now go forth and conquer those tasks! Happy coding!