Hey there, fellow developer! Ready to dive into the world of Salesforce Commerce Cloud API integration with Ruby? You're in for a treat. This powerful combo will supercharge your e-commerce projects, giving you the ability to manage products, orders, and customers with ease. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get you authenticated. We'll be using OAuth 2.0, because we're not savages.
require 'oauth2' client = OAuth2::Client.new(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, site: 'https://account.demandware.com') token = client.client_credentials.get_token
Boom! You've got your access token. Guard it with your life (or at least until it expires).
Let's get your project structure sorted. Create a new directory and initialize your Gemfile:
mkdir sfcc_integration && cd sfcc_integration bundle init
Add these gems to your Gemfile:
gem 'oauth2' gem 'faraday' gem 'json'
Run bundle install
and you're good to go!
Time to create our API client. We'll use Faraday because it's awesome.
require 'faraday' require 'json' class SFCCClient BASE_URL = 'https://your-instance.demandware.net/s/Sites-Site/dw/shop/v20_2' def initialize(access_token) @conn = Faraday.new(url: BASE_URL) do |faraday| faraday.headers['Authorization'] = "Bearer #{access_token}" faraday.headers['Content-Type'] = 'application/json' faraday.adapter Faraday.default_adapter end end # We'll add more methods here soon! end
Let's add some methods to interact with products, orders, and customers. Here's a taste:
class SFCCClient # ... previous code ... def get_product(product_id) response = @conn.get("products/#{product_id}") JSON.parse(response.body) end def create_order(order_data) response = @conn.post('orders', order_data.to_json) JSON.parse(response.body) end def get_customer(customer_id) response = @conn.get("customers/#{customer_id}") JSON.parse(response.body) end end
Don't let errors catch you off guard. Let's add some error handling and logging:
class SFCCClient # ... previous code ... private def handle_response(response) case response.status when 200..299 JSON.parse(response.body) else log_error(response) raise "API error: #{response.status} - #{response.body}" end end def log_error(response) # Implement your logging logic here puts "Error: #{response.status} - #{response.body}" end end
You're not done until you've tested it! Here's a quick example using RSpec:
require 'rspec' require_relative 'sfcc_client' RSpec.describe SFCCClient do let(:client) { SFCCClient.new('your_access_token') } it 'fetches a product successfully' do product = client.get_product('some-product-id') expect(product).to have_key('id') expect(product['id']).to eq('some-product-id') end # Add more tests for other endpoints end
Remember to:
And there you have it! You've just built a Salesforce Commerce Cloud API integration in Ruby. Pat yourself on the back, you magnificent code wizard!
Remember, this is just the beginning. There's a whole world of SFCC API endpoints to explore. Keep experimenting, keep building, and most importantly, keep being awesome!
Happy coding! 🚀