Module: Typesense::Config

Included in:
Typesense
Defined in:
lib/typesense/config.rb

Constant Summary collapse

@@pagination_backend =
nil
@@log_level =
nil
@@configuration =
nil

Instance Method Summary collapse

Instance Method Details

#clientObject



63
64
65
66
# File 'lib/typesense/config.rb', line 63

def client
  setup_client if @client.nil?
  @client
end

#configurationObject



10
11
12
13
# File 'lib/typesense/config.rb', line 10

def configuration
  @@configuration || raise(NotConfigured,
                           "Please configure Typesense. Set Typesense.configuration = {nodes: [{host: 'localhost', port: 8108, protocol: 'http'}], api_key: 'xyz'}")
end

#configuration=(configuration) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/typesense/config.rb', line 15

def configuration=(configuration)
  @@pagination_backend = configuration[:pagination_backend] if configuration.key?(:pagination_backend)
  @@log_level = configuration[:log_level] if configuration.key?(:log_level)
  @@configuration = configuration
  @client = nil
  reset_server_version_cache!
end

#debug_infoObject



79
80
81
# File 'lib/typesense/config.rb', line 79

def debug_info
  @debug_info ||= client.debug.retrieve
end

#initiliazeObject



6
7
8
# File 'lib/typesense/config.rb', line 6

def initiliaze
  @client = nil
end

#log_levelObject



27
28
29
# File 'lib/typesense/config.rb', line 27

def log_level
  defined?(@@log_level) ? @@log_level : nil
end

#log_level_to_const(level) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/typesense/config.rb', line 31

def log_level_to_const(level)
  # Be more forgiving in inputs.
  # Accepts Integer (e.g., Logger::WARN), Symbol/String (e.g., :warn, "warn", "WARN", "Logger::WARN")
  return level if level.is_a?(Integer)
  return Logger::WARN if level.nil?

  str = level.to_s

  # Handle fully-qualified constants like "Logger::WARN"
  if str.include?("::")
    const = str.split("::").last
    return Logger.const_get(const) if Logger.const_defined?(const)
  end

  # Normalize common misnomer
  upper = str.upcase
  upper = "WARN" if upper == "WARNING"
  return Logger.const_get(upper) if Logger.const_defined?(upper)

  # Fallback to explicit mapping
  case str.downcase.to_sym
  when :debug then Logger::DEBUG
  when :info then Logger::INFO
  when :warn, :warning then Logger::WARN
  when :error then Logger::ERROR
  when :fatal then Logger::FATAL
  when :unknown then Logger::UNKNOWN
  else
    Logger::WARN
  end
end

#pagination_backendObject



23
24
25
# File 'lib/typesense/config.rb', line 23

def pagination_backend
  @@pagination_backend
end

#reset_server_version_cache!Object



83
84
85
86
# File 'lib/typesense/config.rb', line 83

def reset_server_version_cache!
  @server_major_version = nil
  @debug_info = nil
end

#server_major_versionObject



72
73
74
75
76
77
# File 'lib/typesense/config.rb', line 72

def server_major_version
  @server_major_version ||= begin
    version = debug_info.fetch("version", "")
    version == "nightly" ? 30 : version.split(".").first.to_i
  end
end

#setup_clientObject



68
69
70
# File 'lib/typesense/config.rb', line 68

def setup_client
  @client = Typesense::Client.new(@@configuration)
end