Class: Explicit::TestHelper::ExampleRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/explicit/test_helper/example_recorder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExampleRecorder

Returns a new instance of ExampleRecorder.



22
23
24
# File 'lib/explicit/test_helper/example_recorder.rb', line 22

def initialize
  @examples = Concurrent::Map.new { |hash, key| hash[key] = [] }
end

Class Method Details

.instanceObject



15
16
17
18
19
# File 'lib/explicit/test_helper/example_recorder.rb', line 15

def instance
  return @remote_instance if defined?(@remote_instance)

  @local_instance ||= new
end

.set_remote_instance(uri) ⇒ Object



9
10
11
12
13
# File 'lib/explicit/test_helper/example_recorder.rb', line 9

def set_remote_instance(uri)
  ::DRb.stop_service

  @remote_instance = ::DRbObject.new_with_uri(uri)
end

.start_serviceObject



5
6
7
# File 'lib/explicit/test_helper/example_recorder.rb', line 5

def start_service
  ::DRb.start_service("drbunix:", instance).uri
end

Instance Method Details

#add(request_gid:, params:, headers:, response:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/explicit/test_helper/example_recorder.rb', line 26

def add(request_gid:, params:, headers:, response:)
  @examples[request_gid] << Explicit::Request::Example.new(
    request: nil,
    params:,
    headers:,
    response:
  )
end

#save!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/explicit/test_helper/example_recorder.rb', line 35

def save!
  examples_hash = @examples.keys.map do |key|
    [key, @examples[key]]
  end.to_h

  total_examples_count = examples_hash.sum { _2.size }
  file_path = Explicit.configuration.request_examples_file_path

  puts "" if Explicit.configuration.test_runner == :rspec
  puts ""
  puts "  [Explicit] ========="
  puts "  [Explicit] Saving request examples to #{file_path}"
  puts "  [Explicit] #{total_examples_count} requests recorded"
  puts "  [Explicit] ========="
  puts "" if Explicit.configuration.test_runner == :minitest

  ::File.write(file_path, examples_hash.to_json, mode: "w")
end