Class: RSpecRcv::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-rcv/handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path, data, metadata: {}) ⇒ Handler

Returns a new instance of Handler.



6
7
8
9
10
# File 'lib/rspec-rcv/handler.rb', line 6

def initialize(file_path, data, metadata: {})
  @file_path = file_path
  @opts = RSpecRcv.config()
  @data = data.call(@opts)
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rspec-rcv/handler.rb', line 12

def call
  return :no_change if existing_data && existing_data["file"] == file_path && existing_data["data"] == data

  output = { recorded_at: Time.now, file: file_path, data: data }
  output = opts[:codec].export_with(output) + "\n"

  if existing_data
    existing_data_comparison = opts.fetch(:parse_existing).call(existing_data)
    eq = opts[:compare_with].call(existing_data_comparison, data, opts)

    if !eq && opts[:fail_on_changed_output]
      raise_error!(output, JsonCompare.get_diff(existing_data_comparison, data, opts.fetch(:ignore_keys, [])))
    end

    return :same if eq
  end

  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'w') do |file|
    file.write(output)
  end

  return :to_disk
end