Class: ItunesConnect::Report

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/itunes_connect/report.rb

Overview

This class transforms the raw input given in the constructor into a series of objects representing each row. You can either get the entire set of data by accessing the data attribute, or by calling the each method and handing it a block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Report

Give me an IO-like object (one that responds to the each_line method) and I’ll parse that sucker for you.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/itunes_connect/report.rb', line 17

def initialize(input)
  @data = Hash.new { |h,k| h[k] = { }}
  input.each_line do |line|
    line.chomp!
    next if line =~ /^(Provider|$)/
    tokens = line.split(/\s+/)
    country = tokens[11]
    count = tokens[6].to_i
    @data[country][:date] = Date.strptime(tokens[8], "%m/%d/%Y")
    case tokens[5].to_i
    when 7
      @data[country][:upgrade] = count
    when 1
      @data[country][:install] = count
    end
  end
end

Instance Attribute Details

#dataObject (readonly)

The report as a Hash, where the keys are country codes and the values are Hashes with the keys, :date, :upgrade, :install.



13
14
15
# File 'lib/itunes_connect/report.rb', line 13

def data
  @data
end

Instance Method Details

#eachObject

Yields each parsed data row to the given block. Each item yielded has the following attributes:

* country
* date
* install_count
* upgrade_count


41
42
43
44
45
46
47
48
49
50
# File 'lib/itunes_connect/report.rb', line 41

def each                      # :yields: record
  @data.each do |country, value|
    if block_given?
      yield OpenStruct.new(:country => country,
                           :date => value[:date],
                           :install_count => value[:install] || 0,
                           :upgrade_count => value[:upgrade] || 0)
    end
  end
end

#sizeObject

The total number of rows in the report



53
54
55
# File 'lib/itunes_connect/report.rb', line 53

def size
  @data.size
end