Module: ResponseFaker

Defined in:
lib/response_faker.rb,
lib/response_faker/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.load_responses(file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/response_faker.rb', line 11

def load_responses(file)
  begin
    responses = JSON.parse(File.read(file))
  rescue JSON::ParserError => e
    raise ResponseFaker::Error, "Invalid JSON format in file: #{file}. Error: #{e.message}"
  end

  class_name = File.basename(file, ".*").split('_').map(&:capitalize).join
  klass = const_set(class_name, Class.new)

  responses.each do |key, value|
    klass.define_singleton_method(key) { value }
  end
end

.reload_responsesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/response_faker.rb', line 26

def reload_responses
  directory_path = "./lib/response_faker/responses/"

  # Iterate over each file in the directory
  Dir.glob(File.join(directory_path, "*")).each do |file|
    begin
      load_responses(file)
      puts "Loaded: #{file}"
    rescue ResponseFaker::Error => e
      puts "Skipping file due to error: #{e.message}"
    end
  end
end