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) ⇒ PhantomJob

Returns a new instance of PhantomJob.



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

def initialize(base_url, html, css="", width=1000, height=700)
  @base_url = base_url
  @html     = html
  @css      = css
  @width    = width
  @height   = height
  @config   = Configuration.instance
end

Instance Attribute Details

#html_fileObject

Returns the value of attribute html_file.



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

def html_file
  @html_file
end

#png_fileObject

Returns the value of attribute png_file.



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

def png_file
  @png_file
end

Instance Method Details

#cache_keyObject



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

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

#documentObject



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

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



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

def html_file_name
  "#{cache_key}.html"
end

#input_pathObject



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

def input_path
  @config.fs_path_for(html_file_name)
end

#output_pathObject



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

def output_path
  @config.fs_path_for(png_file_name)
end

#png_file_nameObject



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

def png_file_name
  "#{cache_key}.png"
end

#programObject



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

def program
  @config.phantom_bin_path
end

#rasterizeObject



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

def rasterize
  File.open(input_path, 'w') do |f|
    f.write(document)
  end
  rasterize_cl()
  self.png_file  = @config.storage.new(png_file_name, Shutterbug::Handlers::FileHandlers::PngFile.new)
  self.html_file = @config.storage.new(html_file_name,  Shutterbug::Handlers::FileHandlers::HtmlFile.new)
end

#rasterize_clObject



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

def rasterize_cl
  %x[#{self.program} #{self.rasterize_js} #{self.input_path} #{self.output_path} #{@width}*#{@height}]
end

#rasterize_jsObject



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

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