Back

Step by Step Guide to Building a Delighted API Integration in Ruby

Aug 16, 20246 minute read

Introduction

Hey there, fellow Ruby enthusiast! Ready to dive into the world of customer feedback? Let's talk about integrating the Delighted API into your Ruby project. Delighted is a powerhouse for gathering customer feedback, and with their API, you can automate surveys, fetch responses, and do a whole lot more. Buckle up, because we're about to make your app a lot more insightful!

Prerequisites

Before we jump in, make sure you've got:

  • Ruby 2.5 or higher (come on, live a little on the edge!)
  • The delighted gem
  • Your Delighted API key (keep it secret, keep it safe)

Setting up the project

First things first, let's get our project off the ground:

mkdir delighted_integration cd delighted_integration bundle init

Now, crack open that Gemfile and add:

gem 'delighted'

Run bundle install, and we're off to the races!

Configuring the Delighted API client

Time to get that API client set up. In your Ruby file, let's do this:

require 'delighted' Delighted.api_key = 'your_api_key_here'

Pro tip: In a real-world scenario, you'd want to use environment variables for that API key. Security first, folks!

Basic API operations

Sending a survey

Let's spread some survey love:

person = Delighted::Person.create( email: "[email protected]", name: "John Doe", properties: { plan: "Enterprise" } )

Retrieving survey responses

Curious about what your customers think? Let's find out:

survey_responses = Delighted::SurveyResponse.all( page: 1, per_page: 25 ) survey_responses.each do |response| puts "Score: #{response.score}, Comment: #{response.comment}" end

Listing people

Who are all these wonderful people you're surveying?

people = Delighted::Person.all(page: 1) people.each do |person| puts "#{person.name} (#{person.email})" end

Advanced usage

Pagination

Don't let large datasets scare you. Pagination's got your back:

page = 1 loop do responses = Delighted::SurveyResponse.all(page: page, per_page: 100) break if responses.empty? # Process responses page += 1 end

Filtering results

Need to narrow things down? Filters are your friend:

responses = Delighted::SurveyResponse.all( since: Time.now - 30 * 24 * 60 * 60, # Last 30 days score: 9 # Only promoters )

Error handling

Always be prepared for the unexpected:

begin Delighted::Person.create(email: "invalid-email") rescue Delighted::ResourceInvalidError => e puts "Oops! #{e.message}" end

Best practices

  • Mind the rate limits! Delighted's pretty generous, but don't go crazy.
  • Keep that API key secret. Use environment variables, not hard-coded strings.
  • Batch operations when possible to reduce API calls.

Testing the integration

Don't forget to test! Here's a quick example using RSpec and WebMock:

require 'webmock/rspec' RSpec.describe "Delighted Integration" do it "creates a person" do stub_request(:post, "https://api.delighted.com/v1/people.json") .to_return(status: 200, body: '{"id": "123"}') person = Delighted::Person.create(email: "[email protected]") expect(person.id).to eq("123") end end

Deployment considerations

When you're ready to ship:

  • Use environment variables for configuration (I know, I'm a broken record).
  • Consider adding Delighted API calls to background jobs if they're not time-sensitive.
  • Monitor your API usage to stay within limits and optimize as needed.

Conclusion

And there you have it! You're now armed and dangerous with Delighted API integration skills. Remember, the key to great customer feedback is asking the right questions and making it easy for customers to respond. Now go forth and delight those customers!

Want to dive deeper? Check out the Delighted API documentation for more advanced features and options. Happy coding, and may your NPS scores be ever in your favor!