Back

Step by Step Guide to Building a Tilda Publishing API Integration in Ruby

Aug 18, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Tilda Publishing API integration? Great! We're going to walk through building a sleek Ruby integration that'll have you manipulating Tilda projects like a pro. This guide assumes you're already familiar with Ruby and API basics, so we'll keep things snappy and focus on the good stuff.

Prerequisites

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

  • A Ruby environment (2.7+ recommended)
  • Bundler installed
  • Your Tilda API credentials handy

Setting up the project

Let's kick things off by creating a new Ruby project:

mkdir tilda_integration && cd tilda_integration bundle init

Now, let's add the gems we'll need. Pop open your Gemfile and add:

gem 'httparty' gem 'dotenv'

Run bundle install, and we're ready to rock!

Authentication

Tilda uses API key authentication. Create a .env file in your project root:

TILDA_API_KEY=your_api_key_here

Now, let's set up a basic client:

require 'httparty' require 'dotenv/load' class TildaClient include HTTParty base_uri 'https://api.tildacdn.info/v1' def initialize @options = { query: { publickey: ENV['TILDA_API_KEY'] } } end # We'll add methods here soon! end

Making API requests

Let's add a method to fetch projects:

def get_projects self.class.get("/getprojectslist", @options) end

Easy peasy! Now you can use it like this:

client = TildaClient.new projects = client.get_projects puts projects

Core API functionalities

Let's add methods for pages and content:

def get_pages(project_id) self.class.get("/getpageslist", @options.merge(query: { projectid: project_id })) end def get_page_full(page_id) self.class.get("/getpagefull", @options.merge(query: { pageid: page_id })) end def update_page(page_id, html) self.class.post("/updatepage", @options.merge(body: { pageid: page_id, html: html })) end

Error handling and rate limiting

Let's add some basic error handling:

def handle_response(response) case response.code when 200 JSON.parse(response.body) when 429 raise "Rate limit exceeded. Try again later." else raise "Error: #{response.code} - #{response.message}" end end

Now update our methods to use this:

def get_projects handle_response(self.class.get("/getprojectslist", @options)) end

Data processing and storage

For this example, let's just save to a JSON file:

require 'json' def save_projects(projects) File.write('projects.json', JSON.pretty_generate(projects)) end

Building a simple CLI tool

Let's create a basic CLI:

#!/usr/bin/env ruby require_relative 'tilda_client' client = TildaClient.new case ARGV[0] when 'projects' projects = client.get_projects client.save_projects(projects) puts "Projects saved to projects.json" when 'pages' pages = client.get_pages(ARGV[1]) puts pages else puts "Unknown command" end

Testing the integration

Here's a simple test using Minitest:

require 'minitest/autorun' require_relative 'tilda_client' class TestTildaClient < Minitest::Test def setup @client = TildaClient.new end def test_get_projects projects = @client.get_projects assert_kind_of Array, projects refute_empty projects end end

Best practices and optimization

  • Keep your API key secure using environment variables
  • Use meaningful method names
  • Handle rate limiting gracefully
  • Cache responses when appropriate to reduce API calls

Conclusion

And there you have it! You've just built a solid foundation for a Tilda Publishing API integration in Ruby. From here, you can expand on this base, add more complex operations, or even build a full-fledged gem.

Remember, the key to great API integration is understanding the API's quirks and limitations. Don't be afraid to dive into the Tilda API docs for more advanced features.

Now go forth and create some awesome Tilda integrations! Happy coding!