Class: VibeSort::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vibe_sort/client.rb

Overview

Client is the main public interface for the VibeSort gem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, temperature: 0.0, provider: :openai, model: nil) ⇒ Client

Initialize a new VibeSort client

Examples:

OpenAI (default provider)

client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])

Anthropic Claude

client = VibeSort::Client.new(provider: :anthropic, api_key: ENV['ANTHROPIC_API_KEY'])

Google Gemini with a custom model

client = VibeSort::Client.new(provider: :gemini, api_key: ENV['GEMINI_API_KEY'], model: 'gemini-2.5-pro')

Parameters:

  • api_key (String)

    API key for the selected provider

  • temperature (Float) (defaults to: 0.0)

    Temperature for the model (default: 0.0). Ignored by providers whose current models do not accept it (Anthropic).

  • provider (Symbol, String) (defaults to: :openai)

    LLM provider: :openai (default), :anthropic, or :gemini

  • model (String, nil) (defaults to: nil)

    Model ID override (nil uses the provider's default)

Raises:

  • (ArgumentError)

    if api_key or provider is invalid



25
26
27
# File 'lib/vibe_sort/client.rb', line 25

def initialize(api_key:, temperature: 0.0, provider: :openai, model: nil)
  @config = Configuration.new(api_key: api_key, temperature: temperature, provider: provider, model: model)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/vibe_sort/client.rb', line 6

def config
  @config
end

Instance Method Details

#sort(array) ⇒ Hash

Sort an array of numbers and/or strings using the configured provider's API

Examples:

Successful sort with numbers

result = client.sort([5, 2, 8, 1, 9])
#=> { success: true, sorted_array: [1, 2, 5, 8, 9] }

Successful sort with strings

result = client.sort(["banana", "Apple", "cherry"])
#=> { success: true, sorted_array: ["Apple", "banana", "cherry"] }

Successful sort with mixed types

result = client.sort([42, "hello", 8, "world"])
#=> { success: true, sorted_array: [8, 42, "hello", "world"] }

Invalid input

result = client.sort([1, :symbol, 3])
#=> { success: false, sorted_array: [], error: "Input must be an array of numbers or strings" }

API error

result = client.sort([1, 2, 3]) # with invalid API key
#=> { success: false, sorted_array: [], error: "OpenAI API error: Invalid API key" }
# The error prefix names the configured provider (OpenAI, Anthropic, or Gemini)

Parameters:

  • array (Array)

    Array of numbers and/or strings to sort

Returns:

  • (Hash)

    Result hash with keys:

    • :success [Boolean] whether the operation succeeded
    • :sorted_array [Array] the sorted array (empty on failure)
    • :error [String] error message (only present on failure)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vibe_sort/client.rb', line 57

def sort(array)
  # Validate input
  unless valid_input?(array)
    return {
      success: false,
      sorted_array: [],
      error: "Input must be an array of numbers or strings"
    }
  end

  # Perform the sort via API
  sorter = Sorter.new(config)
  sorter.perform(array)
rescue ApiError => e
  {
    success: false,
    sorted_array: [],
    error: e.message
  }
rescue StandardError => e
  {
    success: false,
    sorted_array: [],
    error: "Unexpected error: #{e.message}"
  }
end