Class: Shutterbug::PhantomJob
- Inherits:
-
Object
- Object
- Shutterbug::PhantomJob
- Defined in:
- lib/shutterbug/phantom_job.rb
Instance Attribute Summary collapse
-
#html_file ⇒ Object
Returns the value of attribute html_file.
-
#image_file ⇒ Object
Returns the value of attribute image_file.
Instance Method Summary collapse
- #cache_key ⇒ Object
- #convert_quality(val, format) ⇒ Object
- #document ⇒ Object
- #html_file_name ⇒ Object
- #image_file_name ⇒ Object
-
#initialize(base_url, options) ⇒ PhantomJob
constructor
A new instance of PhantomJob.
- #input_path ⇒ Object
- #output_path ⇒ Object
- #program ⇒ Object
- #rasterize ⇒ Object
- #rasterize_cl ⇒ Object
- #rasterize_js ⇒ Object
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, ) @base_url = base_url @html = [:html] || "" @css = [:css] || "" @width = [:width] || 1000 @height = [:height] || 700 @format = [:format] || "png" @quality = [:quality] || 1 @quality = convert_quality(@quality, @format) @config = Configuration.instance end |
Instance Attribute Details
#html_file ⇒ Object
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_file ⇒ Object
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_key ⇒ Object
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 |
#document ⇒ Object
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_name ⇒ Object
65 66 67 |
# File 'lib/shutterbug/phantom_job.rb', line 65 def html_file_name "#{cache_key}.html" end |
#image_file_name ⇒ Object
69 70 71 |
# File 'lib/shutterbug/phantom_job.rb', line 69 def image_file_name "#{cache_key}.#{@format}" end |
#input_path ⇒ Object
73 74 75 |
# File 'lib/shutterbug/phantom_job.rb', line 73 def input_path @config.fs_path_for(html_file_name) end |
#output_path ⇒ Object
77 78 79 |
# File 'lib/shutterbug/phantom_job.rb', line 77 def output_path @config.fs_path_for(image_file_name) end |
#program ⇒ Object
6 7 8 |
# File 'lib/shutterbug/phantom_job.rb', line 6 def program @config.phantom_bin_path end |
#rasterize ⇒ Object
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_cl ⇒ Object
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_js ⇒ Object
10 11 12 |
# File 'lib/shutterbug/phantom_job.rb', line 10 def rasterize_js File.join(File.dirname(__FILE__),'rasterize.js') end |