Module: NxtVcrHarness

Defined in:
lib/nxt_vcr_harness.rb,
lib/nxt_vcr_harness/version.rb,
lib/nxt_vcr_harness/cassette_tracker.rb,
lib/nxt_vcr_harness/unneeded_headers.rb,
lib/nxt_vcr_harness/cassette_name_by_example.rb

Defined Under Namespace

Modules: UnneededHeaders, VcrCassetteHelper Classes: CassetteNameByExample, CassetteTracker

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.enable_vcr_cassette_helperObject



44
45
46
47
48
# File 'lib/nxt_vcr_harness.rb', line 44

def enable_vcr_cassette_helper
  RSpec.configure do |config|
    config.include VcrCassetteHelper
  end
end

.enable_vcr_tag(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nxt_vcr_harness.rb', line 27

def enable_vcr_tag(options = {})
  tag_name = options.fetch(:tag_name, :vcr_cassette)
  default_cassette_options = options.fetch(:default_cassette_options, {})

  RSpec.configure do |config|
    config.around(:each, tag_name) do |example|
      cassette_path = CassetteNameByExample.new(example).call(**options.slice(:prefix, :suffix))
      cassette_options = example.[tag_name].is_a?(TrueClass) ? {} : example.[tag_name]
      cassette_options = default_cassette_options.merge(cassette_options)

      VCR.use_cassette(cassette_path, **cassette_options) do
        example.call
      end
    end
  end
end

.strip_unneeded_headers_before_save {|headers_to_strip| ... } ⇒ Object

Yields:

  • (headers_to_strip)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nxt_vcr_harness.rb', line 70

def strip_unneeded_headers_before_save
  headers_to_strip = UnneededHeaders.default_headers_to_strip

  yield headers_to_strip if block_given?

  ::VCR.configure do |config|
    config.before_record do |interaction, _cassette|
      UnneededHeaders.strip(interaction.request.headers, headers_to_strip[:requests])
      UnneededHeaders.strip(interaction.response.headers, headers_to_strip[:responses])
    end
  end
end

.track_cassettesObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nxt_vcr_harness.rb', line 55

def track_cassettes
  RSpec.configure do |config|
    config.after(:suite) do
      CassetteTracker.instance.stats
      CassetteTracker.instance.reveal_unused_cassettes(::VCR.configuration.cassette_library_dir)
    end
  end

  VCR.configure do |config|
    config.before_playback do |_, cassette|
      CassetteTracker.instance.track(cassette)
    end
  end
end

.track_cassettes_if(condition) ⇒ Object



50
51
52
53
# File 'lib/nxt_vcr_harness.rb', line 50

def track_cassettes_if(condition)
  return unless condition
  track_cassettes
end