Skip to content

Commit

Permalink
rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
jkremser committed Apr 4, 2016
1 parent c0e1dbf commit 578c95f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
43 changes: 24 additions & 19 deletions lib/simple_websocket_vcr/cassette.rb
Expand Up @@ -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
Expand All @@ -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) }
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/vcr_spec.rb
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 578c95f

Please sign in to comment.