Hey there, fellow Ruby enthusiast! Ready to add some digital signature magic to your app? Let's dive into integrating the SignRequest API. This nifty tool will let you send, sign, and manage documents with ease. Buckle up, and let's get coding!
Before we jump in, make sure you've got:
httparty
gem (our HTTP request wingman)First things first, let's get our environment ready:
gem install httparty
Now, let's keep our API key safe:
# config/initializers/signrequest.rb SIGNREQUEST_API_KEY = ENV['SIGNREQUEST_API_KEY']
Time to create our SignRequest client:
require 'httparty' class SignRequestClient include HTTParty base_uri 'https://signrequest.com/api/v1' headers 'Authorization' => "Token #{SIGNREQUEST_API_KEY}" end # Test the connection response = SignRequestClient.get('/documents/') puts response.code == 200 ? "We're in!" : "Houston, we have a problem."
def create_document(file_path) response = SignRequestClient.post('/documents/', body: { file: File.new(file_path) }, multipart: true ) JSON.parse(response.body) end
def send_signature_request(document_id, signers) body = { document: document_id, signers: signers.map { |email| { email: email } } } SignRequestClient.post('/signrequests/', body: body.to_json) end
def check_status(signrequest_id) SignRequestClient.get("/signrequests/#{signrequest_id}/") end
Always wrap your API calls in error handling:
begin response = SignRequestClient.get('/documents/') rescue => e puts "Oops! #{e.message}" end
And don't forget about rate limiting. Be a good API citizen!
Want to level up? Check out these cool features:
Don't forget to test! Here's a quick example using RSpec:
RSpec.describe SignRequestClient do it "successfully creates a document" do response = create_document('path/to/test/file.pdf') expect(response['id']).not_to be_nil end end
When deploying, remember:
And there you have it! You're now ready to sign documents like a boss. Remember, the SignRequest API has a ton more features to explore, so don't be shy – dive into their docs and see what else you can do.
Happy coding, and may all your signatures be digital! 🚀✍️