Class: Yabbie::Slimer

Inherits:
Object
  • Object
show all
Defined in:
lib/yabbie/slimer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_or_file, opts = {}) ⇒ Slimer

Returns a new instance of Slimer.

Raises:



9
10
11
12
13
14
# File 'lib/yabbie/slimer.rb', line 9

def initialize(url_or_file, opts = {})
  @source  = Source.new(url_or_file)
  @options = Yabbie.configuration.default_options.merge(opts)

  raise NoExecutableError.new(options[:slimerjs]) unless command?(options[:slimerjs])
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



6
7
8
# File 'lib/yabbie/slimer.rb', line 6

def configuration
  @configuration
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/yabbie/slimer.rb', line 7

def error
  @error
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/yabbie/slimer.rb', line 7

def options
  @options
end

#resultObject (readonly)

Returns the value of attribute result.



7
8
9
# File 'lib/yabbie/slimer.rb', line 7

def result
  @result
end

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/yabbie/slimer.rb', line 6

def source
  @source
end

Instance Method Details

#cmdObject



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

def cmd
  [
    prepend_command,
    slimerjs,
    command_config_file,
    command_error_log,
    script,
    @source.to_s.shellescape,
    outfile,
    width,
    height,
    format,
    timeout
  ].join(" ").strip
end

#render(path = nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/yabbie/slimer.rb', line 44

def render(path = nil)
  @outfile = File.expand_path(path) if path
  self.run

  raise Yabbie::RenderingError.new(@error) if @error

  @outfile
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/yabbie/slimer.rb', line 16

def run
  @error  = nil
  @result = `#{cmd}`

  unless $?.exitstatus == 0
    @error  = @result
    @result = nil
  end

  @result
end

#to_file(path = nil) ⇒ Object



53
54
55
56
# File 'lib/yabbie/slimer.rb', line 53

def to_file(path = nil)
  self.render(path)
  File.new(@outfile)
end

#to_string(path = nil) ⇒ Object



58
59
60
# File 'lib/yabbie/slimer.rb', line 58

def to_string(path = nil)
  File.open(self.render(path)).read
end