Module: Phantomjs

Defined in:
lib/phantomjs.rb,
lib/phantomjs/version.rb,
lib/phantomjs/platform.rb

Defined Under Namespace

Classes: Platform

Constant Summary collapse

UnknownPlatform =
Class.new StandardError
VERSION =
'2.0.0.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.proxy_hostObject

Returns the value of attribute proxy_host.



8
9
10
# File 'lib/phantomjs.rb', line 8

def proxy_host
  @proxy_host
end

.proxy_portObject

Returns the value of attribute proxy_port.



8
9
10
# File 'lib/phantomjs.rb', line 8

def proxy_port
  @proxy_port
end

Class Method Details

.available_platformsObject



10
11
12
# File 'lib/phantomjs.rb', line 10

def available_platforms
  @available_platforms ||= []
end

.base_dirObject



14
15
16
# File 'lib/phantomjs.rb', line 14

def base_dir
   @base_dir ||= File.join(File.expand_path('~'), '.phantomjs', version)
end

.base_dir=(dir) ⇒ Object



18
19
20
# File 'lib/phantomjs.rb', line 18

def base_dir=(dir)
  @base_dir = dir
end

.implode!Object

Removes the local phantomjs copy



40
41
42
# File 'lib/phantomjs.rb', line 40

def implode!
  FileUtils.rm_rf File.join(File.expand_path('~'), '.phantomjs')
end

.pathObject



26
27
28
# File 'lib/phantomjs.rb', line 26

def path
  @path ||= platform.phantomjs_path
end

.platformObject



30
31
32
33
34
35
36
37
# File 'lib/phantomjs.rb', line 30

def platform
  if platform = available_platforms.find { |p| p.useable? }
    platform.ensure_installed!
    platform
  else
    raise UnknownPlatform, "Could not find an appropriate PhantomJS library for your platform (#{RUBY_PLATFORM} :( Please install manually."
  end
end

.reset!Object

Clears cached state. Primarily useful for testing.



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

def reset!
  @base_dir = @path = nil
end

.run(*args, &block) ⇒ Object

Run phantomjs with the given arguments, and either return the stdout or yield each line to the passed block.



51
52
53
54
55
# File 'lib/phantomjs.rb', line 51

def run(*args, &block)
  IO.popen([path, *args]) do |io|
    block ? io.each(&block) : io.read
  end
end

.versionObject



22
23
24
# File 'lib/phantomjs.rb', line 22

def version
  Phantomjs::VERSION.split('.')[0..-2].join('.')
end