SmallPict Ruby SDK
Official Ruby gem for the SmallPict Image Optimization API โ high-performance next-gen image transcoding (AVIF, WebP), smart compression, Edge CDN delivery, cache purging, and Rails ActiveStorage support.
โก Features
-
๐ Idiomatic Ruby Design: Block-based configuration (
SmallPict.configure) and flexible standalone instances. -
๐ Rails ActiveStorage Ready: Built-in service adapter (
SmallPict::ActiveStorage::Service). - ๐ก๏ธ Secure HMAC-SHA256 & Bearer Auth: Tamper-proof payload verification.
-
โจ 4 Core Unified Methods:
optimize(),get_quota(),purge_cdn(), andvalidate_key(). -
๐ Resilience & Fault Tolerance: Automatic
Idempotency-KeyUUID injection, 30s timeouts, and exponential backoff with jitter on HTTP 429/5xx. -
๐ Zero-Leak Privacy: API keys and credentials are automatically redacted from
to_sandinspecterror logs.
๐ฅ Installation
Add to your application's Gemfile:
gem "smallpict", "~> 1.0"And then execute:
bundle install๐ Quick Start
1. Global Block Configuration & Standalone Ruby
require "smallpict"
SmallPict.configure do |config|
config.api_key = ENV["SMALLPICT_API_KEY"]
config.secret_key = ENV["SMALLPICT_SECRET_KEY"] # Optional HMAC Secret Key
end
image_data = File.binread("hero-banner.png")
result = SmallPict.optimize(
image_data,
format: "avif",
quality: 80,
max_width: 1920
)
puts "Optimized CDN URL: #{result.url}"
puts "Saved: #{result.savings_percentage}% (#{result.bytes_saved} bytes)"2. Rails Initializer (config/initializers/smallpict.rb)
SmallPict.configure do |config|
config.api_key = Rails.application.credentials.dig(:smallpict, :api_key)
config.secret_key = Rails.application.credentials.dig(:smallpict, :secret_key)
config.fallback_mode = :passthrough # :throw | :passthrough
end3. Rails Controller Example
class MediaController < ApplicationController
def create
uploaded_file = params[:image]
result = SmallPict.optimize(
uploaded_file.tempfile,
format: "auto",
quality: 85
)
render json: {
url: result.url,
format: result.format,
savings: "#{result.savings_percentage}%"
}
end
end๐ Account Quota & Edge CDN Purge
# 1. Check real-time quota usage
quota = SmallPict.get_quota
puts "Plan: #{quota.plan}, Quota Used: #{quota.quota_percentage}%"
# 2. Invalidate CDN cache for updated images
purge = SmallPict.purge_cdn(["https://cdn.smallpict.app/opt/hero-banner.avif"])
puts purge.message๐งช Testing
bundle exec rspec
bundle exec rubocop๐ License
MIT ยฉ SmallPict Engineering