diff --git a/lib/simple_websocket_vcr/cassette.rb b/lib/simple_websocket_vcr/cassette.rb index f51ba12..592b893 100644 --- a/lib/simple_websocket_vcr/cassette.rb +++ b/lib/simple_websocket_vcr/cassette.rb @@ -17,24 +17,7 @@ def initialize(name, options) if File.exist?(filename) @recording = false - file_content = File.open(filename, &:read) - - # do the ERB substitution - unless @options[:erb].nil? - require 'ostruct' - namespace = OpenStruct.new(@options[:erb]) - file_content = ERB.new(file_content).result(namespace.instance_eval{ binding }) - end - - # parse JSON/YAML - if @using_json - parsed_content = JSON.parse(file_content) - @sessions = RecordedJsonSession.load(parsed_content) - else - parsed_content = YAML.load(file_content) - @sessions = RecordedYamlSession.load(parsed_content) - end - + @sessions = initialize_sessions filename else fail "No cassette '#{name}' found and recording has been turned off" if @options[:record] == :none @recording = true @@ -60,7 +43,6 @@ def save if @using_json text = JSON.pretty_generate(@sessions.map(&:record_entries)) else - # TODO: :data: !ruby/string:WebSocket::Frame::Data 'GenericErrorResponse={"e.. text = { 'websocket_interactions' => @sessions.map(&:record_entries) }.to_yaml(Indent: 8) end File.open(filename, 'w') { |f| f.write(text) } @@ -71,6 +53,29 @@ def save def filename "#{WebSocketVCR.configuration.cassette_library_dir}/#{name}" end + + private + + def initialize_sessions(filename) + file_content = File.open(filename, &:read) + + # do the ERB substitution + unless @options[:erb].nil? + require 'ostruct' + namespace = OpenStruct.new(@options[:erb]) + file_content = ERB.new(file_content).result(namespace.instance_eval { binding }) + end + + # parse JSON/YAML + if @using_json + parsed_content = JSON.parse(file_content) + sessions = RecordedJsonSession.load(parsed_content) + else + parsed_content = YAML.load(file_content) + sessions = RecordedYamlSession.load(parsed_content) + end + sessions + end end class RecordedSession diff --git a/spec/vcr_spec.rb b/spec/vcr_spec.rb index af43d5f..eaf5008 100644 --- a/spec/vcr_spec.rb +++ b/spec/vcr_spec.rb @@ -229,7 +229,7 @@ def test_complex prefix = WebSocketVCR.configuration.cassette_library_dir cassette_path = '/EXPLICIT/something_nonexistent' expect do - WebSocketVCR.use_cassette(cassette_path, {record: :none}) do + WebSocketVCR.use_cassette(cassette_path, record: :none) do fail 'this code should not be reachable' end end.to raise_error(RuntimeError) @@ -256,7 +256,7 @@ def test_substitution(text1, text2 = nil) WebSocketVCR.configure do |c| c.hook_uris = [HOST] end - WebSocketVCR.use_cassette(cassette_path, {erb: {something: 11223344}}) do + WebSocketVCR.use_cassette(cassette_path, erb: { something: 11_223_344 }) do test_substitution '11223344' end file_path = "#{WebSocketVCR.configuration.cassette_library_dir}#{cassette_path}.yml" @@ -269,7 +269,7 @@ def test_substitution(text1, text2 = nil) c.hook_uris = [HOST] c.json_cassettes = true end - WebSocketVCR.use_cassette(cassette_path, {erb: {something: 'world', bar: 'hello'}}) do + WebSocketVCR.use_cassette(cassette_path, erb: { something: 'world', bar: 'hello' }) do test_substitution 'world', 'hello' end end