module Pictate

Public Instance Methods

pictate(image_path, options = {}) click to toggle source

Starts the server, gets the best result for the image and stops server.

# File lib/pictate.rb, line 12
def pictate(image_path, options = {})
  SpinningCursor.start do
    action do
      start_server options[:server]
      SpinningCursor.set_banner "I'm thinking"
      SpinningCursor.set_message (do_search image_path)
      stop_server
    end
  end
end
start_server(server_path) click to toggle source

Starts the server. Checks to see if you’ve given it a server path, if not it will check the ext directory of the gem library. If it’s not there, it will download it to that directory.

Note

perhaps a better directory is needed as it is overwritten when the gem is reinstalled.

# File lib/pictate.rb, line 31
def start_server(server_path)
  if server_path
    selenium_path = server_path
  else
    previous_wd = FileUtils.pwd
    begin
      FileUtils.cd(File.join ENV['HOME'], '.selenium')
    rescue
      FileUtils.mkdir(File.join ENV['HOME'], '.selenium')
      FileUtils.cd(File.join ENV['HOME'], '.selenium')
    end
    selenium_latest = Selenium::Server.latest
    if not File.exists? "selenium-server-standalone-#{selenium_latest}.jar"
      @download = true
      SpinningCursor.set_banner "Downloading latest version of selenium " +
        "server standalone"
    end
    @selenium_path = Selenium::Server.download(selenium_latest)
  end

  SpinningCursor.set_banner "Starting selenium standalone server"
  @server = Selenium::Server.new(@selenium_path, :background => true)
  @server.start
  caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(
    :javascript_enabled => true)
  @driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps)
  FileUtils.cd(previous_wd)
end
stop_server() click to toggle source

Stops the server

# File lib/pictate.rb, line 63
  def stop_server
    @driver.quit
    @server.stop
    if @download
      SpinningCursor.set_message <<-EOF
#{SpinningCursor.set_message nil}

Selenium was downloaded to #{@selenium_path}.
You can remove it if you need, but note that pictate will download it again if you don't specify another path.
EOF
    end
  end