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!
Before we jump in, make sure you've got:
delighted
gemFirst 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!
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!
Let's spread some survey love:
person = Delighted::Person.create( email: "[email protected]", name: "John Doe", properties: { plan: "Enterprise" } )
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
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
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
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 )
Always be prepared for the unexpected:
begin Delighted::Person.create(email: "invalid-email") rescue Delighted::ResourceInvalidError => e puts "Oops! #{e.message}" end
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
When you're ready to ship:
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!