Back

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

Aug 18, 20245 minute read

Introduction

Hey there, fellow Ruby enthusiast! Ready to dive into the world of TeamUp API integration? You're in for a treat. TeamUp's API is a powerful tool that'll let you tap into their calendar management system, and we're going to build something cool with it. Let's get cracking!

Prerequisites

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

  • Ruby 2.7+ installed
  • The httparty and json gems
  • Your TeamUp API key (if you don't have one, grab it from your TeamUp account)

Setting up the project

First things first, let's get our project set up:

mkdir teamup_integration cd teamup_integration bundle init

Now, add these lines to your Gemfile:

gem 'httparty' gem 'json'

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

Authentication

Let's create a client class to handle our API calls:

require 'httparty' require 'json' class TeamUpClient include HTTParty base_uri 'https://api.teamup.com' def initialize(api_key) @options = { headers: { 'Teamup-Token' => api_key } } end # We'll add more methods here soon! end

Making API requests

Now, let's add some methods to our client class:

def get_calendars self.class.get('/calendars', @options) end def create_event(calendar_id, event_data) self.class.post("/calendars/#{calendar_id}/events", @options.merge(body: event_data.to_json)) end def update_event(calendar_id, event_id, event_data) self.class.put("/calendars/#{calendar_id}/events/#{event_id}", @options.merge(body: event_data.to_json)) end def delete_event(calendar_id, event_id) self.class.delete("/calendars/#{calendar_id}/events/#{event_id}", @options) end

Handling responses

TeamUp's API returns JSON, so let's parse it and handle any errors:

def handle_response(response) case response.code when 200..299 JSON.parse(response.body) else raise "API error: #{response.code} - #{response.message}" end end

Don't forget to wrap your API calls with this method!

Implementing key features

Let's put it all together:

client = TeamUpClient.new('your_api_key_here') # Fetch calendars calendars = client.handle_response(client.get_calendars) # Create an event new_event = { title: 'Ruby Meetup', start_dt: '2023-06-01T18:00:00', end_dt: '2023-06-01T20:00:00' } created_event = client.handle_response(client.create_event(calendars.first['id'], new_event)) # Update the event updated_event = client.handle_response(client.update_event(calendars.first['id'], created_event['id'], { title: 'Super Ruby Meetup' })) # Delete the event client.handle_response(client.delete_event(calendars.first['id'], created_event['id']))

Optimizing the integration

To keep things smooth:

  1. Respect rate limits (check TeamUp's docs for current limits)
  2. Implement caching for frequently accessed data

Here's a simple caching example:

require 'redis' class CachedTeamUpClient < TeamUpClient def initialize(api_key, redis_url) super(api_key) @cache = Redis.new(url: redis_url) end def get_calendars cached = @cache.get('calendars') return JSON.parse(cached) if cached calendars = handle_response(super) @cache.setex('calendars', 3600, calendars.to_json) calendars end end

Testing the integration

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

RSpec.describe TeamUpClient do let(:client) { TeamUpClient.new('fake_api_key') } it 'fetches calendars' do VCR.use_cassette('calendars') do calendars = client.handle_response(client.get_calendars) expect(calendars).to be_an(Array) expect(calendars.first).to have_key('id') end end # Add more tests for other methods end

Conclusion

And there you have it! You've just built a solid TeamUp API integration in Ruby. You've got the basics down, and you're ready to expand on this foundation. Remember, the API has a lot more to offer, so don't be afraid to explore and build something amazing!

Happy coding, Rubyist! 🚀