Class: WebSnap
- Inherits:
-
Object
- Object
- WebSnap
- Defined in:
- lib/websnap/source.rb,
lib/websnap/websnap.rb
Defined Under Namespace
Classes: ImproperSourceError, NoExecutableError, Source
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source ⇒ Object
Returns the value of attribute source.
-
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
Instance Method Summary collapse
- #command ⇒ Object
-
#initialize(url_file_or_html, options = {}) ⇒ WebSnap
constructor
A new instance of WebSnap.
- #to_bytes ⇒ Object
- #to_file(path) ⇒ Object
Constructor Details
#initialize(url_file_or_html, options = {}) ⇒ WebSnap
Returns a new instance of WebSnap.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/websnap/websnap.rb', line 18 def initialize(url_file_or_html, ={}) @source = Source.new(url_file_or_html) @stylesheets = [] = { :'crop-h' => '768', :'crop-w' => '1024', :'crop-x' => '0', :'crop-y' => '0', :'format' => 'png' } @options = (.merge()) raise NoExecutableError.new if wkhtmltoimage.nil? || wkhtmltoimage == '' end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/websnap/websnap.rb', line 16 def @options end |
#source ⇒ Object
Returns the value of attribute source.
15 16 17 |
# File 'lib/websnap/websnap.rb', line 15 def source @source end |
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
15 16 17 |
# File 'lib/websnap/websnap.rb', line 15 def stylesheets @stylesheets end |
Instance Method Details
#command ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/websnap/websnap.rb', line 35 def command args = [wkhtmltoimage] args += @options.to_a.flatten.compact if @source.html? args << '-' # Get HTML from stdin else args << @source.to_s end args << '-' # Read PDF from stdout args.join(' ') end |
#to_bytes ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/websnap/websnap.rb', line 49 def to_bytes img = IO.popen(command, "w+") img.puts(@source.to_s) if @source.html? img.close_write result = img.gets(nil) img.close_read return result end |
#to_file(path) ⇒ Object
58 59 60 |
# File 'lib/websnap/websnap.rb', line 58 def to_file(path) File.open(path,'w') {|file| file << self.to_bytes} end |