Hey there, fellow developer! Ready to dive into the world of Adobe Experience Manager (AEM) API integration with Ruby? You're in for a treat. AEM's API is a powerful tool that lets you interact with content, assets, and more. In this guide, we'll walk through creating a robust integration that'll make your AEM-powered projects sing.
Before we jump in, let's make sure you've got your ducks in a row:
Oh, and don't forget to install these gems:
gem install httparty json
First things first – let's get you authenticated. Head over to your AEM instance and grab your API credentials. Got 'em? Great!
Now, let's set up authentication in Ruby:
require 'httparty' require 'base64' class AEMClient include HTTParty base_uri 'https://your-aem-instance.com' def initialize(username, password) @auth = { username: username, password: password } end end
Time to create a client and make sure everything's working:
client = AEMClient.new('your_username', 'your_password') response = client.get('/api/assets.json', basic_auth: @auth) puts response.code puts response.body
If you see a 200 status code, you're golden!
Let's cover the CRUD operations – the bread and butter of any API integration.
def get_page_content(path) get("/api/content#{path}.json", basic_auth: @auth) end
def create_page(parent_path, name, template) post("/api/content#{parent_path}", body: { :cmd => 'createPage', :parentPath => parent_path, :pageName => name, :template => template }, basic_auth: @auth ) end
def update_page_properties(path, properties) post("/api/content#{path}", body: properties, basic_auth: @auth ) end
def delete_page(path) post("/api/content#{path}", body: { :cmd => 'deletePage' }, basic_auth: @auth ) end
AEM's asset management is top-notch. Let's harness that power!
def upload_asset(file_path, destination_path) file = File.new(file_path) post("/api/assets#{destination_path}", body: { file: file }, basic_auth: @auth ) end
def get_asset_metadata(path) get("/api/assets#{path}.json", basic_auth: @auth) end
Want to get fancy with content? Here's how:
def query_content(query) get("/bin/querybuilder.json", query: query, basic_auth: @auth ) end
Don't let errors catch you off guard. Implement some robust error handling:
def api_request yield rescue HTTParty::Error => e logger.error "API request failed: #{e.message}" raise end
Test, test, and test again! Here's a simple RSpec example:
RSpec.describe AEMClient do it "successfully authenticates" do client = AEMClient.new('username', 'password') response = client.get_page_content('/content/mysite') expect(response.code).to eq(200) end end
When deploying, remember:
And there you have it! You're now armed with the knowledge to create a robust AEM API integration in Ruby. Remember, the AEM API is vast and powerful – we've just scratched the surface here. Keep exploring, keep coding, and most importantly, have fun with it!
Got questions? Hit up the Adobe forums or dive into their extensive documentation. Now go forth and create something awesome!