class AppEarnings::GooglePlay::Reporter

Generates a report based on the data provided

Constants

AVAILABLE_FORMATS

Attributes

raw_data[RW]

Public Class Methods

new(raw_data) click to toggle source
# File lib/app_earnings/google_play/reporter.rb, line 11
def initialize(raw_data)
  @raw_data = raw_data
end

Public Instance Methods

as_json() click to toggle source
# File lib/app_earnings/google_play/reporter.rb, line 58
def as_json
  currency, total = full_amount
  puts JSON.generate(apps: @reports.map(&:to_json),
                     currency: currency,
                     total: total)
end
as_text() click to toggle source
# File lib/app_earnings/google_play/reporter.rb, line 50
def as_text
  currency, total = full_amount
  formatted_amount = AppEarnings::Report.formatted_amount(currency, total)
  puts @reports
  puts "Total of all transactions: #{formatted_amount}"
  @reports
end
full_amount() click to toggle source
# File lib/app_earnings/google_play/reporter.rb, line 25
def full_amount
  total = @reports.reduce(0.0) { |a, e| a + e.amount.to_f }
  currency = @reports.first.currency
  [currency, total]
end
generate() click to toggle source
# File lib/app_earnings/google_play/reporter.rb, line 15
def generate
  by_apps = @raw_data.group_by { |element| element[:product_id] }
                     .sort_by { |app| app }

  @reports = []
  by_apps.each do |key, application|
    @reports << PlayReport.new(key, application)
  end
end
render_as(format = 'text') click to toggle source
# File lib/app_earnings/google_play/reporter.rb, line 41
def render_as(format = 'text')
  case format
  when 'text'
    as_text
  when 'json'
    as_json
  end
end
report_as(format = 'text') click to toggle source
# File lib/app_earnings/google_play/reporter.rb, line 31
def report_as(format = 'text')
  unless AVAILABLE_FORMATS.include?(format)
    fail "#{format} Not supported. Available formats are: " +
         " #{AVAILABLE_FORMATS.join(", ")}"
  end

  generate
  render_as(format)
end