Skip to main content

Ruby SDK

The Ruby SDK is currently in development. This page shows the planned implementation.
The official Gumnut Ruby SDK will provide an idiomatic Ruby interface to the Gumnut API, perfect for Ruby on Rails applications and Ruby scripts.

Planned Features

Core Capabilities

  • Ruby 2.7+ Support: Compatible with modern Ruby versions
  • Idiomatic Ruby Interface: Follows Ruby conventions and best practices
  • ActiveSupport Integration: Seamless integration with Rails applications
  • Comprehensive Test Coverage: Full RSpec test suite
  • RubyGems Distribution: Easy installation via gem install

Advanced Features

  • Async Operations: Support for concurrent operations
  • Automatic Retries: Configurable retry logic with exponential backoff
  • Connection Pooling: Optimized HTTP performance
  • Rails Integration: ActiveModel-like interfaces
  • Debugging Support: Detailed logging and error reporting

Expected Usage

Installation

Gemfile:
gem 'gumnut'
Direct Installation:
gem install gumnut

Basic Usage

require 'gumnut'

# Initialize client
client = Gumnut::Client.new(
  api_key: ENV['GUMNUT_API_KEY']
)

# Upload an asset
asset = client.assets.create(
  asset_data: File.open('photo.jpg'),
  device_asset_id: 'photo_001',
  device_id: 'my_device',
  file_created_at: Time.now.iso8601,
  file_modified_at: Time.now.iso8601
)

# Create an album
album = client.albums.create(
  album_name: 'Vacation 2024',
  description: 'Summer vacation photos'
)

# Add asset to album
client.albums.add_assets(
  album_id: album.id,
  asset_ids: [asset.id]
)

Rails Integration

# config/initializers/gumnut.rb
Gumnut.configure do |config|
  config.api_key = Rails.application.credentials.gumnut_api_key
  config.timeout = 30
  config.retries = 3
end

# app/models/photo.rb
class Photo < ApplicationRecord
  include Gumnut::Model
  
  gumnut_asset :image, device_id: -> { "rails_#{Rails.env}" }
  
  after_create :upload_to_gumnut
  
  private
  
  def upload_to_gumnut
    self.gumnut_asset = Gumnut.client.assets.create(
      asset_data: image,
      device_asset_id: id.to_s,
      device_id: "rails_#{Rails.env}"
    )
    save!
  end
end

Block Syntax

# Using blocks for resource management
client.assets.upload('photo.jpg') do |asset|
  puts "Uploaded: #{asset.id}"
  
  # Create album and add asset
  album = client.albums.create(album_name: 'New Album')
  client.albums.add_assets(album.id, [asset.id])
end

# Batch operations
client.assets.batch_upload(['photo1.jpg', 'photo2.jpg']) do |results|
  results.each do |asset|
    puts "Processed: #{asset.original_path}"
  end
end

Error Handling

begin
  asset = client.assets.find('invalid_id')
rescue Gumnut::NotFoundError => e
  puts "Asset not found: #{e.message}"
rescue Gumnut::RateLimitError => e
  puts "Rate limited. Retry after: #{e.retry_after}s"
  sleep(e.retry_after)
  retry
rescue Gumnut::APIError => e
  puts "API Error: #{e.status} - #{e.message}"
  puts "Request ID: #{e.request_id}"
end

Enumerable Interface

# Assets act like Ruby collections
client.assets.each do |asset|
  puts "Asset: #{asset.id} (#{asset.type})"
end

# Chain operations
recent_images = client.assets
  .select(&:image?)
  .select { |asset| asset.created_at > 1.week.ago }
  .sort_by(&:created_at)
  .reverse

# Find methods
sunset_photos = client.assets.find_all do |asset|
  asset.description&.include?('sunset')
end

Planned Repository

GitHub Repository: github.com/gumnut-ai/photos-sdk-ruby (Coming Soon) The repository will include:
  • Complete source code with Ruby best practices
  • Rails integration examples
  • RSpec test suite
  • YARD documentation
  • Rake tasks for common operations

Use Cases

