Class: Spaceship::Portal::Device

Inherits:
Spaceship::PortalBase show all
Defined in:
lib/spaceship/portal/device.rb

Overview

Represents a device from the Apple Developer Portal

Instance Attribute Summary collapse

Attributes inherited from Base

#client, #raw_data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spaceship::PortalBase

client

Methods inherited from Base

attr_accessor, attr_mapping, #attributes, attributes, #initialize, #inspect, mapping_module, method_missing, set_client, #setup, #to_s

Constructor Details

This class inherits a constructor from Spaceship::Base

Instance Attribute Details

#device_typeString

Returns Device type.

Examples:

'watch'  - Apple Watch
'ipad'   - iPad
'iphone' - iPhone
'ipod'   - iPod
'tvOS'   - Apple TV

Returns:



42
43
44
# File 'lib/spaceship/portal/device.rb', line 42

def device_type
  @device_type
end

#idString

Returns The ID given from the developer portal. You’ll probably not need it.

Examples:

"XJXGVS46MW"

Returns:

  • (String)

    The ID given from the developer portal. You’ll probably not need it.



8
9
10
# File 'lib/spaceship/portal/device.rb', line 8

def id
  @id
end

#modelString

Returns Model (can be nil).

Examples:

'iPhone 6', 'iPhone 4 GSM'

Returns:

  • (String)

    Model (can be nil)



33
34
35
# File 'lib/spaceship/portal/device.rb', line 33

def model
  @model
end

#nameString

Returns The name of the device.

Examples:

"Felix Krause's iPhone 6"

Returns:

  • (String)

    The name of the device



13
14
15
# File 'lib/spaceship/portal/device.rb', line 13

def name
  @name
end

#platformString

Returns The platform of the device. This is probably always “ios”.

Examples:

"ios"

Returns:

  • (String)

    The platform of the device. This is probably always “ios”



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

def platform
  @platform
end

#statusString

Returns Status of the device. “c” for enabled devices, “r” for disabled devices.

Examples:

"c"

Returns:

  • (String)

    Status of the device. “c” for enabled devices, “r” for disabled devices.



28
29
30
# File 'lib/spaceship/portal/device.rb', line 28

def status
  @status
end

#udidString

Returns The UDID of the device.

Examples:

"4c24a7ee5caaa4847f49aaab2d87483053f53b65"

Returns:

  • (String)

    The UDID of the device



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

def udid
  @udid
end

Class Method Details

.all(mac: false, include_disabled: false) ⇒ Array

Returns all devices registered for this account

Parameters:

  • mac (Bool) (defaults to: false)

    Fetches Mac devices if true

  • include_disabled (Bool) (defaults to: false)

    Whether to include disable devices. false by default.

Returns:

  • (Array)

    Returns all devices registered for this account



64
65
66
# File 'lib/spaceship/portal/device.rb', line 64

def all(mac: false, include_disabled: false)
  client.devices(mac: mac, include_disabled: include_disabled).map { |device| self.factory(device) }
end

.all_apple_tvsArray

Returns all Apple TVs registered for this account

Returns:

  • (Array)

    Returns all Apple TVs registered for this account



69
70
71
# File 'lib/spaceship/portal/device.rb', line 69

def all_apple_tvs
  client.devices_by_class('tvOS').map { |device| self.factory(device) }
end

.all_for_profile_type(profile_type) ⇒ Array

Returns all devices that can be used for iOS profiles (all devices except TVs)

Returns:

  • (Array)

    Returns all devices that can be used for iOS profiles (all devices except TVs)



104
105
106
107
108
109
110
# File 'lib/spaceship/portal/device.rb', line 104

def all_for_profile_type(profile_type)
  if profile_type.include? "tvOS"
    Spaceship::Device.all_apple_tvs
  else
    Spaceship::Device.all_ios_profile_devices
  end
end

.all_ios_profile_devicesArray

Returns all devices that can be used for iOS profiles (all devices except TVs)

Returns:

  • (Array)

    Returns all devices that can be used for iOS profiles (all devices except TVs)



99
100
101
# File 'lib/spaceship/portal/device.rb', line 99

def all_ios_profile_devices
  all.select { |device| device.device_type != "tvOS" }
end

.all_ipadsArray

Returns all iPads registered for this account

Returns:

  • (Array)

    Returns all iPads registered for this account



79
80
81
# File 'lib/spaceship/portal/device.rb', line 79

def all_ipads
  client.devices_by_class('ipad').map { |device| self.factory(device) }
end

.all_iphonesArray

Returns all iPhones registered for this account

Returns:

  • (Array)

    Returns all iPhones registered for this account



84
85
86
# File 'lib/spaceship/portal/device.rb', line 84

def all_iphones
  client.devices_by_class('iphone').map { |device| self.factory(device) }
end

.all_ipod_touchesArray

Returns all iPods registered for this account

Returns:

  • (Array)

    Returns all iPods registered for this account



