Class: ItunesConnect::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/itunes_connect/connection.rb

Overview

Abstracts the iTunes Connect website. Implementation inspired by code.google.com/p/itunes-connect-scraper/

Constant Summary collapse

REPORT_PERIODS =
["Weekly", "Daily"]
BASE_URL =

login base

'https://itunesconnect.apple.com'
REPORT_URL =
'https://reportingitc.apple.com/sales.faces'
LOGIN_URL =
'https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa'
ID_SELECT_DAILY =

select ids: theForm:datePickerSourceSelectElementSales (daily) theForm:weekPickerSourceSelectElement (weekly)

"theForm:datePickerSourceSelectElementSales"
ID_SELECT_WEEKLY =
"theForm:weekPickerSourceSelectElement"

Instance Method Summary collapse

Constructor Details

#initialize(username, password, verbose = false, debug = false) ⇒ Connection

Create a new instance with the username and password used to sign in to the iTunes Connect website



53
54
55
56
57
58
# File 'lib/itunes_connect/connection.rb', line 53

def initialize(username, password, verbose=false, debug=false)
  @username, @password = username, password
  @verbose = verbose
  @debug = debug
  @current_period = "Daily"  # default period in reportingitc interface
end

Instance Method Details

#debug?Boolean

:nodoc:

Returns:

  • (Boolean)


64
65
66
# File 'lib/itunes_connect/connection.rb', line 64

def debug?                  # :nodoc:
  !!@debug
end

#get_report(date, out, period = 'Daily') ⇒ Object

Retrieve a report from iTunes Connect. This method will return the raw report file as a String. If specified, the date parameter should be a Date instance, and the period parameter must be one of the values identified in the REPORT_PERIODS array, or this method will raise and ArgumentError.

Any dates given that equal the current date or newer will cause this method to raise an ArgumentError.



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/itunes_connect/connection.rb', line 82

def get_report(date, out, period = 'Daily')
  date = Date.parse(date) if String === date
  if date >= Date.today
    raise ArgumentError, "You must specify a date before today"
  end

  unless REPORT_PERIODS.member?(period)
    raise ArgumentError, "'period' must be one of #{REPORT_PERIODS.join(', ')}"
  end

   unless self.logged_in?

  # fetch report
  # (cache report page)
  fetch_report_page unless @report_page

  # requested download date
  date_str = date.strftime("%m/%d/%Y")
  debug_msg("download date: #{date_str}")

  # determine available download options
  @select_name = period == 'Daily' ? ID_SELECT_DAILY : ID_SELECT_WEEKLY
  options = @report_page.search(".//select[@id='#{@select_name}']/option")
  options = options.collect { |i| i ? i['value'] : nil } if options
  raise "unable to determine daily report options" unless options

  debug_msg("options: #{options.inspect}")

  # constrain download to available reports
  available = options.find { |i| i <= date_str } ? true : false

  unless available
    raise ArgumentError, "No #{period} reports are available for #{date_str}"
  end

  # get ajax parameter name for Daily/Weekly (<a> id)
  report_period_link = @report_page.link_with(:text => /#{period}/)
  @report_period_id = report_period_link.node['id']
  raise "could not determine form period AJAX parameter" unless @report_period_id

  # get ajax parameter name from <select> onchange attribute
  # 'parameters':{'theForm:j_id_jsp_4933398_30':'theForm:j_id_jsp_4933398_30'}
  report_date_select = @report_page.search(".//select[@id='#{@select_name}']")
  @report_date_id = report_date_select[0]['onchange'].match(/parameters':\{'(.*?)'/)[1] rescue nil
  raise "could not determine form date AJAX parameter" unless @report_date_id

  # select report period to download (Weekly/Daily)
  if @current_period != period
    change_report(@report_page, date_str, @report_period_id => @report_period_id)
    @current_period = period
  end

  # select report date
  page = change_report(@report_page, date_str, @report_date_id => @report_date_id)

  # after selecting report type, recheck if selection is available.
  # (selection options exist even when the report isn't available, so
  #  we need to do another check here)
  dump(client, page)
  available = !page.body.match(/There is no report available for this selection/)
  unless available
    raise ArgumentError, "No #{period} reports are available for #{date_str}"
  end

  # download the report
  page = @report_page.form_with(:name => 'theForm') do |form|
    form['theForm:xyz'] = 'notnormal'
    form['theForm:downloadLabel2'] = 'theForm:downloadLabel2'
    form[@select_name] = date_str

    form.delete_field!('AJAXREQUEST')
    form.delete_field!(@report_period_id)
    form.delete_field!(@report_date_id)

    debug_form(form)
  end.submit

  dump(client, page)
  report = page.body.to_s
  debug_msg("report is #{report.length} bytes")
  out << report
  report
end

#logged_in?Boolean

:nodoc:

Returns:

  • (Boolean)


68
69
70
# File 'lib/itunes_connect/connection.rb', line 68

def logged_in?              # :nodoc:
  !!@logged_in
end

#verbose?Boolean

:nodoc:

Returns:

  • (Boolean)


60
61
62
# File 'lib/itunes_connect/connection.rb', line 60

def verbose?                # :nodoc:
  !!@verbose
end