Hey there, fellow Ruby enthusiast! Ready to dive into the world of digital signatures? Let's get cracking with Adobe Sign API integration. This nifty tool will let you automate document signing processes, making life easier for you and your users.
Before we jump in, make sure you've got:
First things first, let's set up our project:
mkdir adobe_sign_integration cd adobe_sign_integration bundle init
Now, add these gems to your Gemfile:
gem 'faraday' gem 'dotenv'
Run bundle install
, and we're off to the races!
Adobe Sign uses OAuth 2.0. Here's a quick way to get your access token:
require 'faraday' require 'dotenv/load' client_id = ENV['ADOBE_SIGN_CLIENT_ID'] client_secret = ENV['ADOBE_SIGN_CLIENT_SECRET'] redirect_uri = ENV['ADOBE_SIGN_REDIRECT_URI'] # Get your authorization code first, then: response = Faraday.post('https://secure.na1.echosign.com/oauth/token', { grant_type: 'authorization_code', client_id: client_id, client_secret: client_secret, code: authorization_code, redirect_uri: redirect_uri }) access_token = JSON.parse(response.body)['access_token']
Pro tip: Store your credentials in a .env
file and use the dotenv
gem to load them. Security first!
Let's create and send an agreement:
conn = Faraday.new(url: 'https://api.na1.echosign.com/api/rest/v6') do |f| f.request :json f.response :json f.adapter Faraday.default_adapter end response = conn.post('/agreements') do |req| req.headers['Authorization'] = "Bearer #{access_token}" req.body = { fileInfos: [{ transientDocumentId: 'your_document_id' }], name: 'Test Agreement', participantSetsInfo: [ { memberInfos: [{ email: '[email protected]' }], order: 1, role: 'SIGNER' } ], signatureType: 'ESIGN', state: 'IN_PROCESS' } end agreement_id = response.body['id']
Want to get fancy? Let's set up a webhook:
conn.post('/webhooks') do |req| req.headers['Authorization'] = "Bearer #{access_token}" req.body = { name: 'My Webhook', scope: 'ACCOUNT', state: 'ACTIVE', webhookSubscriptionEvents: ['AGREEMENT_ALL'], webhookUrlInfo: { url: 'https://your-webhook-url.com' } } end
Always expect the unexpected:
begin response = conn.get('/agreements/#{agreement_id}') if response.status == 429 # Handle rate limiting sleep(60) retry end rescue Faraday::ConnectionFailed # Handle network issues retry end
Mock it 'til you make it:
require 'webmock/rspec' RSpec.describe AdobeSignClient do it 'creates an agreement' do stub_request(:post, 'https://api.na1.echosign.com/api/rest/v6/agreements') .to_return(status: 201, body: '{"id": "abc123"}') client = AdobeSignClient.new response = client.create_agreement(name: 'Test') expect(response['id']).to eq('abc123') end end
Keep your secrets secret:
# config/initializers/adobe_sign.rb AdobeSign.configure do |config| config.client_id = ENV['ADOBE_SIGN_CLIENT_ID'] config.client_secret = ENV['ADOBE_SIGN_CLIENT_SECRET'] end
And there you have it! You're now equipped to integrate Adobe Sign into your Ruby projects like a pro. Remember, the API is your oyster – there's plenty more to explore in the official docs. Happy coding, and may your signatures always be digital!