lib/parseitc.rb in parseitc-0.1.2 vs lib/parseitc.rb in parseitc-0.1.3

- old
+ new

@@ -1,115 +1,10 @@ #!/usr/bin/env ruby -module ParseITC - class TransactionParser - attr_accessor :transactions - Version = '0.1.2' - # files can be either string or array - def initialize(files=[]) - @transactions = [] - [files].flatten.each do |file| - add_file file - end - end - def add_file file - lines = File.readlines(file) - lines.shift if lines.first.match(/^Provider/) - lines.each do |xion| - add_transaction xion - end - end +require 'lib/parseitc/transaction' +require 'lib/parseitc/parser' - def add_transaction transaction - @transactions << Transaction.new(transaction.split(/\t|\n/)) - end - - private - def method_missing(method_id, *arguments) - match = /numbers_by_([_a-zA-Z]\w*)/.match(method_id.to_s) - if match - numbers_by(match) - else - super - end - end - - def allowed_fields - first_xion = @transactions.first - first_xion.instance_variables.map{|field| field[1..-1]} + - first_xion.public_methods - end - - def numbers_by(match) - field = match.captures.first - raise NoMethodError.new("#{match}") unless allowed_fields.include? field - values = {} - @transactions.map do |xion| - value = xion.send(field.to_sym) - values[value] = (values[value] || 0) + xion.units.to_i - end - values - end - # end private - end - - class Transaction - YEAR_TWO_THOUSAND = Date.parse("1/1/2000") - TWO_THOUSAND_YEARS = YEAR_TWO_THOUSAND - Date.parse("1/1/0") - - # ignoring some columns that are ignored by iphone apps - attr_accessor :provider, # Apple (always) - :provider_country, # US (always?) - :company, # company x - :product, # widgets and sprockets - :vendor_identifier, # id you give the product - :date, # just take the begin date - :units, - :royalty_price, # this is what you get paid - :royalty_currency, - :customer_price, - :customer_currency, - :vendor_offer_code, - :promo_code, - :country, - :apple_identifier # itunes link id - - alias :price :royalty_price - - def initialize array - raise WrongNumberOfElementsException.new(27, array.length) unless array.length == 27 - @provider = array[0] - @provider_country = array[1] - @company = array[5] - @product = array[6] - @vendor_identifier = array[2] - @date = Date.parse(array[11]) - @units = array[9] - @royalty_price = array[10] - @royalty_currency = array[15] - @customer_price = array[20] - @customer_currency = array[13] - @vendor_offer_code = array[23] - @promo_code = array[25] - @country = array[14] - @apple_identifier = array[19] - - # Adjust date if necessary ("1/5/10" should not be 10 A.D.) - @date += TWO_THOUSAND_YEARS if @date < YEAR_TWO_THOUSAND - end - - def price_tier - prices = ApplePricing[@royalty_currency.downcase.to_sym] - prices.find_index(@royalty_price.to_f) - end - end - - class WrongNumberOfElementsException < Exception - def initialize(expected, actual) - super "Invalid number of elements (#{actual}). Expected #{expected} values" - end - end - +module ParseITC # pricing tier info ApplePricing = { :usd => [0, 0.7, 1.4, 2.1, 2.8, 3.5, 4.2, 4.9, 5.6, 6.3, 7, 7.7, 8.4, 9.1, 9.8, 10.5, 11.2, 11.9, 12.6, 13.3, 14, 14.7, 15.4, 16.1, 16.8, 17.5, 18.2, 18.9, 19.6, 20.3, 21, 21.7, 22.4, 23.1, 23.8, 24.5, 25.2, 25.9, 26.6, 27.3, 28, 28.7, 29.4, 30.1, 30.8, 31.5, 32.2, 32.9, 33.6, 34.3, 35, 38.5, 42, 45.5, 49, 52.5, 56, 59.5, 63, 66.5, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 210, 245, 280, 315, 350, 420, 490, 560, 630, 700], :cad => [0, 0.7, 1.4, 2.1, 2.8, 3.5, 4.2, 4.9, 5.6, 6.3, 7, 7.7, 8.4, 9.1, 9.8, 10.5, 11.2, 11.9, 12.6, 13.3, 14, 14.7, 15.4, 16.1, 16.8, 17.5, 18.2, 18.9, 19.6, 20.3, 21, 21.7, 22.4, 23.1, 23.8, 24.5, 25.2, 25.9, 26.6, 27.3, 28, 28.7, 29.4, 30.1, 30.8, 31.5, 32.2, 32.9, 33.6, 34.3, 35, 38.5, 42, 45.5, 49, 52.5, 56, 59.5, 63, 66.5, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 210, 245, 280, 315, 350, 420, 490, 560, 630, 700], :aud => [0, 0.76, 1.58, 2.54, 3.18, 3.81, 5.08, 5.72, 6.36, 7.63, 8.27, 8.9, 9.54, 10.18, 10.81, 11.45, 12.08, 12.72, 13.99, 14.63, 15.27, 15.9, 17.18, 17.81, 18.45, 19.08, 20.36, 20.99, 21.63, 22.27, 23.54, 24.18, 24.81, 25.45, 26.72, 27.36, 27.99, 28.63, 29.27, 29.9, 30.54, 31.18, 31.81, 33.08, 33.72, 34.36, 34.99, 36.27, 36.9, 37.54, 38.18, 44.54, 47.72, 50.9, 57.27, 60.45, 63.63, 66.81, 69.99, 73.18, 76.36, 89.08, 95.45, 101.81, 108.18, 120.9, 127.27, 139.99, 146.36, 152.72, 159.08, 165.45, 171.81, 178.18, 184.54, 190.9, 222.72, 254.54, 286.36, 318.18, 381.81, 445.45, 509.08, 572.72, 636.36, 763.63],