Class: AppEarnings::Amazon::AmazonReport

Inherits:
Object
  • Object
show all
Includes:
Report
Defined in:
lib/app_earnings/amazon/amazon_report.rb

Overview

Converts a csv file to a hash.

Instance Attribute Summary collapse

Attributes included from Report

#amount, #currency, #name, #transactions

Instance Method Summary collapse

Methods included from Report

#extract_amount, formatted_amount, #formatted_total_by_products, #formatted_transactions_count_by_type, #to_json, #to_s, #total_from_in_app_purchases, #transactions_by_type, #transactions_count_by_type, #transactions_from_in_app_purchases, #transactions_from_in_app_purchases_by_id_and_name, #transactions_from_product

Constructor Details

#initialize(name, transactions, exchange_info) ⇒ AmazonReport

Returns a new instance of AmazonReport.



11
12
13
14
# File 'lib/app_earnings/amazon/amazon_report.rb', line 11

def initialize(name, transactions, exchange_info)
  @exchange_info = exchange_info
  extract_amount(name, transactions)
end

Instance Attribute Details

#exchange_infoObject

Returns the value of attribute exchange_info.



9
10
11
# File 'lib/app_earnings/amazon/amazon_report.rb', line 9

def exchange_info
  @exchange_info
end

Instance Method Details

#amount_from_transactions(transactions) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/app_earnings/amazon/amazon_report.rb', line 23

def amount_from_transactions(transactions)
  amounts = transactions.reduce({}) do |sum, transaction|
    marketplace = transaction[:marketplace]
    sum[marketplace] ||= 0.0
    sum[marketplace] += transaction[:gross_earnings_or_refunds].to_f
    sum
  end

  {
    currency: 'USD',
    amount: convert_amounts(amounts).round(2)
  }
end

#convert_amounts(amounts) ⇒ Object



16
17
18
19
20
21
# File 'lib/app_earnings/amazon/amazon_report.rb', line 16

def convert_amounts(amounts)
  amounts.reduce(0.0) do |sum, (marketplace, amount)|
    amount = amount * @exchange_info[marketplace].to_f
    sum + amount
  end
end