File: page_dump_helper.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (35 lines) | stat: -rw-r--r-- 1,137 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

module ActionDispatch
  module TestHelpers
    module PageDumpHelper
      class InvalidResponse < StandardError; end

      # Saves the content of response body to a file and tries to open it in your browser.
      # Launchy must be present in your Gemfile for the page to open automatically.
      def save_and_open_page(path = html_dump_default_path)
        save_page(path).tap { |s_path| open_file(s_path) }
      end

      private
        def save_page(path = html_dump_default_path)
          raise InvalidResponse.new("Response is a redirection!") if response.redirection?
          path = Pathname.new(path)
          path.dirname.mkpath
          File.write(path, response.body)
          path
        end

        def open_file(path)
          require "launchy"
          Launchy.open(path)
        rescue LoadError
          warn "File saved to #{path}.\nPlease install the launchy gem to open the file automatically."
        end

        def html_dump_default_path
          Rails.root.join("tmp/html_dump", "#{method_name}_#{DateTime.current.to_i}.html").to_s
        end
    end
  end
end