Class: Codes::ItunesConnect

Inherits:
FastlaneCore::ItunesConnect
  • Object
show all
Defined in:
lib/codes/itunes_connect.rb

Constant Summary collapse

PROMO_URL =
"https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa/wa/LCAppPage/viewPromoCodes?adamId=[[app_id]]&platform=[[platform]]"
CODE_URL =
"https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/redeemLandingPage?code=[[code]]"

Instance Method Summary collapse

Instance Method Details

#click_nextObject



67
68
69
# File 'lib/codes/itunes_connect.rb', line 67

def click_next
  wait_for_elements("input.continueActionButton").first.click
end

#download_codes(url) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/codes/itunes_connect.rb', line 71

def download_codes(url)
  host = Capybara.current_session.current_host
  url = URI.join(host, url)
  Helper.log.debug "Downloading promo code file from #{url}"

  cookie_string = ""
  page.driver.cookies.each do |key, cookie|
    cookie_string << "#{cookie.name}=#{cookie.value};"
  end

  open(url, "Cookie" => cookie_string).read
end

#run(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/codes/itunes_connect.rb', line 9

def run(args)
  number_of_codes = args[:number_of_codes]

  code_or_codes = number_of_codes == 1 ? "code" : "codes"
  Helper.log.info "Downloading #{number_of_codes} promo #{code_or_codes}..." 


  app_id = args[:apple_id]
  app_id ||= (FastlaneCore::ItunesSearchApi.fetch_by_identifier(args[:app_identifier])['trackId'] rescue nil)

  app_identifier = args[:app_identifier]

  if app_id.to_i == 0 or app_identifier.to_s.length == 0
    raise "Could not find app using the following information: #{args}. Maybe the app is not in the store. Pass the Apple ID of the app as well!".red
  end

  app = FastlaneCore::ItunesSearchApi.fetch(app_id)
  platform = (app['kind'] == "mac-software" ? "osx" : "ios")

  # Use Pathname because it correctly handles the distinction between relative paths vs. absolute paths
  output_file_path = Pathname.new(args[:output_file_path]) if args[:output_file_path]
  output_file_path ||= Pathname.new(File.join(Dir.getwd, "#{app_identifier || app_id}_codes.txt"))
  raise "Insufficient permissions to write to output file".red if File.exists?(output_file_path) and not File.writable?(output_file_path)
  visit PROMO_URL.gsub("[[app_id]]", app_id.to_s).gsub("[[platform]]", platform)

  begin
    text_fields = wait_for_elements("input[type=text]")
  rescue
    raise "Could not open details page for app #{app_identifier}. Are you sure you are using the correct apple account and have access to this app?".red
  end
  raise "There should only be a single text input field to specify the number of codes".red unless text_fields.count == 1

  text_fields.first.set(number_of_codes.to_s)
  click_next
  Helper.log.debug "Accepting the App Store Volume Custom Code Agreement"
  wait_for_elements("input[type=checkbox]").first.click
  click_next

  # the find(:xpath, "..") gets the parent element of the previous expression
  download_url = wait_for_elements("a > img").first.find(:xpath, '..')['href'] 

  codes = download_codes download_url
  
  if args[:urls]
      codes = codes.split("\n").map do |code|
          code +" - "+ CODE_URL.gsub("[[code]]", code)
      end
      codes = codes.join("\n") + "\n"
  end

  bytes_written = File.write(output_file_path.to_s, codes, mode: "a+")
  Helper.log.warn "Could not write your codes to the codes.txt file, but you can still access them from iTunes Connect later" if bytes_written == 0
  Helper.log.info "Added generated codes to '#{output_file_path.to_s}'".green unless  bytes_written == 0

  Helper.log.info "Your codes were successfully downloaded:".green
  puts codes
end