Permalink
Browse files

refactor - extract parser and fetcher

  • Loading branch information...
1 parent 5e2d5ed commit c1f5a878beb65c5bb9ae739b86b4a0fd50e3936a @siuying committed Feb 2, 2012
View
@@ -1,89 +1,6 @@
-require 'rest-client'
-require 'zlib'
-class AutoIngestion
- attr_reader :response
-
- BASE_URL = "https://reportingitc.apple.com/autoingestion.tft?"
+module ITunesIngestion
+ class ITunesConnectError < RuntimeError; end
- REPORT_TYPE_SALES = "Sales"
- DATE_TYPE_DAILY = "Daily"
- DATE_TYPE_WEEKLY = "Weekly"
-
- REPORT_SUMMARY = "Summary"
- REPORT_OPT_IN = "Opt-In"
-
- PRODUCT_TYPE_IDENTIFIER = {
- "1" => "Free or Paid Apps, iPhone and iPod Touch",
- "7" => "Updates, iPhone and iPod Touch",
- "IA1" => "In Apps Purchase",
- "IA9" => "In Apps Subscription",
- "IAY" => "Auto-Renewable Subscription",
- "1F" => "Free or Paid Apps (Universal)",
- "7F" => "Updates (Universal)",
- "1T" => "Free or Paid Apps, iPad",
- "7T" => "Updates, iPad",
- "F1" => "Free or Paid Apps, Mac OS",
- "F7" => "Updates, Mac OS",
- "FI1" => "In Apps Purchase, Mac OS",
- "1E" => "Custome iPhone and iPod Touch",
- "1EP" => "Custome iPad",
- "1EU" => "Custome Universal"
- }
-
- def initialize(username, password, vadnumber)
- @username = username
- @password = password
- @vadnumber = vadnumber
- end
-
- def fetch(options={})
- params = {
- :USERNAME => @username, :PASSWORD => @password, :VNDNUMBER => @vadnumber
- }
- params[:TYPEOFREPORT] = options[:type_of_report] || REPORT_TYPE_SALES
- params[:DATETYPE] = options[:date_type] || DATE_TYPE_DAILY
- params[:REPORTTYPE] = options[:report_type] || REPORT_SUMMARY
- params[:REPORTDATE] = options[:report_date] || (Time.now-60*60*24).strftime("%Y%m%d")
-
- @response = RestClient.post BASE_URL, params
- if @response.headers[:"errormsg"]
- raise @response.headers[:"errormsg"]
- elsif @response.headers[:"filename"]
- report = Zlib::GzipReader.new(StringIO.new(@response.body)).read
- parse_report(report)
- else
- raise "no data returned from itunes!"
- end
- end
-
- private
- def parse_report(report)
- lines = report.split("\n")
- header = lines.shift # remove first line
- lines.collect do |line|
- provider, country, sku, developer, title, version, product_type_id, units, developer_proceeds, begin_date, end_date, currency, country_code, currency_of_proceeds, apple_id, customer_price, promo_code, parent_id, subscription, period = line.split("\t")
- {
- :provider => provider.strip,
- :country => country.strip,
- :sku => sku.strip,
- :developer => developer.strip,
- :title => title.strip,
- :version => version.strip,
- :product_type_id => product_type_id.strip,
- :units => units.to_i,
- :developer_proceeds => developer_proceeds.to_f,
- :begin_date => Date.strptime(begin_date.strip, '%m/%d/%Y'),
- :end_date => Date.strptime(end_date.strip, '%m/%d/%Y'),
- :currency => currency.strip,
- :country_code => country_code.strip,
- :currency_of_proceeds => currency_of_proceeds.strip,
- :apple_id => apple_id.to_i,
- :customer_price => customer_price.to_f,
- :promo_code => promo_code.strip,
- :parent_id => parent_id.strip,
- :subscription => subscription.strip,
- :period => period
- }
- end
- end
-end
+ autoload :Fetcher, 'itunes_ingestion/fetcher'
+ autoload :SalesReportParser, 'itunes_ingestion/sales_report_parser'
+end
@@ -0,0 +1,54 @@
+require 'rest-client'
+require 'zlib'
+
+module ITunesIngestion
+ class Fetcher
+ BASE_URL = "https://reportingitc.apple.com/autoingestion.tft?"
+
+ REPORT_TYPE_SALES = "Sales"
+ DATE_TYPE_DAILY = "Daily"
+ DATE_TYPE_WEEKLY = "Weekly"
+
+ REPORT_SUMMARY = "Summary"
+ REPORT_OPT_IN = "Opt-In"
+
+ # Create new instance of Fetcher
+ #
+ # username - username
+ # password - password
+ # vadnumber - vadnumber
+ def initialize(username, password, vadnumber)
+ @username = username
+ @password = password
+ @vadnumber = vadnumber
+ end
+
+ # Fetch and unzip report from itunes connect
+ #
+ # options - Hash of options:
+ # - :type_of_report can only be REPORT_TYPE_SALES at the moment
+ # - :date_type either DATE_TYPE_DAILY or DATE_TYPE_WEEKLY, default DATE_TYPE_DAILY
+ # - :report_type either REPORT_OPT_IN or REPORT_SUMMARY, default REPORT_SUMMARY
+ # - :report_date date in YYYYMMDD
+ #
+ # Returns text file downloaded and unzipped from iTunes
+ def fetch(options={})
+ params = {
+ :USERNAME => @username, :PASSWORD => @password, :VNDNUMBER => @vadnumber
+ }
+ params[:TYPEOFREPORT] = options[:type_of_report] || REPORT_TYPE_SALES
+ params[:DATETYPE] = options[:date_type] || DATE_TYPE_DAILY
+ params[:REPORTTYPE] = options[:report_type] || REPORT_SUMMARY
+ params[:REPORTDATE] = options[:report_date] || (Time.now-60*60*24).strftime("%Y%m%d")
+
+ response = RestClient.post BASE_URL, params
+ if response.headers[:"errormsg"]
+ raise ITunesConnectError.new response.headers[:"errormsg"]
+ elsif response.headers[:"filename"]
+ Zlib::GzipReader.new(StringIO.new(response.body)).read
+ else
+ raise "no data returned from itunes: #{response.body}"
+ end
+ end
+ end
+end
@@ -0,0 +1,59 @@
+require 'date'
+
+module ITunesIngestion
+ class SalesReportParser
+ PRODUCT_TYPE_IDENTIFIER = {
+ "1" => "Free or Paid Apps, iPhone and iPod Touch",
+ "7" => "Updates, iPhone and iPod Touch",
+ "IA1" => "In Apps Purchase",
+ "IA9" => "In Apps Subscription",
+ "IAY" => "Auto-Renewable Subscription",
+ "1F" => "Free or Paid Apps (Universal)",
+ "7F" => "Updates (Universal)",
+ "1T" => "Free or Paid Apps, iPad",
+ "7T" => "Updates, iPad",
+ "F1" => "Free or Paid Apps, Mac OS",
+ "F7" => "Updates, Mac OS",
+ "FI1" => "In Apps Purchase, Mac OS",
+ "1E" => "Custome iPhone and iPod Touch",
+ "1EP" => "Custome iPad",
+ "1EU" => "Custome Universal"
+ }
+
+ # Parse sales report
+ #
+ # report - text based report form itunesconnect
+ #
+ # Returns array of hash, each hash contains one line of sales report
+ def self.parse(report)
+ lines = report.split("\n")
+ header = lines.shift # remove first line
+ lines.collect do |line|
+ provider, country, sku, developer, title, version, product_type_id, units, developer_proceeds, begin_date, end_date, currency, country_code, currency_of_proceeds, apple_id, customer_price, promo_code, parent_id, subscription, period = line.split("\t")
+ {
+ :provider => provider.strip,
+ :country => country.strip,
+ :sku => sku.strip,
+ :developer => developer.strip,
+ :title => title.strip,
+ :version => version.strip,
+ :product_type_id => product_type_id.strip,
+ :units => units.to_i,
+ :developer_proceeds => developer_proceeds.to_f,
+ :begin_date => Date.strptime(begin_date.strip, '%m/%d/%Y'),
+ :end_date => Date.strptime(end_date.strip, '%m/%d/%Y'),
+ :currency => currency.strip,
+ :country_code => country_code.strip,
+ :currency_of_proceeds => currency_of_proceeds.strip,
+ :apple_id => apple_id.to_i,
+ :customer_price => customer_price.to_f,
+ :promo_code => promo_code.strip,
+ :parent_id => parent_id.strip,
+ :subscription => subscription.strip,
+ :period => period
+ }
+ end
+ end
+ end
+
+end
View
@@ -0,0 +1,6 @@
+require File.expand_path("../../lib/itunes_ingestion", __FILE__)
+
+describe ITunesIngestion::Fetcher do
+ # No test for fetcher right now
+end
+
View
@@ -0,0 +1,3 @@
+Provider Provider Country SKU Developer Title Version Product Type Identifier Units Developer Proceeds Begin Date End Date Customer Currency Country Code Currency of Proceeds Apple Identifier Customer Price Promo Code Parent Identifier Subscription Period
+APPLE US UT124500 Your Company App1 1.0.0 10 970 0 02/01/2011 02/01/2011 USD TW USD 10001000 0
+APPLE US UA124502 Your Company App2 1.2.0 700 7100 0 02/01/2011 02/01/2011 JPY JP JPY 10001001 0
@@ -0,0 +1,20 @@
+require File.expand_path("../../lib/itunes_ingestion", __FILE__)
+
+describe ITunesIngestion::SalesReportParser do
+ let (:data) { open(File.expand_path("../report/report.txt", __FILE__)).read }
+
+ it "parse report" do
+ report = ITunesIngestion::SalesReportParser.parse(data)
+ report.class.should == Array
+ report.length.should == 2
+
+ first_report = report.first
+ first_report[:sku].should == "UT124500"
+ first_report[:product_type_id].should == "10"
+ first_report[:title].should == "App1"
+ first_report[:begin_date].should == Date.new(2011, 2, 1)
+ first_report[:country_code].should == "TW"
+ first_report[:currency].should == "USD"
+ first_report[:currency_of_proceeds].should == "USD"
+ end
+end

0 comments on commit c1f5a87

Please sign in to comment.