Class: Spaceship::ConnectAPI::Device

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
spaceship/lib/spaceship/connect_api/models/device.rb

Defined Under Namespace

Modules: DeviceClass, Status

Instance Attribute Summary collapse

Attributes included from Model

#id, #reverse_attr_map

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#attr_mapping, included, #initialize, #reverse_attr_mapping, #to_json, #update_attributes

Instance Attribute Details

#added_dateObject

Returns the value of attribute added_date.



14
15
16
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 14

def added_date
  @added_date
end

#device_classObject

Returns the value of attribute device_class.



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

def device_class
  @device_class
end

#modelObject

Returns the value of attribute model.



9
10
11
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 9

def model
  @model
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 10

def name
  @name
end

#platformObject

Returns the value of attribute platform.



11
12
13
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 11

def platform
  @platform
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 12

def status
  @status
end

#udidObject

Returns the value of attribute udid.



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

def udid
  @udid
end

Class Method Details

.all(client: nil, filter: {}, includes: nil, fields: nil, limit: Spaceship::ConnectAPI::MAX_OBJECTS_PER_PAGE_LIMIT, sort: nil) ⇒ Object

API



56
57
58
59
60
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 56

def self.all(client: nil, filter: {}, includes: nil, fields: nil, limit: Spaceship::ConnectAPI::MAX_OBJECTS_PER_PAGE_LIMIT, sort: nil)
  client ||= Spaceship::ConnectAPI
  resps = client.get_devices(filter: filter, includes: includes, fields: fields, limit: limit, sort: sort).all_pages
  return resps.flat_map(&:to_models)
end

.create(client: nil, name: nil, platform: nil, udid: nil) ⇒ Device

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

Parameters:

  • client (ConnectAPI) (defaults to: nil)

    ConnectAPI client.

  • name (String) (defaults to: nil)

    The name to be assigned to the device.

  • platform (String) (defaults to: nil)

    The platform of the device.

  • udid (String) (defaults to: nil)

    The udid of the device being created.

Returns:

  • (Device)

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



169
170
171
172
173
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 169

def self.create(client: nil, name: nil, platform: nil, udid: nil)
  client ||= Spaceship::ConnectAPI
  resp = client.post_device(name: name, platform: platform, udid: udid)
  return resp.to_models.first
end

.devices_for_platform(platform: nil, include_mac_in_profiles: false, client: nil) ⇒ Device

Returns List of enabled devices.

Parameters:

  • platform (String) (defaults to: nil)

    The provisioning profile’s platform (i.e. ios, tvos, macos, catalyst).

  • include_mac_in_profiles (Bool) (defaults to: false)

    Whether to include macs in iOS provisioning profiles. false by default.

  • client (ConnectAPI) (defaults to: nil)

    ConnectAPI client.

Returns:

  • (Device)

    List of enabled devices.



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 66

def self.devices_for_platform(platform: nil, include_mac_in_profiles: false, client: nil)
  platform = platform.to_sym
  include_mac_in_profiles &&= platform == :ios

  device_platform = case platform
                    when :osx, :macos, :mac
                      Spaceship::ConnectAPI::Platform::MAC_OS
                    when :ios, :tvos, :xros, :visionos
                      Spaceship::ConnectAPI::Platform::IOS
                    when :catalyst
                      Spaceship::ConnectAPI::Platform::MAC_OS
                    end

  device_platforms = [
    device_platform,
    'UNIVERSAL' # Universal Bundle ID platform is undocumented as of Oct 4, 2023.
  ]

  device_classes =
    case platform
    when :ios
      [
        Spaceship::ConnectAPI::Device::DeviceClass::IPAD,
        Spaceship::ConnectAPI::Device::DeviceClass::IPHONE,
        Spaceship::ConnectAPI::Device::DeviceClass::IPOD,
        Spaceship::ConnectAPI::Device::DeviceClass::APPLE_WATCH,
        Spaceship::ConnectAPI::Device::DeviceClass::APPLE_VISION_PRO
      ]
    when :tvos
      [
        Spaceship::ConnectAPI::Device::DeviceClass::APPLE_TV
      ]
    when :macos, :catalyst
      [
        Spaceship::ConnectAPI::Device::DeviceClass::MAC,
        Spaceship::ConnectAPI::Device::DeviceClass::APPLE_SILICON_MAC,
        Spaceship::ConnectAPI::Device::DeviceClass::INTEL_MAC
      ]
    else
      []
    end

  if include_mac_in_profiles
    device_classes << Spaceship::ConnectAPI::Device::DeviceClass::APPLE_SILICON_MAC
    device_platforms << Spaceship::ConnectAPI::Platform::MAC_OS
  end

  filter = {
    status: Spaceship::ConnectAPI::Device::Status::ENABLED
  }
  filter[:platform] = device_platforms.uniq.join(',') unless device_platforms.empty?

  devices = Spaceship::ConnectAPI::Device.all(
    client: client,
    filter: filter
  )

  unless device_classes.empty?
    devices.select! do |device|
      # App Store Connect API return MAC in device_class instead of APPLE_SILICON_MAC for Silicon Macs.
      # The difference between old MAC and APPLE_SILICON_MAC is provisioning uuid.
      # Intel-based provisioning UUID: 01234567-89AB-CDEF-0123-456789ABCDEF.
      # arm64-based provisioning UUID: 01234567-89ABCDEF12345678.
      # Workaround is to include macs having:
      #   * 25 chars length and only one hyphen in provisioning UUID.
      if include_mac_in_profiles &&
         device.device_class == Spaceship::ConnectAPI::Device::DeviceClass::MAC

        next device.udid.length == 25 && device.udid.count('-') == 1
      end

      device_classes.include?(device.device_class)
    end
  end

  devices
