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) ⇒ Client

Initialize a new VibeSort client

Examples:

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

Parameters:

  • api_key (String)

    OpenAI API key

  • temperature (Float) (defaults to: 0.0)

    Temperature for the model (default: 0.0)

Raises:

  • (ArgumentError)

    if api_key is invalid



16
17
18
# File 'lib/vibe_sort/client.rb', line 16

def initialize(api_key:, temperature: 0.0)
  @config = Configuration.new(api_key: api_key, temperature: temperature)
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 OpenAI 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" }

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)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vibe_sort/client.rb', line 47

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