Class: Spaceship::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/spaceship/ui.rb,
lib/spaceship/portal/ui/select_team.rb,
lib/spaceship/client.rb

Direct Known Subclasses

PortalClient, TunesClient

Defined Under Namespace

Classes: InvalidUserCredentialsError, NoUserCredentialsError, UnexpectedResponse, UserInterface

Constant Summary collapse

PROTOCOL_VERSION =
"QH65B2"

Instance Attribute Summary collapse

Automatic Paging collapse

Login and Team Selection collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/spaceship/client.rb', line 59

def initialize
  @client = Faraday.new(self.class.hostname) do |c|
    c.response :json, content_type: /\bjson$/
    c.response :xml, content_type: /\bxml$/
    c.response :plist, content_type: /\bplist$/
    c.adapter Faraday.default_adapter

    if ENV['DEBUG']
      # for debugging only
      # This enables tracking of networking requests using Charles Web Proxy
      c.response :logger
      c.proxy "https://127.0.0.1:8888"
    end
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Returns the value of attribute cookie.



19
20
21
# File 'lib/spaceship/client.rb', line 19

def cookie
  @cookie
end

#loggerObject

The logger in which all requests are logged /tmp/spaceship.log by default



23
24
25
# File 'lib/spaceship/client.rb', line 23

def logger
  @logger
end

Class Method Details

.hostnameObject



55
56
57
# File 'lib/spaceship/client.rb', line 55

def self.hostname
  raise "You must implemented self.hostname"
end

.login(user = nil, password = nil) ⇒ Spaceship::Client

Authenticates with Apple’s web services. This method has to be called once to generate a valid session. The session will automatically be used from then on.

This method will automatically use the username from the Appfile (if available) and fetch the password from the Keychain (if available)

Parameters:

  • user (String) (defaults to: nil)

    (optional): The username (usually the email address)

  • password (String) (defaults to: nil)

    (optional): The password

Returns:

Raises:

  • InvalidUserCredentialsError: raised if authentication failed



46
47
48
49
50
51
52
53
# File 'lib/spaceship/client.rb', line 46

def self.(user = nil, password = nil)
  instance = self.new
  if instance.(user, password)
    instance
  else
    raise InvalidUserCredentialsError.new
  end
end

Instance Method Details

#login(user = nil, password = nil) ⇒ Spaceship::Client

Authenticates with Apple’s web services. This method has to be called once to generate a valid session. The session will automatically be used from then on.

This method will automatically use the username from the Appfile (if available) and fetch the password from the Keychain (if available)

Parameters:

  • user (String) (defaults to: nil)

    (optional): The username (usually the email address)

  • password (String) (defaults to: nil)

    (optional): The password

Returns:

Raises:

  • InvalidUserCredentialsError: raised if authentication failed



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/spaceship/client.rb', line 138

def (user = nil, password = nil)
  if user.to_s.empty? or password.to_s.empty?
    require 'credentials_manager'
    data = CredentialsManager::PasswordManager.shared_manager(user, false)
    user ||= data.username
    password = data.password
  end

  if user.to_s.strip.empty? or password.to_s.strip.empty?
    raise NoUserCredentialsError.new("No login data provided")
  end

  (user, password) # different in subclasses
end

#page_sizeObject

The page size we want to request, defaults to 500



100
101
102
# File 'lib/spaceship/client.rb', line 100

def page_size
  @page_size ||= 500
end

#pagingObject

Handles the paging for you… for free Just pass a block and use the parameter as page number



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/spaceship/client.rb', line 106

def paging
  page = 0
  results = []
  loop do
    page += 1
    current = yield(page)

    results = results + current

    break if ((current || []).count < page_size) # no more results
  end

  return results
end

#session?Bool

Returns Do we have a valid session?.

Returns:

  • (Bool)

    Do we have a valid session?



154
155
156
# File 'lib/spaceship/client.rb', line 154

def session?
  !!@cookie
end

#UIObject

Public getter for all UI related code



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

def UI
  UserInterface.new(self)
end

#with_retry(tries = 5, &block) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/spaceship/client.rb', line 158

def with_retry(tries = 5, &block)
  return block.call
rescue Faraday::Error::TimeoutError => ex # New Faraday version: Faraday::TimeoutError => ex
  unless (tries -= 1).zero?
    sleep 3
    retry
  end

  raise ex # re-raise the exception
end