Hey there, fellow Ruby enthusiast! Ready to supercharge your forms with Paperform's API? Let's dive into building a slick integration using the paperform-ruby package. Trust me, it's easier than you might think, and you'll be up and running in no time.
Before we jump in, make sure you've got:
First things first, let's get that paperform-ruby gem installed:
gem install paperform-ruby
Easy peasy, right?
Now, let's get you authenticated. It's as simple as initializing the Paperform client with your API key:
require 'paperform' client = Paperform::Client.new(api_key: 'your_api_key_here')
Alright, time for the fun stuff! Let's cover the basics:
forms = client.forms.list puts forms
submissions = client.submissions.list(form_id: 'your_form_id') puts submissions
new_submission = client.submissions.create( form_id: 'your_form_id', data: { name: 'John Doe', email: '[email protected]' } ) puts new_submission
Ready to level up? Let's explore some advanced features:
filtered_submissions = client.submissions.list( form_id: 'your_form_id', filter: { field: 'email', operator: 'contains', value: 'example.com' }, sort: { field: 'created_at', direction: 'desc' } )
page_1 = client.submissions.list(form_id: 'your_form_id', page: 1, per_page: 10) page_2 = client.submissions.list(form_id: 'your_form_id', page: 2, per_page: 10)
require 'sinatra' post '/webhook' do payload = JSON.parse(request.body.read) # Process the webhook payload status 200 end
Don't let errors catch you off guard. Here's how to handle them like a pro:
begin result = client.some_operation rescue Paperform::Error => e puts "Oops! Something went wrong: #{e.message}" end
A couple of quick tips to keep your integration smooth:
Let's put it all together with a simple script:
require 'paperform' client = Paperform::Client.new(api_key: ENV['PAPERFORM_API_KEY']) # Fetch all forms forms = client.forms.list puts "You have #{forms.length} forms" # Get submissions for the first form first_form = forms.first submissions = client.submissions.list(form_id: first_form['id']) puts "Form '#{first_form['title']}' has #{submissions.length} submissions" # Create a new submission new_submission = client.submissions.create( form_id: first_form['id'], data: { name: 'API Test', email: '[email protected]' } ) puts "Created new submission with ID: #{new_submission['id']}"
And there you have it! You're now equipped to build awesome Paperform integrations with Ruby. Remember, this is just scratching the surface. The Paperform API has tons more to offer, so don't be afraid to explore and experiment.
Happy coding, and may your forms be ever responsive!