module AppleBot

Constants

DESCRIPTION
MESSAGE_FRAGMENT_TO_ERROR
VERSION
WEBSITE

Public Instance Methods

applebot_root_path() click to toggle source
# File lib/applebot.rb, line 17
def applebot_root_path
  File.expand_path(File.join(File.dirname(__FILE__), '..'))
end
casper_installed?() click to toggle source
# File lib/applebot.rb, line 33
def casper_installed?
  !!find_executable("casperjs")
end
command_file_path(command) click to toggle source
# File lib/applebot.rb, line 25
def command_file_path(command)
  file = command
  if !file.end_with?(".js")
    file << ".js"
  end
  File.join(phantom_scripts_path, file)
end
find_executable(bin, path = nil) click to toggle source

ripped from mkmf source

# File lib/applebot/mkmf.rb, line 5
def find_executable(bin, path = nil)
  ext = ""
  if File.expand_path(bin) == bin
    return bin if File.executable?(bin)
    ext and File.executable?(file = bin + ext) and return file
    return nil
  end
  if path ||= ENV['PATH']
    path = path.split(File::PATH_SEPARATOR)
  else
    path = %w[/usr/local/bin /usr/ucb /usr/bin /bin]
  end
  file = nil
  path.each do |dir|
    return file if File.executable?(file = File.join(dir, bin))
    return file if ext and File.executable?(file << ext)
  end
  nil
end
phantom_scripts_path() click to toggle source
# File lib/applebot.rb, line 21
def phantom_scripts_path
  File.join(applebot_root_path, 'phantom')
end
run_command(command, options = {}) click to toggle source
# File lib/applebot.rb, line 65
def run_command(command, options = {})
  raise "CasperJS is not installed - `brew install caspjerjs --devel` or visit http://casperjs.org/" if !casper_installed?

  options = options.with_indifferent_access
  verbose = options.delete(:verbose)
  format = options.delete(:format).to_s
  print_result = options.delete(:print_result)

  if options[:manifest]
    options = File.open(options[:manifest], 'r') { |f|
      JSON.parse(f.read).with_indifferent_access
    }
  end

  command_file = command_file_path(command)
  manifest_file = Tempfile.new('manifest.json')
  begin
    write_options = {
      "output_format" => 'json',
      "username" => options[:username] || @username || ENV['APPLEBOT_USERNAME'],
      "password" => options[:password] || @password || ENV['APPLEBOT_PASSWORD'],
      "applebot_root_path" => AppleBot.applebot_root_path
    }.merge(options)
    manifest_file.write(write_options.to_json)

    manifest_file.close

    sys_command = "casperjs #{command_file} --manifest=#{manifest_file.path.to_s.shellescape}"
    command_result = shell.command(sys_command, verbose, format)

    if command_result != nil
      shell.result(command_result, format, print_result)
    else
      true
    end
  ensure
    manifest_file.unlink
  end
end
set_credentials(options = {}) click to toggle source
# File lib/applebot.rb, line 52
def set_credentials(options = {})
  @username = options[:username]
  @password = options[:password]
  true
end
shell() click to toggle source
# File lib/applebot.rb, line 37
def shell
  @shell ||= Shell.new
end
with_credentials(options = {}) { |self| ... } click to toggle source
# File lib/applebot.rb, line 58
def with_credentials(options = {})
  set_credentials(options)
  yield(self).tap do |res|
    set_credentials({})
  end
end