Project

llamahair

0.0
The project is in a healthy, maintained state
A Ruby on Rails implementation of the LlamaHair API client with webhook validation support
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 13.0
~> 3.0
~> 1.21
~> 0.9

Runtime

 Project Readme

LlamaHair Ruby

Ruby implementation of the LlamaHair library for interacting with the LlamaHair API.

Installation

Add this line to your application's Gemfile:

gem 'llamahair'

And then execute:

$ bundle install

Usage

Basic Client Usage

Initialize the client with your API credentials:

client = LlamaHair::Client.new(
  LlamaHair::Types::ClientOptions.new(
    api_key_id: 'your_api_key_id',
    api_key_secret: 'your_api_key_secret'
  )
)

You can also use environment variables:

  • LLAMAHAIR_API_KEY_ID
  • LLAMAHAIR_API_SECRET

Sending Requests

Send a request to a specific prompt:

request = LlamaHair::Types::SendRequest.new(
  llama: LlamaHair::Types::Llama.new(
    id: 'unique_id',
    body: 'Your prompt text here'
  )
)

response = client.send('https://api.llamahair.ai/prompts/your-prompt', request)
puts response.job_id

Retrieving Results

Retrieve results using a job ID:

request = LlamaHair::Types::OutputRequest.new(job_id: 'your_job_id')
response = client.retrieve(request)

puts response.response.output if response.response.output
puts response.response.outputs if response.response.outputs
puts response.response.summary if response.response.summary

Send and Retrieve in One Call

response = client.send_and_retrieve(
  'https://api.llamahair.ai/prompts/your-prompt',
  request
)

Webhook Validation

Add webhook validation to your Rails controller:

class WebhooksController < ApplicationController
  skip_before_action :verify_authenticity_token

  def create
    validator = LlamaHair::WebhookValidator.new(
      LlamaHair::Types::WebhookOptions.new(secret: ENV['WEBHOOK_SECRET'])
    )

    unless validator.verify(request)
      return head :unauthorized
    end

    # Process the webhook
    head :ok
  end
end

The validator:

  • Automatically verifies webhook signatures
  • Handles both X-Webhook-Signature and HTTP_X_WEBHOOK_SIGNATURE headers
  • Logs validation errors to Rails logger
  • Uses secure comparison for signature verification

Error Handling

The library provides specific error classes for different scenarios:

begin
  response = client.send_and_retrieve(prompt_url, request)
rescue LlamaHair::TimeoutError => e
  puts "Request timed out: #{e.message}"
rescue LlamaHair::AuthenticationError => e
  puts "Authentication failed: #{e.message}"
rescue LlamaHair::RequestError => e
  puts "Request failed: #{e.message}"
rescue LlamaHair::InvalidResponseError => e
  puts "Invalid response: #{e.message}"
rescue LlamaHair::Error => e
  puts "Unexpected error: #{e.message}"
end

Development

After checking out the repo, run bundle install to install dependencies. Then:

  • Run rake spec to run the tests
  • Run rake rubocop to check code style
  • Run rake test to run both tests and style checks

License

See the LICENSE file for details.