# Ruby

ImageKit integration for Ruby and Ruby on Rails applications with support for image/video transformations, URL generation, and file uploads.

ImageKit provides comprehensive Ruby support through two packages designed for different use cases in the Ruby ecosystem:

[**Ruby SDK**](/content/docs/integration/ruby#ruby-sdk/index.html)

The [ImageKit Ruby SDK](https://github.com/imagekit-developer/imagekit-ruby) is a complete Ruby library for integrating ImageKit's media API. It provides:

- **URL generation** with transformations for images and videos
- **File upload** management through ImageKit's REST API
- **Signed URLs** for secure content delivery
- **Helper methods** for authentication and responsive images
- **Type safety** with comprehensive RBI (Sorbet) and RBS definitions

The SDK is built on Ruby's standard `net/http` library with connection pooling, supports Ruby 3.2.0+, and ships with complete YARD documentation.

## Installation:

```ruby
gem "imagekitio"
```

## Documentation:

- [Gemdocs](https://gemdocs.org/gems/imagekitio)
- [SDK GitHub repo](https://github.com/imagekit-developer/imagekit-ruby)

[**Ruby on Rails Gem**](/content/docs/integration/ruby#ruby-on-rails-gem/index.html)

The [ImageKit Rails gem](https://github.com/imagekit-developer/imagekit-rails) builds on top of the Ruby SDK to provide Rails-specific integrations:

- **View helpers** (`ik_image_tag`, `ik_video_tag`) for responsive images and videos
- **Active Storage service** for storing uploads in ImageKit
- **Automatic responsive images** with `srcset` generation
- **Seamless integration** with Rails asset pipeline and ERB templates

The Rails gem handles common Rails patterns like form uploads, model attachments, and view rendering with transformations.

## Installation:

```ruby
gem "imagekitio"
gem "imagekitio-rails"
```

## Documentation:

- [Rails Integration Guide](/content/docs/integration/ruby/ruby-on-rails/index.html)
- [GitHub Repository](https://github.com/imagekit-developer/imagekit-rails)

### Use Cases

Use the Ruby SDK for:

- Non-Rails Ruby applications (Sinatra, Hanami, etc.)
- Background job processors
- Command-line tools and scripts
- Custom integrations where you need direct API access

### Example:

```ruby
require 'imagekitio'

client = Imagekitio::Client.new(
  private_key: ENV['IMAGEKIT_PRIVATE_KEY']
)

# Upload a file
response = client.files.upload(
  file: Pathname('/path/to/image.jpg'),
  file_name: 'image.jpg'
)

# Generate URL with transformations
url = client.helper.build_url({
  src: '/default-image.jpg',
  url_endpoint: 'https://ik.imagekit.io/your_imagekit_id',
  transformation: [{ width: 400, height: 300, quality: 80 }]
})
```

### Ruby on Rails Applications

Use the Rails gem for:

- Full-stack Rails applications
- Active Storage integration
- View helpers for images and videos
- Form uploads with model attachments

### Example:

```erb
<%# app/views/products/show.html.erb %>
<%= ik_image_tag(@product.featured_image,
  transformation: [{ width: 800, height: 600, crop: 'at_max' }],
  sizes: '(max-width: 768px) 100vw, 800px',
  alt: @product.name
) %>
```

### Key Features

Both packages support ImageKit's complete transformation API:

- Image resizing, cropping, and format conversion
- Quality optimization and format auto-detection
- Effects (blur, grayscale, rotation, etc.)
- Text and image overlays
- Chained transformations
- AI-powered transformations (background removal, upscaling)

### File Management

Upload files to ImageKit through the Ruby SDK's file management API or through Rails Active Storage integration:

```ruby
# Direct upload with Ruby SDK
client.files.upload(file: File.read('image.jpg'), file_name: 'image.jpg')

# Rails Active Storage
user.avatar.attach(params[:avatar])
# Automatically stored in ImageKit
```

### Secure Delivery

Generate signed URLs for private content with expiration times:

```ruby
# Ruby SDK
url = client.helper.build_url({
  src: '/private-image.jpg',
  signed: true,
  expires_in: 3600
})

# Rails view helper
<%= ik_image_tag('/private-image.jpg', signed: true, expires_in: 3600) %>
```

### Additional Resources

- [ImageKit REST API Reference](/content/docs/api-reference/index.html)
- [Image and video transformation](/content/docs/transformations/index.html)
- [CarrierWave Integration](/content/docs/integration/ruby/ruby-on-rails-with-carrierwave/index.html) - For legacy Rails applications using CarrierWave.
