require 'phantomjs'

class Snapshotter
  def initialize(url)
    @url = url
  end

  def take_snapshot!
    FileUtil.with_tempfile do |snapshot_file|
      opts = {
        address: @url.address,
        outfile: snapshot_file,
        viewportSize: {
          width:  @url.viewport_width,
          height: @url.viewport_width * 2
        }
      }
      Phantomjs.run(Rails.root.join('script', 'take-snapshot.js').to_s,
                    opts.to_json) do |line|
        puts line
      end
      FileUtil.upload_to_cloudinary(snapshot_file)
    end
  end
end

require 'phantomjs'

class Snapshotter
  def initialize(url)
    @url = url
  end

  def take_snapshot!
    FileUtil.with_tempfile do |snapshot_file|
      opts = {
        address: @url.address,
        outfile: snapshot_file,
        viewportSize: {
          width:  @url.viewport_width,
          height: viewport_height
        }
      }
      Phantomjs.run(Rails.root.join('script', 'take-snapshot.js').to_s,
                    opts.to_json) do |line|
        puts line
      end
      FileUtil.upload_to_cloudinary(snapshot_file)
    end
  end

  private

  # @return [Integer]
  def viewport_height
    if @url.viewport_width < 960
      @url.viewport_width * 2
    else
      (@url.viewport_width * 0.75).round # 16:12
    end
  end
end

Use shorter viewport height for widths >= 960
Use shorter viewport height for widths >= 960

We were setting the viewport height to be `viewport width * 2`.

For narrow viewport widths this actually makes some sense. For example,
phones are often held in portrait orientation where the height is
somewhere within the zone of twice the width.

However, for wider viewport widths, this is fairly uncommon--laptop
screens and desktop monitors are usually in landscape orientation.

As a quick fix, I decided that viewports narrower than 960px should be
considered to be "mobile" devices and will receive heights that are
double their widths. For everyone else, I am calculating the height to
be 3/4 of the width, which matches the 16:12 ratio of 1024x768 or
1600x1200.

Fixes #20

Change-Id: I5b1597ac211ef02038152594306c5c17f74b4e5f
