Module: AppleBot

Defined in:
lib/applebot.rb,
lib/applebot.rb,
lib/applebot/mkmf.rb,
lib/applebot/error.rb,
lib/applebot/shell.rb,
lib/applebot/version.rb,
lib/applebot/commands.rb,
lib/applebot/command_proxy.rb

Defined Under Namespace

Classes: AppIdNotFound, AppNameTakenError, AppleBotError, AppleMaintenanceMode, AuthenticationError, Command, CommandOption, CommandOptionSet, CommandProxy, MultipleProfileError, Shell, SignalTermination

Constant Summary collapse

MESSAGE_FRAGMENT_TO_ERROR =
{
  "could not find App ID in options" => AppIdNotFound,
  'class="maintenance"' => AppleMaintenanceMode,
  'iTunes Connect is temporarily unavailable' => AppleMaintenanceMode,
  'Your Apple ID or password was entered incorrectly' => AuthenticationError,
  'Multiple profiles found with the name' => MultipleProfileError,
  'The App Name you entered has already been used' => AppNameTakenError,
}
VERSION =
"0.0.2"
DESCRIPTION =
'A robot for Apple Developer Center and iTunes Connect tasks'
WEBSITE =
'https://github.com/usepropeller/applebot'

Class Method Summary collapse

Class Method Details

.applebot_root_pathObject



17
18
19
# File 'lib/applebot.rb', line 17

def applebot_root_path
  File.expand_path(File.join(File.dirname(__FILE__), '..'))
end

.casper_installed?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/applebot.rb', line 33

def casper_installed?
  !!find_executable("casperjs")
end

.command_file_path(command) ⇒ Object



25
26
27
28
29
30
31
# 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) ⇒ Object

ripped from mkmf source



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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_pathObject



21
22
23
# File 'lib/applebot.rb', line 21

def phantom_scripts_path
  File.join(applebot_root_path, 'phantom')
end

.run_command(command, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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 = {}) ⇒ Object



52
53
54
55
56
# File 'lib/applebot.rb', line 52

def set_credentials(options = {})
  @username = options[:username]
  @password = options[:password]
  true
end

.shellObject



37
38
39
# File 'lib/applebot.rb', line 37

def shell
  @shell ||= Shell.new
end

.with_credentials(options = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/applebot.rb', line 58

def with_credentials(options = {})
  set_credentials(options)
  yield(self).tap do |res|
    set_credentials({})
  end
end