end

.disable(device_udid, client: nil) ⇒ Device

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

Parameters:

  • device_udid (String)

    Device Provisioning UDID that needs to be disabled.

  • client (ConnectAPI) (defaults to: nil)

    ConnectAPI client.

Returns:

  • (Device)

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



204
205
206
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 204

def self.disable(device_udid, client: nil)
  self.modify(device_udid, client: client, enabled: false)
end

.enable(device_udid, client: nil) ⇒ Device

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

Parameters:

  • device_udid (String)

    Device Provisioning UDID that needs to be enabled.

  • client (ConnectAPI) (defaults to: nil)

    ConnectAPI client.

Returns:

  • (Device)

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



197
198
199
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 197

def self.enable(device_udid, client: nil)
  self.modify(device_udid, client: client, enabled: true)
end

.find_by_udid(device_udid, client: nil, include_disabled: false) ⇒ Device

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

Parameters:

  • client (ConnectAPI) (defaults to: nil)

    ConnectAPI client.

  • platform (String)

    The platform of the device.

  • 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.



148
149
150
151
152
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 148

def self.find_by_udid(device_udid, client: nil, include_disabled: false)
  self.all(client: client).find do |device|
    device.udid.casecmp(device_udid) == 0 && (include_disabled ? true : device.enabled?)
  end
end

.find_or_create(device_udid, client: nil, name: nil, platform: nil) ⇒ Device

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

Parameters:

  • client (ConnectAPI) (defaults to: nil)

    ConnectAPI client.

  • name (String) (defaults to: nil)

    The name to be assigned to the device, if it needs to be created.

  • platform (String) (defaults to: nil)

    The platform of the device.

Returns:

  • (Device)

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



158
159
160
161
162
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 158

def self.find_or_create(device_udid, client: nil, name: nil, platform: nil)
  existing = self.find_by_udid(device_udid, client: client)
  return existing if existing
  return self.create(client: client, name: name, platform: platform, udid: device_udid)
end

.modify(device_udid, client: nil, enabled: nil, new_name: nil) ⇒ Device

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

Parameters:

  • device_udid (String)

    Device Provisioning UDID that needs to be modified.

  • client (ConnectAPI) (defaults to: nil)

    ConnectAPI client.

  • enabled (Boolean) (defaults to: nil)

    New enabled value. true - if device must be enabled, ‘false` - to disable device. nil if no status change needed.

  • new_name (String) (defaults to: nil)

    A new name for the device. nil if no name change needed.

Returns:

  • (Device)

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



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 180

def self.modify(device_udid, client: nil, enabled: nil, new_name: nil)
  client ||= Spaceship::ConnectAPI
  existing = self.find_by_udid(device_udid, client: client, include_disabled: true)
  return nil if existing.nil?

  enabled = existing.enabled? if enabled.nil?
  new_name ||= existing.name
  return existing if existing.name == new_name && existing.enabled? == enabled
  new_status = enabled ? Status::ENABLED : Status::DISABLED

  resp = client.patch_device(id: existing.id, new_name: new_name, status: new_status)
  return resp.to_models.first
end

.rename(device_udid, new_name, client: nil) ⇒ Device

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

Parameters:

  • device_udid (String)

    Device Provisioning UDID that needs to be renamed.

  • new_name (String)

    A new name for the device.

  • client (ConnectAPI) (defaults to: nil)

    ConnectAPI client.

Returns:

  • (Device)

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



212
213
214
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 212

def self.rename(device_udid, new_name, client: nil)
  self.modify(device_udid, client: client, new_name: new_name)
end

.typeObject



45
46
47
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 45

def self.type
  return "devices"
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'spaceship/lib/spaceship/connect_api/models/device.rb', line 49

def enabled?
  return status == Status::ENABLED
end