Class: Produce::Merchant

Inherits:
Object
  • Object
show all
Defined in:
produce/lib/produce/merchant.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.detect_merchant_identifier(config = Produce.config) ⇒ Object



80
81
82
83
# File 'produce/lib/produce/merchant.rb', line 80

def self.detect_merchant_identifier(config = Produce.config)
  identifier = config[:merchant_identifier] || input("Merchant identifier (reverse-domain name style string starting with 'merchant'): ", ":merchant_identifier option is required")
  prepare_identifier(identifier)
end

.find_merchant(identifier, merchant: Spaceship.merchant) ⇒ Object



103
104
105
106
# File 'produce/lib/produce/merchant.rb', line 103

def self.find_merchant(identifier, merchant: Spaceship.merchant)
  @cache ||= {}
  @cache[identifier] ||= merchant.find(identifier, mac: mac?)
end

.input(message, error_message) ⇒ Object



85
86
87
88
89
90
91
# File 'produce/lib/produce/merchant.rb', line 85

def self.input(message, error_message)
  if UI.interactive?
    UI.input(message)
  else
    UI.user_error!(error_message)
  end
end

.mac?(config = Produce.config) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'produce/lib/produce/merchant.rb', line 108

def self.mac?(config = Produce.config)
  config[:platform].to_s == "mac"
end

.merchant_name_from_identifier(identifier) ⇒ Object



116
117
118
119
# File 'produce/lib/produce/merchant.rb', line 116

def self.merchant_name_from_identifier(identifier)
  capitalized_words = identifier.split(".").map(&:capitalize)
  capitalized_words.reverse.join(' ')
end

.prepare_identifier(identifier) ⇒ Object



93
94
95
96
97
# File 'produce/lib/produce/merchant.rb', line 93

def self.prepare_identifier(identifier)
  return identifier if identifier.start_with?("merchant.")

  "merchant.#{identifier}"
end

Instance Method Details

#app_identifierObject



62
63
64
# File 'produce/lib/produce/merchant.rb', line 62

def app_identifier
  Produce.config[:app_identifier]
end

#associate(_options, args) ⇒ Object



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
# File 'produce/lib/produce/merchant.rb', line 29

def associate(_options, args)
  

  app = Spaceship.app.find(app_identifier)

  if app
    app.update_service(Spaceship.app_service.apple_pay.on)

    UI.message("Validating merchants before association")

    # associate requires identifiers to exist. This splits the provided identifiers into existing/non-existing. See: https://ruby-doc.org/core/Enumerable.html#method-i-partition
    valid_identifiers, errored_identifiers = args.partition { |identifier| merchant_exists?(identifier) }
    new_merchants = valid_identifiers.map { |identifier| find_merchant(identifier) }

    errored_identifiers.each do |merchant_identifier|
      UI.message("[DevCenter] Merchant '#{merchant_identifier}' does not exist, please create it first, skipping for now")
    end

    UI.message("Finalising association with #{new_merchants.count} #{pluralize('merchant', new_merchants)}")
    app.associate_merchants(new_merchants)
    UI.success("Done!")
  else
    UI.message("[DevCenter] App '#{Produce.config[:app_identifier]}' does not exist, nothing to associate with the merchants")
  end
end

#create(_options, _args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'produce/lib/produce/merchant.rb', line 6

def create(_options, _args)
  

  merchant_identifier = detect_merchant_identifier
  merchant = find_merchant(merchant_identifier)

  if merchant
    UI.success("[DevCenter] Merchant '#{merchant.bundle_id})' already exists, nothing to do on the Dev Center")
  else
    merchant_name = Produce.config[:merchant_name] || merchant_name_from_identifier(merchant_identifier)
    UI.success("Creating new merchant '#{merchant_name}' with identifier '#{merchant_identifier}' on the Apple Dev Center")
    merchant = Spaceship.merchant.create!(bundle_id: merchant_identifier, name: merchant_name, mac: self.class.mac?)

    if merchant.name != merchant_name
      UI.important("Your merchant name might include non-ASCII characters, which are not supported by the Apple Developer Portal.")
      UI.important("To fix this a unique (internal) name '#{merchant.name}' has been created for you.")
    end

    UI.message("Created merchant #{merchant.merchant_id}")
    UI.success("Finished creating new merchant '#{merchant_name}' on the Dev Center")
  end
end

#detect_merchant_identifierObject



76
77
78
# File 'produce/lib/produce/merchant.rb', line 76

def detect_merchant_identifier
  self.class.detect_merchant_identifier
end

#find_merchant(identifier) ⇒ Object



99
100
101
# File 'produce/lib/produce/merchant.rb', line 99

def find_merchant(identifier)
  self.class.find_merchant(identifier)
end

#loginObject



55
56
57
58
59
60
# File 'produce/lib/produce/merchant.rb', line 55

def 
  UI.message("Starting login with user '#{Produce.config[:username]}'")
  Spaceship.(Produce.config[:username], nil)
  Spaceship.select_team
  UI.message("Successfully logged in")
end

#merchant_exists?(identifier) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'produce/lib/produce/merchant.rb', line 72

def merchant_exists?(identifier)
  find_merchant(identifier)
end

#merchant_name_from_identifier(identifier) ⇒ Object



112
113
114
# File 'produce/lib/produce/merchant.rb', line 112

def merchant_name_from_identifier(identifier)
  self.class.merchant_name_from_identifier(identifier)
end

#pluralize(singular, arr) ⇒ Object



66
67
68
69
70
# File 'produce/lib/produce/merchant.rb', line 66

def pluralize(singular, arr)
  return singular if arr.count == 1

  "#{singular}s"
end