Class: Exa::CLI::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/exa/cli/base.rb

Class Method Summary collapse

Class Method Details

.build_client(api_key, **options) ⇒ Object

Build a client instance with the given API key



32
33
34
# File 'lib/exa/cli/base.rb', line 32

def self.build_client(api_key, **options)
  Exa::Client.new(api_key: api_key, **options)
end

.encode_as_toon(data) ⇒ Object

Encode data as TOON format



51
52
53
54
# File 'lib/exa/cli/base.rb', line 51

def self.encode_as_toon(data)
  require "toon" unless defined?(Toon)
  Toon.encode(data)
end

.format_output(data, format) ⇒ Object

Format output data based on format type



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/exa/cli/base.rb', line 37

def self.format_output(data, format)
  case format
  when "json"
    JSON.pretty_generate(data.is_a?(Hash) ? data : { result: data })
  when "pretty"
    data.inspect
  when "text"
    data.to_s
  else
    data.to_s
  end
end

.resolve_api_key(flag_value) ⇒ Object

Resolve API key from flag or environment variable Flag takes precedence over environment variable



8
9
10
11
12
13
14
15
16
# File 'lib/exa/cli/base.rb', line 8

def self.resolve_api_key(flag_value)
  return flag_value if flag_value && !flag_value.empty?

  env_key = ENV["EXA_API_KEY"]
  return env_key if env_key && !env_key.empty?

  raise Exa::ConfigurationError,
        "Missing API key. Set EXA_API_KEY environment variable or use --api-key flag"
end

.resolve_output_format(flag_value) ⇒ Object

Resolve and validate output format Valid formats: json, pretty, text, toon Defaults to json



21
22
23
24
25
26
27
28
29
# File 'lib/exa/cli/base.rb', line 21

def self.resolve_output_format(flag_value)
  format = (flag_value || "json").downcase
  valid_formats = %w[json pretty text toon]

  return format if valid_formats.include?(format)

  raise Exa::ConfigurationError,
        "Invalid output format: #{format}. Valid formats: #{valid_formats.join(', ')}"
end