Sha256: 9f522504cc319ac217d5734e78ddc085af3951da7a33c608342c9849ab951a67

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

module Teaspoon
  class Exporter
    include Teaspoon::Utility

    def initialize(suite, url, output_path)
      @suite = suite
      @url = url
      @output_path = File.join(File.expand_path(output_path || "export"), @suite.to_s)
    end

    def export
      Dir.mktmpdir do |temp_path|
        Dir.chdir(temp_path) do
          %x{#{executable} --convert-links --adjust-extension --page-requisites --span-hosts #{@url.shellescape} 2>&1}
          raise Teaspoon::DependencyError.new("Unable to export #{@suite} suite.") unless $?.exitstatus == 0
          create_export(File.join(temp_path, @url.match(/^http:\/\/([^\/]+).*/)[1]))
        end
      end
    end

    private

      def executable
        return @executable if @executable
        @executable = which("wget")
        return @executable unless @executable.blank?
        raise Teaspoon::MissingDependencyError.new("Unable to locate `wget` for exporter.")
      end

      def create_export(path)
        Dir.chdir(path) do
          update_relative_paths
          cleanup_output
          move_output
        end
      end

      def update_relative_paths
        html = File.read(".#{Teaspoon.configuration.mount_at}/#{@suite}.html")
        File.write("index.html", html.gsub!('"../', '"'))
      end

      def cleanup_output
        FileUtils.rm_r(Dir["{.#{Teaspoon.configuration.mount_at},robots.txt.html}"])
      end

      def move_output
        FileUtils.mkdir_p(@output_path)
        FileUtils.mv(Dir["*"], @output_path, force: true)
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
teaspoon-1.4.0 lib/teaspoon/exporter.rb
teaspoon-1.2.2 lib/teaspoon/exporter.rb
teaspoon-1.2.1 lib/teaspoon/exporter.rb
teaspoon-1.2.0 lib/teaspoon/exporter.rb