Sha256: 2f5b11b9954d4a7bead719576ad707ef946b784138c511794b6989a1b42e7fa8

Contents?: true

Size: 1.64 KB

Versions: 8

Compression:

Stored size: 1.64 KB

Contents

module Perus::Pinger
    class Screenshot < Command
        description 'Takes a screenshot of the primary screen on the client and
                     uploads it. The screenshot is saved to "path" before being
                     uploaded. Valid values for "path" are contained in the
                     pinger config file.'
        option :path, default: '/tmp/screenshot.jpg', restricted: true
        option :resize, default: '20%'
        metric!

        def run
            if darwin?
                if options.resize[-1] != '%'
                    raise 'Non percent resize option unsupported by OS X currently'
                else
                    percent = options.resize.to_f / 100
                end

                shell("screencapture -m -t jpg -x #{options.path}")
                width  = shell("sips -g pixelWidth #{options.path}").match(/pixelWidth: (\d+)/)[1]
                height = shell("sips -g pixelHeight #{options.path}").match(/pixelHeight: (\d+)/)[1]

                # sips helpfully prints data to stderr, so it's run with
                # backticks to avoid thowing an exception on success
                `sips -z #{height.to_i * percent} #{width.to_i * percent} #{options.path}`
            else
                shell("export DISPLAY=:0; import -window root -resize #{options.resize} #{options.path}")
            end

            @screenshot_file = File.new(options.path)
            {screenshot: @screenshot_file}
        end

        def cleanup
            unless @screenshot_file.nil? || @screenshot_file.closed?
                @screenshot_file.close
            end

            File.delete(options.path)
        end
    end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
perus-0.1.10 lib/perus/pinger/metrics/screenshot.rb
perus-0.1.9 lib/perus/pinger/metrics/screenshot.rb
perus-0.1.8 lib/perus/pinger/metrics/screenshot.rb
perus-0.1.7 lib/perus/pinger/metrics/screenshot.rb
perus-0.1.6 lib/perus/pinger/metrics/screenshot.rb
perus-0.1.5 lib/perus/pinger/metrics/screenshot.rb
perus-0.1.4 lib/perus/pinger/metrics/screenshot.rb
perus-0.1.3 lib/perus/pinger/metrics/screenshot.rb