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, html, css = "", width = 1000, height = 700, format = "png", quality = 10) ⇒ PhantomJob

Returns a new instance of PhantomJob.



14
15
16
17
18
19
20
21
22
23
# File 'lib/shutterbug/phantom_job.rb', line 14

def initialize(base_url, html, css="", width=1000, height=700, format="png", quality=10)
  @base_url = base_url
  @html     = html
  @css      = css
  @width    = width
  @height   = height
  @format   = format
  @quality  = quality
  @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



25
26
27
# File 'lib/shutterbug/phantom_job.rb', line 25

def cache_key
  @cache_key ||= Digest::SHA1.hexdigest("#{@html}#{@css}#{@base_url}#{@format}#{@quality}")[0..10]
end

#documentObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shutterbug/phantom_job.rb', line 29

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



47
48
49
# File 'lib/shutterbug/phantom_job.rb', line 47

def html_file_name
  "#{cache_key}.html"
end

#image_file_nameObject



51
52
53
# File 'lib/shutterbug/phantom_job.rb', line 51

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

#input_pathObject



55
56
57
# File 'lib/shutterbug/phantom_job.rb', line 55

def input_path
  @config.fs_path_for(html_file_name)
end

#output_pathObject



59
60
61
# File 'lib/shutterbug/phantom_job.rb', line 59

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



67
68
69
70
71
72
73
74
# File 'lib/shutterbug/phantom_job.rb', line 67

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



63
64
65
# File 'lib/shutterbug/phantom_job.rb', line 63

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