89
90
91
# File 'lib/spaceship/portal/device.rb', line 89

def all_ipod_touches
  client.devices_by_class('ipod').map { |device| self.factory(device) }
end

.all_macsArray

Returns all Macs registered for this account

Returns:

  • (Array)

    Returns all Macs registered for this account



94
95
96
# File 'lib/spaceship/portal/device.rb', line 94

def all_macs
  all(mac: true)
end

.all_watchesArray

Returns all Watches registered for this account

Returns:

  • (Array)

    Returns all Watches registered for this account



74
75
76
# File 'lib/spaceship/portal/device.rb', line 74

def all_watches
  client.devices_by_class('watch').map { |device| self.factory(device) }
end

.create!(name: nil, udid: nil, mac: false) ⇒ Device

Register a new device to this account

Examples:

Spaceship.device.create!(name: "Felix Krause's iPhone 6", udid: "4c24a7ee5caaa4847f49aaab2d87483053f53b65")

Parameters:

  • name (String) (defaults to: nil)

    (required): The name of the new device

  • udid (String) (defaults to: nil)

    (required): The UDID of the new device

  • mac (Bool) (defaults to: false)

    (optional): Pass Mac if device is a Mac

Returns:

  • (Device)

    : The newly created device



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/spaceship/portal/device.rb', line 147

def create!(name: nil, udid: nil, mac: false)
  # Check whether the user has passed in a UDID and a name
  unless udid && name
    raise "You cannot create a device without a device_id (UDID) and name"
  end

  # Find the device by UDID, raise an exception if it already exists
  existing = self.find_by_udid(udid, mac: mac)
  return existing if existing

  # It is valid to have the same name for multiple devices

  device = client.create_device!(name, udid, mac: mac)

  # Update self with the new device
  self.new(device)
end

.factory(attrs) ⇒ Object

Create a new object based on a hash. This is used to create a new object based on the server response.



57
58
59
# File 'lib/spaceship/portal/device.rb', line 57

def factory(attrs)
  self.new(attrs)
end

.find(device_id, mac: false, include_disabled: false) ⇒ Device

Returns Find a device based on the ID of the device. Attention: This is not the UDID. nil if no device was found.

Parameters:

  • mac (Bool) (defaults to: false)

    Searches for Macs if true

  • include_disabled (Bool) (defaults to: false)

    Whether to include disable devices. false by default.

Returns:

  • (Device)

    Find a device based on the ID of the device. Attention: This is not the UDID. nil if no device was found.



116
117
118
119
120
# File 'lib/spaceship/portal/device.rb', line 116

def find(device_id, mac: false, include_disabled: false)
  all(mac: mac, include_disabled: include_disabled).find do |device|
    device.id == device_id
  end
end

.find_by_name(device_name, mac: false, include_disabled: false) ⇒ Device

Returns Find a device based on its name. nil if no device was found.

Parameters:

  • mac (Bool) (defaults to: false)

    Searches for Macs if true

  • include_disabled (Bool) (defaults to: false)

    Whether to include disable devices. false by default.

Returns:

  • (Device)

    Find a device based on its name. nil if no device was found.



134
135
136
137
138
# File 'lib/spaceship/portal/device.rb', line 134

def find_by_name(device_name, mac: false, include_disabled: false)
  all(mac: mac, include_disabled: include_disabled).find do |device|
    device.name == device_name
  end
end

.find_by_udid(device_udid, mac: false, include_disabled: false) ⇒ Device

Returns Find a device based on the UDID of the device. nil if no device was found.

Parameters:

  • mac (Bool) (defaults to: false)

    Searches for Macs if true

  • include_disabled (Bool) (defaults to: false)

    Whether to include disable devices. false by default.

Returns:

  • (Device)

    Find a device based on the UDID of the device. nil if no device was found.



125
126
127
128
129
# File 'lib/spaceship/portal/device.rb', line 125

def find_by_udid(device_udid, mac: false, include_disabled: false)
  all(mac: mac, include_disabled: include_disabled).find do |device|
    device.udid.casecmp(device_udid) == 0
  end
end

Instance Method Details

#disable!Object

Disable current device. This will invalidate all provisioning profiles that use this device.



183
184
185
186
187
188
189
# File 'lib/spaceship/portal/device.rb', line 183

def disable!
  if enabled?
    client.disable_device!(self.id, self.udid, mac: self.platform == 'mac')
    # disable request doesn't return device json, so we assume that the new status is "r" if response succeeded
    self.status = "r"
  end
end

#disabled?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/spaceship/portal/device.rb', line 170

def disabled?
  return self.status == "r"
end

#enable!Object

Enable current device.



175
176
177
178
179
180
# File 'lib/spaceship/portal/device.rb', line 175

def enable!
  unless enabled?
    attr = client.enable_device!(self.id, self.udid, mac: self.platform == 'mac')
    initialize(attr)
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/spaceship/portal/device.rb', line 166

def enabled?
  return self.status == "c"
end