Hey there, fellow dev! Ready to supercharge your Confluence experience with some Ruby magic? Let's dive into building a Confluence API integration using the nifty confluence-rest-api
package. This guide will have you up and running in no time, automating your Confluence tasks like a pro.
Before we jump in, make sure you've got:
First things first, let's get that gem installed:
gem install confluence-rest-api
Easy peasy, right?
Now, let's get you authenticated and ready to roll:
require 'confluence-rest-api' client = ConfluenceRestApi::Client.new( base_url: 'https://your-domain.atlassian.net/wiki', username: '[email protected]', api_token: 'your-api-token' )
Boom! You're in.
Let's start with some bread-and-butter operations:
spaces = client.spaces.all spaces.each { |space| puts space.name }
pages = client.content.all(space_key: 'YOURSPACE', type: 'page') pages.each { |page| puts page.title }
new_page = client.content.create( type: 'page', title: 'My Awesome New Page', space: { key: 'YOURSPACE' }, body: { storage: { value: '<p>Hello, Confluence!</p>', representation: 'storage' } } )
client.content.update( id: 'page_id', type: 'page', title: 'Updated Page Title', body: { storage: { value: '<p>Updated content</p>', representation: 'storage' } } )
Ready to level up? Let's tackle some cooler stuff:
client.content.attach_file( id: 'page_id', file: File.new('/path/to/file.pdf') )
client.content.restrict_page( id: 'page_id', restrictions: { user: ['username1', 'username2'], group: ['group1', 'group2'] } )
results = client.search.search('your search query') results.each { |result| puts result.title }
Always wrap your API calls in error handling:
begin # Your API call here rescue ConfluenceRestApi::Error => e puts "Oops! #{e.message}" end
And remember, be nice to the API - use rate limiting and pagination when dealing with large datasets.
Here's a quick snippet to get you started on a content syncing tool:
def sync_content(source_space, target_space) source_pages = client.content.all(space_key: source_space, type: 'page') source_pages.each do |page| existing_page = client.content.find_by_title(target_space, page.title) if existing_page # Update existing page client.content.update(id: existing_page.id, type: 'page', title: page.title, body: page.body) else # Create new page client.content.create(type: 'page', title: page.title, space: { key: target_space }, body: page.body) end end end
To keep things speedy:
And there you have it! You're now equipped to build some seriously cool Confluence integrations with Ruby. Remember, the Confluence API is vast, so don't be afraid to explore and experiment. Happy coding, and may your wikis always be in sync!
For more details, check out the confluence-rest-api documentation and the official Confluence API docs.
Now go forth and automate all the things! 🚀