Class: Shutterbug::Service
- Inherits:
-
Object
- Object
- Shutterbug::Service
show all
- Defined in:
- lib/shutterbug/service.rb
Defined Under Namespace
Classes: HtmlFile, JSFile, PngFile, RackFile
Constant Summary
collapse
- PROGRAM =
'phantomjs'
- RASTERIZE_JS =
File.join(File.dirname(__FILE__),'rasterize.js')
- SHUTTERBUG_JS =
File.join(File.dirname(__FILE__),'shutterbug.js')
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Service.
60
61
62
63
|
# File 'lib/shutterbug/service.rb', line 60
def initialize
@file_cache = {}
@js_file = JSFile.new(SHUTTERBUG_JS)
end
|
Instance Method Details
#convert(base_url, html, css = "", width = 1000, height = 700) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/shutterbug/service.rb', line 65
def convert(base_url, html, css="", width=1000, height=700)
html_content = document(html, css, base_url)
signature = Digest::SHA1.hexdigest(html_content)[0..10]
return signature if @file_cache[signature]
infile = Tempfile.new(['phantom_page','.html'])
infile_name = infile.path
outfile = Tempfile.new(['phantom_render','.png'])
outfile_name = outfile.path
begin
infile.write(html_content)
infile.rewind
%x[#{PROGRAM} #{RASTERIZE_JS} #{infile_name} #{outfile_name} #{width}*#{height}]
@file_cache[signature] = {'png' => PngFile.new(outfile), 'html' => HtmlFile.new(infile) }
ensure
infile.close
end
return signature
end
|
#document(html, css, url_base) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/shutterbug/service.rb', line 42
def document(html, css, url_base)
date = Time.now.strftime("%Y-%m-%d (%I:%M%p)")
"""
<!DOCTYPE html>
<html>
<head>
<base href='#{url_base}'>
<meta content='text/html;charset=utf-8' http-equiv='Content-Type'>
<title>png from #{url_base} #{date}</title>
#{css}
</head>
<body>
#{html}
</body>
</html>
"""
end
|
#get_html_file(sha) ⇒ Object
93
94
95
96
97
|
# File 'lib/shutterbug/service.rb', line 93
def get_html_file(sha)
file = @file_cache[sha]['html']
file.open
return file
end
|
#get_png_file(sha) ⇒ Object
87
88
89
90
91
|
# File 'lib/shutterbug/service.rb', line 87
def get_png_file(sha)
file = @file_cache[sha]['png']
file.open
return file
end
|
#get_shutterbug_file ⇒ Object
99
100
101
102
103
|
# File 'lib/shutterbug/service.rb', line 99
def get_shutterbug_file
file = @js_file
file.open
return file
end
|