Class: RailsResponseDumper::Runner
- Inherits:
-
Object
- Object
- RailsResponseDumper::Runner
- Defined in:
- lib/rails_response_dumper/runner.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options) ⇒ Runner
constructor
A new instance of Runner.
- #run_dumps ⇒ Object
Constructor Details
#initialize(options) ⇒ Runner
Returns a new instance of Runner.
10 11 12 |
# File 'lib/rails_response_dumper/runner.rb', line 10 def initialize() @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/rails_response_dumper/runner.rb', line 8 def @options end |
Instance Method Details
#run_dumps ⇒ Object
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rails_response_dumper/runner.rb', line 14 def run_dumps dumps_dir = ['dumps-dir'] if [:filenames].present? globs = [:filenames] else globs = ['dumpers/**/*_dumper.rb'] FileUtils.rm_rf dumps_dir end FileUtils.mkdir_p dumps_dir globs.each do |glob| Dir[Rails.root.join(glob)].each { |f| require f } end errors = [] dumper_blocks = RailsResponseDumper::Defined.dumpers.flat_map do |defined| if [:filenames].present? # clear previous dumps for that file in case removed from updated dump file FileUtils.rm_rf "#{dumps_dir}/#{defined.name.underscore}/" end defined.blocks.map do |dump_block| [defined, dump_block] end end if [:order].present? seed = if [:order] == 'random' Random.new_seed else Integer([:order]) end puts "Randomized with seed #{seed}" random = Random.new(seed) dumper_blocks.shuffle!(random: random) end profile = {} catch :fail_fast do dumper_blocks.each do |(defined, dump_block)| name = "#{defined.name}.#{dump_block.name}" print "#{name} " if [:verbose] defined.reset_models! dumper = defined.klass.new dumper.mock_setup begin rollback_after do t0 = Time.now dumper.instance_eval(&defined.before_block) if defined.before_block begin dumper.instance_eval(&dump_block.block) ensure dumper.instance_eval(&defined.after_block) if defined.after_block end profile[name] = Time.now - t0 end ensure dumper.mock_teardown end unless dumper.responses.count == dump_block.expected_status_codes.count raise <<~ERROR.squish #{dumper.responses.count} responses (expected #{dump_block.expected_status_codes.count}) ERROR end klass_path = defined.name.underscore dumper_dir = "#{dumps_dir}/#{klass_path}/#{dump_block.name}" FileUtils.mkdir_p dumper_dir dumper.responses.each_with_index do |(response, ), index| unless response.status == dump_block.expected_status_codes[index] raise <<~ERROR.squish unexpected status code #{response.status} #{response.} (expected #{dump_block.expected_status_codes[index]}) ERROR end request = response.request dump = { request: { url: request.url, env: request.headers.to_h }, response: { status: response.status, statusText: response., headers: response.headers } } # request.headers includes nonstandard internal data, some of which also lacks default deterministic # serialization. Here we only want CGI standard and HTTP variables. dump[:request][:env].filter! do |key| key.in?(ActionDispatch::Http::Headers::CGI_VARIABLES) || key.start_with?('HTTP_') end dump[:response].delete(:headers) if [:exclude_response_headers] dump[:timestamp] = .iso8601 unless [:exclude_timestamp] File.write("#{dumper_dir}/#{index}.json", JSON.pretty_generate(dump)) File.write("#{dumper_dir}/#{index}.request_body", request.body&.string, mode: 'wb') File.write("#{dumper_dir}/#{index}.response_body", response.body, mode: 'wb') end RailsResponseDumper.print_color('.', :green) print("\n") if [:verbose] rescue StandardError => e errors << { dumper_location: dump_block.block.source_location.join(':'), name: name, exception: e } RailsResponseDumper.print_color('F', :red) print("\n") if [:verbose] throw :fail_fast if [:fail_fast] end end puts unless errors.blank? puts errors.each do |error| RailsResponseDumper.print_color( "#{error[:dumper_location]} #{error[:name]} received #{error[:exception]}\n", :red ) error[:exception].(highlight: RailsResponseDumper::COLORIZE).lines do |line| RailsResponseDumper.print_color(line, :cyan) end puts end end if .include?(:profile) puts # Sort in descending order to obtain the 10 slowest dumps. timings = profile.to_a.sort_by { |(_key, value)| -value } timings.first(10).each do |(dump, time)| formatted_time = format('%.2f', time) puts "#{dump} #{formatted_time} s" end puts end exit(errors.blank?) end |