Hey there, fellow Ruby enthusiast! Ready to supercharge your fundraising efforts? Let's dive into integrating the Donorbox API into your Ruby project. This powerful tool will help you manage campaigns, track donations, and handle donor data like a pro.
Before we jump in, make sure you've got:
httparty
and dotenv
gemsFirst things first, let's get our project set up:
mkdir donorbox_integration cd donorbox_integration bundle init
Now, add these gems to your Gemfile:
gem 'httparty' gem 'dotenv'
Run bundle install
, and we're off to the races!
Donorbox uses API key authentication. Create a .env
file in your project root:
DONORBOX_API_KEY=your_api_key_here
DONORBOX_API_TOKEN=your_api_token_here
Now, let's create a base class for our API calls:
require 'httparty' require 'dotenv/load' class DonorboxAPI include HTTParty base_uri 'https://donorbox.org/api/v1' def initialize @auth = { username: ENV['DONORBOX_API_KEY'], password: ENV['DONORBOX_API_TOKEN'] } end end
With our base class set up, making requests is a breeze:
class DonorboxAPI # ... previous code ... def get_campaigns self.class.get('/campaigns', basic_auth: @auth) end end
Let's add some more useful methods:
class DonorboxAPI # ... previous code ... def get_donations(campaign_id) self.class.get("/campaigns/#{campaign_id}/donations", basic_auth: @auth) end def create_donor(donor_data) self.class.post('/donors', body: donor_data, basic_auth: @auth) end end
Always handle those pesky errors and respect rate limits:
class DonorboxAPI # ... previous code ... def get_campaigns response = self.class.get('/campaigns', basic_auth: @auth) handle_response(response) end private def handle_response(response) case response.code when 200 response.parsed_response when 429 raise "Rate limit exceeded. Try again in #{response.headers['Retry-After']} seconds." else raise "API error: #{response.code} - #{response.message}" end end end
Process that JSON like a champ:
campaigns = api.get_campaigns campaigns.each do |campaign| # Store in your database or do something cool with the data puts "Campaign: #{campaign['name']} - Raised: #{campaign['amount_raised']}" end
If you're using webhooks, set up an endpoint in your web framework of choice:
post '/donorbox_webhook' do payload = JSON.parse(request.body.read) # Process the webhook payload # Verify the webhook signature if provided status 200 end
Don't forget to test! Here's a simple RSpec example:
RSpec.describe DonorboxAPI do let(:api) { DonorboxAPI.new } describe '#get_campaigns' do it 'returns a list of campaigns' do VCR.use_cassette('campaigns') do campaigns = api.get_campaigns expect(campaigns).to be_an(Array) expect(campaigns.first).to have_key('name') end end end end
And there you have it! You've just built a solid Donorbox API integration in Ruby. Remember, this is just the beginning – there's so much more you can do with this API. Keep exploring, keep coding, and most importantly, keep making a difference with your fundraising efforts!
Happy coding, and may your donations be plentiful! 🚀💎