Class: Shutterbug::PhantomJob

Inherits:
Object
  • Object
show all
Defined in:
lib/shutterbug/phantom_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, options) ⇒ PhantomJob

Returns a new instance of PhantomJob.



31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#html_fileObject

Returns the value of attribute html_file.



4
5
6
# File 'lib/shutterbug/phantom_job.rb', line 4

def html_file
  @html_file
end

#image_fileObject

Returns the value of attribute image_file.



3
4
5
# File 'lib/shutterbug/phantom_job.rb', line 3

def image_file
  @image_file
end

Instance Method Details

#cache_keyObject



43
44
45
# 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) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

#documentObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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_nameObject



65
66
67
# File 'lib/shutterbug/phantom_job.rb', line 65

def html_file_name
  "#{cache_key}.html"
end

#image_file_nameObject



69
70
71
# File 'lib/shutterbug/phantom_job.rb', line 69

def image_file_name
  "#{cache_key}.#{@format}"
end

#input_pathObject



73
74
75
# File 'lib/shutterbug/phantom_job.rb', line 73

def input_path
  @config.fs_path_for(html_file_name)
end

#output_pathObject



77
78
79
# File 'lib/shutterbug/phantom_job.rb', line 77

def output_path
  @config.fs_path_for(image_file_name)
end

#programObject



6
7
8
# File 'lib/shutterbug/phantom_job.rb', line 6

def program
  @config.phantom_bin_path
end

#rasterizeObject



85
86
87
88
89
90
91
92
# 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_clObject



81
82
83
# 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_jsObject



10
11
12
# File 'lib/shutterbug/phantom_job.rb', line 10

def rasterize_js
  File.join(File.dirname(__FILE__),'rasterize.js')
end