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: nil, model: nil, extra_params: {}) ⇒ Client

Initialize a new VibeSort client

Examples:

Provider inferred from the key prefix

client = VibeSort::Client.new(api_key: ENV['ANTHROPIC_API_KEY']) # sk-ant-... => :anthropic

OpenAI (explicit)

client = VibeSort::Client.new(provider: :openai, api_key: ENV['OPENAI_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')

OpenRouter with extra request parameters

client = VibeSort::Client.new(
  provider: :openrouter,
  api_key: ENV['OPENROUTER_API_KEY'],
  model: 'meta-llama/llama-3.3-70b-instruct',
  extra_params: { max_tokens: 200 }
)

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, nil) (defaults to: nil)

    LLM provider: :openai, :anthropic, :gemini, :groq, :spacexai, or :openrouter. When omitted, the provider is inferred from the api_key prefix (e.g. "sk-ant-..." routes to Anthropic), falling back to :openai for unrecognized key formats.

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

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

  • extra_params (Hash) (defaults to: {})

    Provider-native request parameters deep-merged into the request payload last, so they can override anything the adapter builds

Raises:

  • (ArgumentError)

    if api_key or provider is invalid



38
39
40
41
# File 'lib/vibe_sort/client.rb', line 38

def initialize(api_key:, temperature: 0.0, provider: nil, model: nil, extra_params: {})
  @config = Configuration.new(api_key: api_key, temperature: temperature, provider: provider, model: model,
                              extra_params: extra_params)
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, 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)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vibe_sort/client.rb', line 71

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