Module: AppEarnings::Report

Included in:
Amazon::AmazonReport, GooglePlay::PlayReport
Defined in:
lib/app_earnings/report.rb

Overview

Base class for reports

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



4
5
6
# File 'lib/app_earnings/report.rb', line 4

def amount
  @amount
end

#currencyObject

Returns the value of attribute currency.



4
5
6
# File 'lib/app_earnings/report.rb', line 4

def currency
  @currency
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/app_earnings/report.rb', line 4

def name
  @name
end

#transactionsObject

Returns the value of attribute transactions.



4
5
6
# File 'lib/app_earnings/report.rb', line 4

def transactions
  @transactions
end

Class Method Details

.formatted_amount(currency, amount) ⇒ Object



66
67
68
69
# File 'lib/app_earnings/report.rb', line 66

def self.formatted_amount(currency, amount)
  symbol = ISO4217::Currency.from_code(currency).symbol
  "#{currency} #{symbol}#{sprintf('%.2f', amount)}"
end

Instance Method Details

#extract_amount(name, transactions) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/app_earnings/report.rb', line 6

def extract_amount(name, transactions)
  @name = name
  @transactions = transactions
  @total_amount = amount_from_transactions(@transactions)
  @currency = @total_amount[:currency]
  @amount = @total_amount[:amount]
end

#formatted_total_by_productsObject



58
59
60
61
62
63
64
# File 'lib/app_earnings/report.rb', line 58

def formatted_total_by_products
  total_from_in_app_purchases.sort_by { |product| product[:id] }
    .map do |app|
      total_amount = Report.formatted_amount(app[:currency], app[:amount])
      "#{app[:id]} - #{app[:name]}: #{total_amount}"
    end
end

#formatted_transactions_count_by_typeObject



52
53
54
55
56
# File 'lib/app_earnings/report.rb', line 52

def formatted_transactions_count_by_type
  transactions_count_by_type.sort_by { |name, _| name }.map do |tr|
    tr.join(': ')
  end
end

#to_jsonObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/app_earnings/report.rb', line 71

def to_json
  {
    id: @name,
    name: @description,
    transactions_types: transactions_count_by_type,
    total: @amount.round(2),
    currency: @currency,
    subtotals: total_from_in_app_purchases
  }
end

#to_sObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/app_earnings/report.rb', line 82

def to_s
  %Q(#{@name} #{@description}
Transactions: #{formatted_transactions_count_by_type.join(", ")}
Total:
#{Report.formatted_amount(@currency, @amount)}

Sub totals by IAP:
#{formatted_total_by_products.join("\n")}

)
end

#total_from_in_app_purchasesObject



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

def total_from_in_app_purchases
  transactions_from_in_app_purchases_by_id_and_name.map do |iap, tr|
    {
      id: iap.first,
      name: iap.last
    }.merge(amount_from_transactions(tr))
  end
end

#transactions_by_typeObject



14
15
16
# File 'lib/app_earnings/report.rb', line 14

def transactions_by_type
  transactions.group_by { |transaction| transaction[:transaction_type] }
end

#transactions_count_by_typeObject



31
32
33
34
35
36
37
# File 'lib/app_earnings/report.rb', line 31

def transactions_count_by_type
  transaction_count = {}
  transactions_by_type.each do |type, transactions|
    transaction_count[type] = transactions.length
  end
  transaction_count
end

#transactions_from_in_app_purchasesObject



18
19
20
21
22
# File 'lib/app_earnings/report.rb', line 18

def transactions_from_in_app_purchases
  transactions.reject do |tr|
    tr[:sku_id].nil? && tr[:vendor_sku].nil?
  end
end

#transactions_from_in_app_purchases_by_id_and_nameObject



24
25
26
27
28
29
# File 'lib/app_earnings/report.rb', line 24

def transactions_from_in_app_purchases_by_id_and_name
  transactions_from_in_app_purchases.group_by do |tr|
    [tr[:sku_id] || tr[:vendor_sku],
     tr[:item_name] || tr[:product_title]]
  end
end

#transactions_from_productObject



39
40
41
# File 'lib/app_earnings/report.rb', line 39

def transactions_from_product
  transactions - transactions_from_in_app_purchases
end