Class: XcodeInstaller::Agent

Inherits:
Mechanize
  • Object
show all
Defined in:
lib/xcode-installer/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



9
10
11
12
13
14
15
# File 'lib/xcode-installer/agent.rb', line 9

def initialize
  super
  self.user_agent_alias = 'Mac Safari'

  pw = Security::InternetPassword.find(:server => XcodeInstaller::AppleDeveloperCenter::HOST)
  @username, @password = pw.attributes['acct'], pw.password if pw
end

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



7
8
9
# File 'lib/xcode-installer/agent.rb', line 7

def dry_run
  @dry_run
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/xcode-installer/agent.rb', line 7

def password
  @password
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/xcode-installer/agent.rb', line 7

def username
  @username
end

#verboseObject

Returns the value of attribute verbose.



7
8
9
# File 'lib/xcode-installer/agent.rb', line 7

def verbose
  @verbose
end

Instance Method Details

#download(xcode_url) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/xcode-installer/agent.rb', line 17

def download(xcode_url)
   = XcodeInstaller::AppleDeveloperCenter::LOGIN_URL
  downloads_url = XcodeInstaller::AppleDeveloperCenter::DOWNLOADS_URL

  begin
    # Request login response
    puts "\n>>> Login response >>>" if @verbose
    response = get()
    if @verbose
      puts "status code: #{response.code}\n"
      pp response
      puts cookie_jar.jar
    end

    # Submit login form
    puts "\n>>> Submit Login Form >>>" if @verbose
    form = response.form_with(:name => 'appleConnectForm')
    form.theAccountName = username
    form.theAccountPW = password
    response = form.submit
    if @verbose
      puts "status code: #{response.code}\n"
      pp response
      puts cookie_jar.jar
    end

    # Request downloads response
    puts "\n>>> Downloads >>>" if @verbose
    response = get(downloads_url)
    if @verbose
      puts "status code: #{response.code}\n"
      pp response
      puts cookie_jar.jar
    end
    # Shouldn't get the login form if login was successful
    form = response.form_with(:name => 'appleConnectForm')
    raise UnsuccessfulAuthenticationError if form

    # Download
    puts "\n>>> Xcode >>>" if @verbose

    if @dry_run
      # HEAD request for testing
      response = head(xcode_url)
      if @verbose
        puts "status code: #{response.code}\n"
        pp response
      else
        puts "filename: #{response.filename}"
        puts "size: #{response.header['content-length']}"
        puts "last-modified: #{response.header['last-modified']}"
        return response.filename
      end
    else
      # GET request for actual download
      pluggable_parser.default = Mechanize::Download
      self.progressbar{ file = get(xcode_url) }
      file = get(xcode_url)
      file.save
      return file.filename
    end

  rescue Mechanize::ResponseCodeError => exception
    if exception.response_code == '403'
      response = exception.page
      puts "The file doesn't exist at that location or access denied (status code: #{response.code})\n"
      puts "\n>>> Unexpected response >>>" if @verbose
      pp response if @verbose
    else
      raise # Some other error, re-raise
    end
  end
end