class Shutterbug::PhantomJob

Attributes

html_file[RW]
image_file[RW]

Public Class Methods

new(base_url, options) click to toggle source
# File lib/shutterbug/phantom_job.rb, line 31
def initialize(base_url, options)
  @base_url = base_url
  @html     = options[:html]    || ""
  @css      = options[:css]     || ""
  @width    = options[:width]   || 1000
  @height   = options[:height]  || 700
  @format   = options[:format]  || "png"
  @quality  = options[:quality] || 1
  @quality  = convert_quality(@quality, @format)
  @config   = Configuration.instance
end

Public Instance Methods

cache_key() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 43
def cache_key
  @cache_key ||= Digest::SHA1.hexdigest("#{@html}#{@css}#{@base_url}#{@format}#{@quality}")[0..10]
end
convert_quality(val, format) click to toggle source
# File lib/shutterbug/phantom_job.rb, line 14
def convert_quality(val, format)
  # Client sends quality between 0 and 1 (similar to .toDataURL() second argument).
  # This conversion tries to ensure that the size of the final image is similar to
  # .toDataURL() output with given quality settings.
  val = val.to_f
  case format
  when "png"
    val *= 10
  when "jpeg"
    val *= 100
  else
    val *= 100
  end
  # PhantomJS expects integer.
  val.to_i
end
document() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 47
def document
  date = Time.now.strftime("%Y-%m-%d (%I:%M%p)")
  """
  <!DOCTYPE html>
  <html>
    <head>
      <base href=\"#{@base_url}\">
      <meta content=\"text/html;charset=utf-8\" http-equiv=\"Content-Type\">
      <title>content from #{@base_url} #{date}</title>
      #{@css}
    </head>
    <body>
      #{@html}
    </body>
  </html>
  """
end
html_file_name() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 65
def html_file_name
  "#{cache_key}.html"
end
image_file_name() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 69
def image_file_name
  "#{cache_key}.#{@format}"
end
input_path() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 73
def input_path
  @config.fs_path_for(html_file_name)
end
output_path() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 77
def output_path
  @config.fs_path_for(image_file_name)
end
program() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 6
def program
  @config.phantom_bin_path
end
rasterize() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 85
def rasterize
  File.open(input_path, 'w') do |f|
    f.write(document)
  end
  rasterize_cl()
  self.image_file = @config.storage.new(image_file_name)
  self.html_file = @config.storage.new(html_file_name)
end
rasterize_cl() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 81
def rasterize_cl
  %x[#{self.program} --ignore-ssl-errors=true --ssl-protocol=any #{self.rasterize_js} #{self.input_path} #{self.output_path} #{@width}*#{@height} #{@quality}]
end
rasterize_js() click to toggle source
# File lib/shutterbug/phantom_job.rb, line 10
def rasterize_js
  File.join(File.dirname(__FILE__),'rasterize.js')
end