Class: Tng::Services::UserAppConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/tng/services/user_app_config.rb

Class Method Summary collapse

Class Method Details

.api_keyObject



6
7
8
# File 'lib/tng/services/user_app_config.rb', line 6

def self.api_key
  Tng.api_key
end

.base_urlObject



10
11
12
# File 'lib/tng/services/user_app_config.rb', line 10

def self.base_url
  Tng.base_url
end

.config_with_sourceObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tng/services/user_app_config.rb', line 25

def self.config_with_source
  auth_entry_points_with_source = []
  methods = Tng.authentication_methods
  methods.each do |method|
    auth_entry_points_with_source << {
      method: method[:method],
      file: method[:file_location],
      auth_type: method[:auth_type],
      source: extract_method_source(method[:file_location], method[:method])
    }
  end
  Tng.config[:authentication_entry_points_with_source] = auth_entry_points_with_source

  # Add source content to test examples for API requests
  if Tng.config[:test_examples]&.any?
    Tng.config[:test_examples] = Tng.config[:test_examples].map do |example|
      next example unless example.is_a?(Hash) && example["path"]

      example.merge("source" => read_test_file_content(example["path"]))
    rescue StandardError
      example
    end
  end

  Tng.config
end

.configured?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/tng/services/user_app_config.rb', line 14

def self.configured?
  Tng.api_key && Tng.base_url
end

.extract_method_source(file_path, method_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tng/services/user_app_config.rb', line 65

def self.extract_method_source(file_path, method_name)
  return unless file_path && method_name

  full_path = Rails.root.join(file_path)
  return unless File.exist?(full_path)

  file_content = File.read(full_path)
  result = Prism.parse(file_content)

  method_node = find_method_in_ast(result.value, method_name)
  return unless method_node

  start_line = method_node.location.start_line - 1
  end_line = method_node.location.end_line - 1

  lines = file_content.lines
  method_source = lines[start_line..end_line].join
  method_source.strip
rescue StandardError
  nil
end

.find_method_in_ast(node, method_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tng/services/user_app_config.rb', line 52

def self.find_method_in_ast(node, method_name)
  return unless node.is_a?(Prism::Node)

  return node if node.is_a?(Prism::DefNode) && node.name == method_name.to_sym

  node.child_nodes.each do |child|
    result = find_method_in_ast(child, method_name)
    return result if result
  end

  nil
end

.missing_configObject



18
19
20
21
22
23
# File 'lib/tng/services/user_app_config.rb', line 18

def self.missing_config
  missing = []
  missing << "API key" unless api_key
  missing << "Base URL" unless base_url
  missing
end

.read_test_file_content(relative_path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tng/services/user_app_config.rb', line 87

def self.read_test_file_content(relative_path)
  return nil unless relative_path

  rails_root = defined?(Rails) && Rails.root ? Rails.root.to_s : Dir.pwd
  full_path = File.join(rails_root, relative_path)

  return nil unless File.exist?(full_path) && File.readable?(full_path)

  File.read(full_path)
rescue StandardError
  nil
end