Ruby
Ruby SDK for the Poodle’s email sending API.
Table of Contents
Section titled “Table of Contents”- Features
- Installation
- Quick Start
- Configuration
- Usage Examples
- API Reference
- Error Types
- Development
- Contributing
- License
- Support
Features
Section titled “Features”- 🚀 Simple API - Send emails with just a few lines of code
- 🔒 Type Safe - Comprehensive validation and error handling
- 🌐 Environment Support - Easy configuration via environment variables
- 📝 Rich Content - Support for HTML, plain text, and multipart emails
- 🔄 Retry Logic - Built-in support for handling rate limits and network issues
- 🧪 Test Support - Comprehensive testing utilities and mocks
- 📚 Well Documented - Comprehensive documentation and examples
- 🎯 Ruby 3.0+ - Modern Ruby support with keyword arguments
Installation
Section titled “Installation”Add this line to your application’s Gemfile:
gem 'poodle-ruby'
And then execute:
bundle install
Or install it yourself as:
gem install poodle-ruby
Quick Start
Section titled “Quick Start”require 'poodle'
# Initialize the clientclient = Poodle::Client.new(api_key: 'your_api_key')
# Send an emailresponse = client.send( subject: 'Hello from Poodle!', html: '<h1>Hello World!</h1><p>This email was sent using Poodle.</p>')
if response.success? puts "Email sent successfully!"else puts "Failed to send email: #{response.message}"end
Configuration
Section titled “Configuration”API Key
Section titled “API Key”Set your API key in one of these ways:
# 1. Pass directly to clientclient = Poodle::Client.new(api_key: 'your_api_key')
# 2. Use environment variableENV['POODLE_API_KEY'] = 'your_api_key'client = Poodle::Client.new
# 3. Use configuration objectconfig = Poodle::Configuration.new( api_key: 'your_api_key', timeout: 30, debug: true)client = Poodle::Client.new(config)
Environment Variables
Section titled “Environment Variables”Variable | Description | Default |
---|---|---|
POODLE_API_KEY | Your Poodle API key | Required |
POODLE_BASE_URL | API base URL | https://api.usepoodle.com |
POODLE_TIMEOUT | Request timeout in seconds | 30 |
POODLE_CONNECT_TIMEOUT | Connection timeout in seconds | 10 |
POODLE_DEBUG | Enable debug logging | false |
Usage Examples
Section titled “Usage Examples”Basic Email Sending
Section titled “Basic Email Sending”# HTML emailresponse = client.send_html( subject: 'Weekly Newsletter', html: '<h1>Newsletter</h1><p>Your weekly update...</p>')
# Plain text emailresponse = client.send_text( subject: 'Account Update', text: 'Your account has been updated successfully.')
# Multipart email (HTML + Text)response = client.send( subject: 'Welcome!', html: '<h1>Welcome!</h1><p>Thanks for joining us.</p>', text: 'Welcome! Thanks for joining us.')
Using Email Objects
Section titled “Using Email Objects”# Create an Email object for reusability and validationemail = Poodle::Email.new( subject: 'Important Update', html: '<h1>Update</h1><p>Please read this important update.</p>', text: 'Update: Please read this important update.')
# Check email propertiesputs "Multipart email: #{email.multipart?}"puts "Content size: #{email.content_size} bytes"
# Send the emailresponse = client.send_email(email)
Multipart Emails (HTML + Text)
Section titled “Multipart Emails (HTML + Text)”# Send emails with both HTML and text content for maximum compatibilityresponse = client.send( subject: 'Weekly Newsletter', html: '<h1>Newsletter</h1><p>This week\'s updates...</p>', text: 'Newsletter\n\nThis week\'s updates...')
Rails Integration
Section titled “Rails Integration”The Poodle SDK provides seamless Rails integration with automatic configuration and helpful rake tasks.
Installation
Section titled “Installation”Add to your Rails application’s Gemfile:
gem 'poodle-ruby'
Configuration
Section titled “Configuration”Create an initializer or let Poodle auto-configure:
Poodle::Rails.configure do |config| config.api_key = Rails.application.credentials.poodle_api_key config.debug = Rails.env.development?end
Or use environment variables:
# .env or environmentPOODLE_API_KEY=your_api_key_here
Usage in Controllers
Section titled “Usage in Controllers”class NotificationController < ApplicationController def send_welcome_email response = Poodle::Rails.client.send( to: params[:email], subject: "Welcome!", html: render_to_string("welcome_email") )
if response.success? render json: { status: "sent" } else render json: { error: response.message }, status: :unprocessable_entity end endend
Rake Tasks
Section titled “Rake Tasks”# Check configurationrake poodle:config
# Test connectionrake poodle:test
# Send test email
# Generate initializerrake poodle:install
Testing
Section titled “Testing”The SDK includes comprehensive testing utilities for easy testing in your applications.
RSpec Integration
Section titled “RSpec Integration”# spec/spec_helper.rb or spec/rails_helper.rbrequire 'poodle'
RSpec.configure do |config| config.include Poodle::TestHelpers
config.before(:each) do Poodle.test_mode! end
config.after(:each) do Poodle.clear_deliveries endend
Testing Email Sending
Section titled “Testing Email Sending”it "sends welcome email" do expect { UserMailer.send_welcome(user) }.to change { Poodle.deliveries.count }.by(1)
email = Poodle.last_delivery expect(email[:to]).to eq(user.email) expect(email[:subject]).to include("Welcome") expect(email[:html]).to include(user.name)end
it "sends notification emails" do service.send_notifications
assert_email_sent(3) assert_email_sent_with_subject("Alert")end
Error Handling
Section titled “Error Handling”begin response = client.send(email_data) puts "Email sent!" if response.success?rescue Poodle::ValidationError => e puts "Validation failed: #{e.message}" e.errors.each do |field, messages| puts "#{field}: #{messages.join(', ')}" endrescue Poodle::AuthenticationError => e puts "Authentication failed: #{e.message}"rescue Poodle::RateLimitError => e puts "Rate limited. Retry after: #{e.retry_after} seconds"rescue Poodle::PaymentError => e puts "Payment required: #{e.message}" puts "Upgrade at: #{e.upgrade_url}"rescue Poodle::ForbiddenError => e puts "Access forbidden: #{e.message}"rescue Poodle::NetworkError => e puts "Network error: #{e.message}"rescue Poodle::ServerError => e puts "Server error: #{e.message}"rescue Poodle::Error => e puts "Poodle error: #{e.message}"end
Retry Logic
Section titled “Retry Logic”def send_with_retry(client, email_data, max_retries: 3) retries = 0
begin client.send(email_data) rescue Poodle::RateLimitError => e if retries < max_retries && e.retry_after retries += 1 sleep(e.retry_after) retry else raise end rescue Poodle::NetworkError, Poodle::ServerError => e if retries < max_retries retries += 1 sleep(2 ** retries) # Exponential backoff retry else raise end endend
API Reference
Section titled “API Reference”Client
Section titled “Client”Poodle::Client.new(config_or_api_key, **options)
Section titled “Poodle::Client.new(config_or_api_key, **options)”Creates a new Poodle client.
Parameters:
config_or_api_key
- Configuration object, API key string, or nil**options
- Additional options (base_url, timeout, debug, etc.)
client.send(from:, to:, subject:, html: nil, text: nil)
Section titled “client.send(from:, to:, subject:, html: nil, text: nil)”Sends an email with the specified parameters.
client.send_email(email)
Section titled “client.send_email(email)”Sends an Email object.
client.send_html(from:, to:, subject:, html:)
Section titled “client.send_html(from:, to:, subject:, html:)”Sends an HTML-only email.
client.send_text(from:, to:, subject:, text:)
Section titled “client.send_text(from:, to:, subject:, text:)”Sends a text-only email.
Poodle::Email.new(from:, to:, subject:, html: nil, text: nil)
Section titled “Poodle::Email.new(from:, to:, subject:, html: nil, text: nil)”Creates a new Email object with validation.
Methods:
#html?
- Returns true if HTML content is present#text?
- Returns true if text content is present#multipart?
- Returns true if both HTML and text are present#content_size
- Returns total content size in bytes#to_h
- Converts to hash for API requests
EmailResponse
Section titled “EmailResponse”Properties
Section titled “Properties”#success?
- Returns true if email was successfully queued#failed?
- Returns true if email sending failed#message
- Response message from API#data
- Additional response data
Configuration
Section titled “Configuration”Poodle::Configuration.new(**options)
Section titled “Poodle::Configuration.new(**options)”Creates a new configuration object.
Options:
api_key
- Your Poodle API keybase_url
- API base URLtimeout
- Request timeout in secondsconnect_timeout
- Connection timeout in secondsdebug
- Enable debug logginghttp_options
- Additional HTTP client options
Error Types
Section titled “Error Types”Error Class | Description | HTTP Status |
---|---|---|
Poodle::ValidationError | Invalid request data | 400, 422 |
Poodle::AuthenticationError | Invalid API key | 401 |
Poodle::PaymentError | Payment required | 402 |
Poodle::ForbiddenError | Access forbidden | 403 |
Poodle::RateLimitError | Rate limit exceeded | 429 |
Poodle::ServerError | Server error | 5xx |
Poodle::NetworkError | Network/connection error | Various |
All errors inherit from Poodle::Error
which provides:
#message
- Error message#context
- Additional error context#status_code
- HTTP status code (if applicable)
Development
Section titled “Development”After checking out the repo, run bin/setup
to install dependencies. Then, run bundle exec rspec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Contributing
Section titled “Contributing”Contributions are welcome! Please read our Contributing Guide for details on the process for submitting pull requests and our Code of Conduct.
License
Section titled “License”The gem is available as open source under the terms of the MIT License.