Class: Spaceship::Tunes::Availability

Inherits:
TunesBase show all
Defined in:
spaceship/lib/spaceship/tunes/availability.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#client, #raw_data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TunesBase

client

Methods inherited from Base

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

Constructor Details

This class inherits a constructor from Spaceship::Base

Instance Attribute Details

#app_available_dateString

Returns App available date in format of “YYYY-MM-DD”.

Returns:

  • (String)

    App available date in format of “YYYY-MM-DD”


17
18
19
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 17

def app_available_date
  @app_available_date
end

#b2b_app_enabledBool

Returns app enabled for b2b users.

Returns:

  • (Bool)

    app enabled for b2b users


20
21
22
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 20

def b2b_app_enabled
  @b2b_app_enabled
end

#b2b_organizationsArray of Spaceship::Tunes::B2bOrganization objects

Returns A list of b2b orgs.

Returns:


32
33
34
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 32

def b2b_organizations
  @b2b_organizations
end

#b2b_unavailableBool

Returns b2b available for distribution.

Returns:

  • (Bool)

    b2b available for distribution


26
27
28
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 26

def b2b_unavailable
  @b2b_unavailable
end

#b2b_usersArray of Spaceship::Tunes::B2bUser objects

Returns A list of users.

Returns:


29
30
31
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 29

def b2b_users
  @b2b_users
end

#cleared_for_preorderBool

Returns Cleared for preorder.

Returns:

  • (Bool)

    Cleared for preorder


14
15
16
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 14

def cleared_for_preorder
  @cleared_for_preorder
end

#educational_discountBool

Returns app enabled for educational discount.

Returns:

  • (Bool)

    app enabled for educational discount


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

def educational_discount
  @educational_discount
end

#include_future_territoriesBool

Returns Are future territories included?.

Returns:

  • (Bool)

    Are future territories included?


8
9
10
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 8

def include_future_territories
  @include_future_territories
end

#territoriesArray of Spaceship::Tunes::Territory objects

Returns A list of the territories.

Returns:


11
12
13
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 11

def territories
  @territories
end

Class Method Details

.from_territories(territories = [], params = {}) ⇒ Object

Create a new object based on a set of territories. This will override any values already set for cleared_for_preorder, app_available_date, b2b_unavailable, b2b_app_enabled, and educational_discount

Parameters:

  • territories (Array of String or Spaceship::Tunes::Territory objects) (defaults to: [])

    : A list of the territories

  • params (Hash) (defaults to: {})

    : Optional parameters (include_future_territories (Bool, default: true) Are future territories included?)


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
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 46

def self.from_territories(territories = [], params = {})
  # Initializes the DataHash with our preOrder structure so values
  # that are being modified will be saved
  #
  # Note: A better solution for this in the future might be to improve how
  # Base::DataHash sets values for paths that don't exist
  obj = self.new(
    'preOrder' => {
        'clearedForPreOrder' => {
            'value' => false
        },
        'appAvailableDate' => {
            'value' => nil
        }
    }
  )

  # Detect if the territories attribute is an array of Strings and convert to Territories
  obj.territories =
    if territories[0].kind_of?(String)
      territories.map { |territory| Spaceship::Tunes::Territory.from_code(territory) }
    else
      territories
    end
  obj.include_future_territories = params.fetch(:include_future_territories, true)
  obj.cleared_for_preorder = params.fetch(:cleared_for_preorder, false)
  obj.app_available_date = params.fetch(:app_available_date, nil)
  obj.b2b_unavailable = params.fetch(:b2b_unavailable, false)
  obj.b2b_app_enabled = params.fetch(:b2b_app_enabled, false)
  obj.educational_discount = params.fetch(:educational_discount, true)
  return obj
end

Instance Method Details

#add_b2b_users(user_list = []) ⇒ Object

Adds users for b2b enabled apps


114
115
116
117
118
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 114

def add_b2b_users(user_list = [])
  raise "Cannot add b2b users if b2b is not enabled" unless b2b_app_enabled
  @b2b_users = user_list.map { |user| B2bUser.from_username(user) }
  return self
end

#enable_b2b_app!Object

Sets `b2b_app_enabled` to true and `educational_discount` to false Requires users to be added with `add_b2b_users` otherwise `update_availability` will error


105
106
107
108
109
110
111
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 105

def enable_b2b_app!
  raise "Not possible to enable b2b on this app" if b2b_unavailable
  @b2b_app_enabled = true
  # need to set the educational discount to false
  @educational_discount = false
  return self
end

#update_b2b_users(user_list = []) ⇒ Object

Updates users for b2b enabled apps


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 121

def update_b2b_users(user_list = [])
  raise "Cannot add b2b users if b2b is not enabled" unless b2b_app_enabled

  added_users = b2b_users.map(&:ds_username)

  # Returns if list is unchanged
  return self if (added_users - user_list) == (user_list - added_users)

  users_to_add = user_list.reject { |user| added_users.include?(user) }
  users_to_remove = added_users.reject { |user| user_list.include?(user) }

  @b2b_users = b2b_users.reject { |user| users_to_remove.include?(user.ds_username) }
  @b2b_users.concat(users_to_add.map { |user| B2bUser.from_username(user) })
  @b2b_users.concat(users_to_remove.map { |user| B2bUser.from_username(user, is_add_type: false) })

  return self
end

#update_dep_organizations(organization_list = []) ⇒ Object

Updates users for b2b enabled apps


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'spaceship/lib/spaceship/tunes/availability.rb', line 140

def update_dep_organizations(organization_list = [])
  raise "Cannot add dep organizations if b2b is not enabled" unless b2b_app_enabled

  added_orgs = b2b_organizations

  # Returns if list is unchanged
  return self if (added_orgs - organization_list) == (organization_list - added_orgs)

  orgs_to_add = organization_list.reject { |organization| added_orgs.include?(organization) }
  orgs_to_remove = added_orgs.nil? ? organization_list : added_orgs.reject { |organization| organization_list.include?(organization) }

  @b2b_organizations = b2b_organizations.reject { |organization| orgs_to_remove.include?(organization) }
  @b2b_organizations.concat(orgs_to_add)
  @b2b_organizations.concat(orgs_to_remove.map do |organization|
    B2bOrganization.from_id_info(dep_id: organization.dep_customer_id,
                                 dep_name: organization.name,
                                 dep_org_id: organization.dep_organization_id,
                                 type: false)
  end)

  return self
end