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
# 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
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

#base_pathObject



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

def base_path
  Configuration.instance.resource_dir
end

#cache_keyObject



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

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

#documentObject



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

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

#infilenameObject



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

def infilename
  File.join(base_path,"phantom_#{cache_key}.html")
end

#outfilenameObject



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

def outfilename
  File.join(base_path,"phantom_#{cache_key}.png")
end

#programObject



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

def program
  Configuration.instance.phantom_bin_path
end

#rasterizeObject



61
62
63
64
65
66
67
68
# File 'lib/shutterbug/phantom_job.rb', line 61

def rasterize
  File.open(infilename, 'w') do |f|
    f.write(document)
  end
  rasterize_cl()
  self.png_file  = PngFile.new(outfilename)
  self.html_file = HtmlFile.new(infilename)
end

#rasterize_clObject



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

def rasterize_cl
  %x[#{self.program} #{self.rasterize_js} #{self.infilename} #{self.outfilename} #{@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