-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert_vcr_response_to_fixture.rb
executable file
·79 lines (59 loc) · 1.97 KB
/
convert_vcr_response_to_fixture.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
# warn_indent: true
##########################################################
###
## File: convert_vcr_response_to_fixture.rb
## Desc: Change a VCR cassette response into a fixture
## By: Dewayne VanHoozer ([email protected])
#
require 'pathname'
SPEC_DIR = Pathname.new(__dir__)
FIXTURES_DIR = SPEC_DIR + "fixtures"
CASSETTE_DIR = SPEC_DIR + "vcr_cassettes"
require 'bundler/inline'
print "Installing gems as necessary ... "
gemfile do
source 'https://rubygems.org'
# gem 'amazing_print' # Pretty print Ruby objects with proper indentation and colors
gem 'multi_json'
gem 'yaml'
end
puts "done."
def pretty_print_json(text)
hash = MultiJson.load( text )
MultiJson.dump( hash, {pretty: true} )
end
###################################################
## Main
#
# breath first directory/sub-directory processing ...
sub_directories = [CASSETTE_DIR]
sub_directories.each do |cassette_dir|
cassette_dir.children.each do |cassette|
if cassette.directory?
sub_directories << cassette
next
end
next unless '.yml' == cassette.extname
filename_yml = cassette.basename.to_s
yaml_hash = YAML.load(cassette.read)
next unless yaml_hash.is_a? Hash
next unless yaml_hash.has_key? "http_interactions"
# Some VCR cassettes have multiple request/response pairs
yaml_hash["http_interactions"].each_with_index do |entry, inx|
if 0 == inx
filename_json = filename_yml.gsub('.yml', '.json')
else
filename_json = filename_yml.gsub('.yml', "_#{inx}.json")
end
json_response_text = entry.dig('response', 'body', 'string')
next if json_response_text.chomp.strip.empty?
source_dir = cassette_dir.to_s
target_dir = Pathname.new source_dir.gsub(CASSETTE_DIR.to_s,FIXTURES_DIR.to_s)
json_path = target_dir + filename_json
json_path.write pretty_print_json(json_response_text)
end
end
end