Module: VHS

Extended by:
VHS
Included in:
VHS
Defined in:
lib/vhs.rb,
lib/vhs/loader.rb,
lib/vhs/cli/cli.rb,
lib/vhs/version.rb,
lib/vhs/cli/config.rb,
lib/vhs/cli/cassettes.rb

Defined Under Namespace

Modules: CLI Classes: Cassetter, Config, Configuration, Loader

Constant Summary collapse

VERSION =
'0.3.0'

Instance Method Summary collapse

Instance Method Details

#cassette_force_updatesObject

Public: forces the update of cassettes.



58
59
60
# File 'lib/vhs.rb', line 58

def cassette_force_updates
  @cassette_forced_update = true
end

#cassette_forced_updates?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/vhs.rb', line 62

def cassette_forced_updates?
  @cassette_forced_update
end

#cassette_optionsObject

TODO move cassette options to spec config file?



83
84
85
86
87
88
89
90
# File 'lib/vhs.rb', line 83

def cassette_options
  {
    erb: { api_host: config.api_host },
    allow_playback_repeats: true,
    record: cassette_forced_updates? ? :all : :once,
    match_requests_on: [:method, :uri, :body, :params]
  }
end

#cassette_update(cassette_filename) ⇒ Object



103
104
105
# File 'lib/vhs.rb', line 103

def cassette_update(cassette_filename)
  Cassetter.update cassette_filename
end

#configurationObject Also known as: config

Accessor for VHS::Configuration



23
24
25
# File 'lib/vhs.rb', line 23

def configuration
  @configuration ||= Configuration.new
end

#configure {|configuration| ... } ⇒ Object

Public: Modify VHS configuration

Example:

VHS.configure do |vhs|
  vhs.api_host = AppConfig.sandbox.rest_url
end

Yields:



18
19
20
# File 'lib/vhs.rb', line 18

def configure
  yield configuration
end

#eject_all_cassettesObject



107
108
109
110
111
# File 'lib/vhs.rb', line 107

def eject_all_cassettes
  VCR.send(:cassettes).each do |cassette|
    cassette.eject
  end
end

#loadObject



28
29
30
31
# File 'lib/vhs.rb', line 28

def load
  require "vhs/typhoeus_stub"
  @active = true
end

#load_cassette(request) ⇒ Object

Loads a cassette for the request unless one is already loaded.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vhs.rb', line 67

def load_cassette(request)
  if turned_on?
    cassette_name = VCR.cassette_name request
    request_cassette = find_cassette cassette_name

    if request_cassette.nil?
      VCR.insert_cassette cassette_name, cassette_options

      puts "~ [vhs] Loaded cassette #{ cassette_name }" if config.log_load
    elsif config.log_load
      puts "~ [vhs] Existing cassette #{ cassette_name }"
    end
  end
end

#remove_cassettesObject

TODO move to Cassetter, together with many methods here, and let this module just proxy methods to classes



115
116
117
118
# File 'lib/vhs.rb', line 115

def remove_cassettes
  #eject_all_cassettes
  VCR.remove_cassettes
end

#turn_offObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vhs.rb', line 45

def turn_off
  #TODO
  # - add a param to leave VCR on?
  # - allow to pass a block to run with VHS off?

  # we need to save all cassettes before turning off, to avoid VCR exceptions
  eject_all_cassettes

  VCR.turn_off!
  @active = false
end

#turn_onObject



37
38
39
40
41
42
43
# File 'lib/vhs.rb', line 37

def turn_on
  #TODO
  # - add a param to leave VCR on?
  # - allow to pass a block to run with VHS on?
  VCR.turn_on!
  @active = true
end

#turned_on?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/vhs.rb', line 33

def turned_on?
  @active
end

#write_cassette(request) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/vhs.rb', line 92

def write_cassette(request)
  #TODO if turned_on?
  cassette_name = VCR.cassette_name request
  request_cassette = find_cassette cassette_name

  if request_cassette
    puts "~ [vhs] Wrote cassette #{ cassette_name }" if config.log_write
    request_cassette.eject
  end
end