# app/controllers/photos_controller.rb
class PhotosController < ApplicationController
  before_action :setup_gumnut_client
  
  def index
    @assets = @gumnut.assets.list(limit: 20, offset: params[:page].to_i * 20)
  end
  
  def create
    uploaded_file = params[:photo]
    
    @asset = @gumnut.assets.create(
      asset_data: uploaded_file,
      device_asset_id: SecureRandom.uuid,
      device_id: "rails_#{request.remote_ip}"
    )
    
    redirect_to photos_path, notice: 'Photo uploaded successfully!'
  rescue Gumnut::APIError => e
    redirect_to photos_path, alert: "Upload failed: #{e.message}"
  end
  
  private
  
  def setup_gumnut_client
    @gumnut = Gumnut::Client.new
  end
end

Background Job Integration

# app/jobs/photo_processing_job.rb
class PhotoProcessingJob < ApplicationJob
  queue_as :default
  
  def perform(photo_id)
    photo = Photo.find(photo_id)
    client = Gumnut::Client.new
    
    # Upload to Gumnut
    asset = client.assets.create(
      asset_data: photo.image,
      device_asset_id: photo.id.to_s,
      device_id: 'background_job'
    )
    
    # Wait for processing
    client.assets.wait_for_processing(asset.id) do |processed_asset|
      photo.update!(
        gumnut_asset_id: processed_asset.id,
        faces_detected: processed_asset.faces.count
      )
    end
  end
end

Rake Tasks

# lib/tasks/gumnut.rake
namespace :gumnut do
  desc "Sync all photos to Gumnut"
  task sync_photos: :environment do
    client = Gumnut::Client.new
    
    Photo.find_each do |photo|
      next if photo.gumnut_asset_id.present?
      
      puts "Syncing photo #{photo.id}..."
      asset = client.assets.create(
        asset_data: photo.image,
        device_asset_id: photo.id.to_s,
        device_id: 'rake_sync'
      )
      
      photo.update!(gumnut_asset_id: asset.id)
    end
    
    puts "Sync completed!"
  end
end

Development Timeline

  1. Q2 2024: Design and architecture planning
  2. Q3 2024: Core SDK development
  3. Q4 2024: Rails integration and testing
  4. Q1 2025: Beta release with documentation
  5. Q2 2025: Stable 1.0 release

Ruby Community

The Ruby SDK will be developed with input from the Ruby community:
  • RubyGems: Distributed via the official RubyGems repository
  • Rails Compatibility: Tested with multiple Rails versions
  • Ruby Style: Follows RuboCop and community conventions
  • Documentation: Comprehensive YARD docs with examples

Stay Updated

To be notified when the Ruby SDK is available:
  • Watch the repository: github.com/gumnut-ai/photos-sdk-ruby
  • Follow our updates at www.gumnut.ai/blog
  • Join Ruby community discussions (coming soon)

Alternative Options

While the Ruby SDK is in development:

HTTP Client Libraries

Use popular Ruby HTTP clients:
require 'net/http'
require 'json'

# Using Net::HTTP
uri = URI('https://api.gumnut.ai/api/assets')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Get.new(uri)
request['Authorization'] = "Bearer #{ENV['GUMNUT_API_KEY']}"

response = http.request(request)
assets = JSON.parse(response.body)

# Using HTTParty
require 'httparty'

class GumnutAPI
  include HTTParty
  base_uri 'https://api.gumnut.ai'
  
  def initialize(api_key)
    @options = { headers: { 'Authorization' => "Bearer #{api_key}" } }
  end
  
  def assets
    self.class.get('/api/assets', @options)
  end
end

OpenAPI Generator

Generate a Ruby client from the OpenAPI spec:
# Install OpenAPI Generator
gem install openapi_client

# Generate Ruby client
openapi-generator-cli generate \
  -i https://api.gumnut.ai/openapi.json \
  -g ruby \
  -o ./gumnut-ruby-client

Next Steps

  • Check back for updates on Ruby SDK development
  • Use the REST API directly for immediate integration
  • See other SDK options for different languages
  • Review Authentication for API setup
I