Sha256: 327d84afa968baf6973993640f7470681a3affdb095ca54a5b5ec1f2105c7515

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

module WickedPdf
  class Command
    attr_reader :binary, :option_parser

    def initialize(binary: Binary.new, option_parser: OptionParser.new)
      @binary = binary
      @option_parser = option_parser
    end

    def execute(options, *args)
      command = [binary.path]
      command += option_parser.parse(options)
      command += args

      print_command(command.inspect) if in_development_mode?

      if track_progress?(options)
        Progress.new(options[:progress]).execute(command)
      else
        begin
          err = Open3.popen3(*command) do |_stdin, _stdout, stderr|
            stderr.read
          end
        rescue StandardError => e
          raise "Failed to execute:\n#{command}\nError: #{e}"
        end

        raise "Error generating PDF\n Command Error: #{err}" if options[:raise_on_all_errors] && !err.empty?
        err
      end
    end

    private

    def in_development_mode?
      defined?(Rails.env) && Rails.env.development?
    end

    def print_command(cmd)
      # TODO: if no Rails what then?
      Rails.logger.debug '[wicked_pdf]: ' + cmd
    end

    def track_progress?(options)
      options[:progress] && !on_windows?
    end

    def on_windows?
      RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adzap-wicked_pdf-2.0.0.beta3 lib/wicked_pdf/command.rb
adzap-wicked_pdf-2.0.0.beta2 lib/wicked_pdf/command.rb