lib/ireporter/reporter.rb in ireporter-0.1.3 vs lib/ireporter/reporter.rb in ireporter-0.2.3
- old
+ new
@@ -7,11 +7,11 @@
class Reporter
# Constants initialization
-VERSION = '1.0'
+VERSION = '2.2'
URL_SALES = 'https://reportingitc-reporter.apple.com/reportservice/sales/v1'
URL_FINANCE = 'https://reportingitc-reporter.apple.com/reportservice/finance/v1'
DATE_TYPE_DAILY = "Daily"
DATE_TYPE_WEEKLY = "Weekly"
@@ -19,14 +19,15 @@
DATE_TYPE_YEARLY = "Yearly"
#constructor
- def initialize(username,password)
+def initialize(username,password,accesstoken)
@username = username
- @password = password
- end
+ @password = password
+ @accesstoken = accesstoken
+end
def get_status (service)
command = "[p=Reporter.properties, "+service+".getStatus]"
url = service == 'Sales' ? URL_SALES : URL_FINANCE
@@ -55,13 +56,60 @@
else response.headers[:"filename"]
return response.body.chomp
end
end
- def get_sales_report(datetype,reportdate)
+def generate_token
+ command = '[p=Reporter.properties, Sales.generateToken]'
+ parameters = build_json_request(command)
+ response = post_request(parameters,URL_SALES)
+ service_request_id = response.headers[:service_request_id]
+
+ opt_param = "&isExistingToken=Y&requestId=" + service_request_id
+ parameters = build_json_request(command) + opt_param
+ response = post_request(parameters,URL_SALES)
+
+ if response.headers[:"errormsg"]
+ raise "token error"
+ else response.headers[:"filename"]
+ return response.body.chomp
+ end
+end
+
+def view_token
+
+ command = '[p=Reporter.properties, Sales.viewToken]'
+
+ parameters = build_json_request(command)
+ response = post_request(parameters,URL_SALES)
+
+ if response.headers[:"errormsg"]
+ raise "token error"
+ else response.headers[:"filename"]
+ return response.body.chomp
+ end
+end
+
+def delete_token
+
+ command = '[p=Reporter.properties, Sales.deleteToken]'
+
+ parameters = build_json_request(command)
+ response = post_request(parameters,URL_SALES)
+
+ if response.headers[:"errormsg"]
+ raise "token error"
+ else response.headers[:"filename"]
+ return response.body.chomp
+ end
+end
+
+def get_sales_report(datetype,reportdate)
+
+
datetype ||= DATE_TYPE_DAILY
reportdate ||= (Time.now-60*60*24).strftime("%Y%m%d")
vendorNumber=get_vendor().chomp
@@ -76,20 +124,20 @@
elsif response.headers[:"filename"]
Zlib::GzipReader.new(StringIO.new(response.body)).read
else
raise "no data returned from itunes: #{response.body}"
end
- end
+end
- def build_json_request(command)
+def build_json_request(command)
- request_data = { :userid => @username ,:queryInput => command, :version => VERSION,:password => @password}
+ request_data = { :userid => @username ,:queryInput => command, :version => VERSION,:password => @password, :accesstoken => @accesstoken}
#Convert request paramters in JSON format
request_data=request_data.to_json
return 'jsonRequest='+ request_data
- end
+end
def post_request(parameters,url)
RestClient.post( url, parameters, headers = {:Accept => 'text/html,image/gif,image/jpeg; q=.2, */*; q=.2'} )
\ No newline at end of file