Class: AppEarnings::Amazon::Reporter
- Inherits:
-
Object
- Object
- AppEarnings::Amazon::Reporter
- Defined in:
- lib/app_earnings/amazon/reporter.rb
Overview
Generates a report based on the data provided
Constant Summary collapse
- AVAILABLE_FORMATS =
%w(json text)
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #as_json ⇒ Object
- #as_text ⇒ Object
- #fetch_exchange_info ⇒ Object
- #full_amount ⇒ Object
- #generate ⇒ Object
-
#initialize(data) ⇒ Reporter
constructor
A new instance of Reporter.
- #refunds ⇒ Object
- #render_as(format = 'text') ⇒ Object
- #report_as(format = 'text') ⇒ Object
Constructor Details
#initialize(data) ⇒ Reporter
Returns a new instance of Reporter.
7 8 9 10 11 12 |
# File 'lib/app_earnings/amazon/reporter.rb', line 7 def initialize(data) @data = data @payments_data = @data.find { |r| r[:report_type] == :payments } @earnings_data = (@data - [@payments_data]).first @exchange_info = fetch_exchange_info end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/app_earnings/amazon/reporter.rb', line 5 def data @data end |
Instance Method Details
#as_json ⇒ Object
83 84 85 86 87 |
# File 'lib/app_earnings/amazon/reporter.rb', line 83 def as_json puts JSON.generate(apps: @reports.map(&:to_json), currency: 'USD', total: full_amount) end |
#as_text ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/app_earnings/amazon/reporter.rb', line 70 def as_text amount = AppEarnings::Report.formatted_amount('USD', full_amount) refund = AppEarnings::Report.formatted_amount('USD', refunds) puts @reports puts "Total of refunds: #{refund}" puts "Total of all transactions: #{amount}" if @payments_full_amount.round(2) != full_amount puts "Total from Payment Report: #{@payments_full_amount.round(2)}" end @reports end |
#fetch_exchange_info ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/app_earnings/amazon/reporter.rb', line 14 def fetch_exchange_info @payments_full_amount = 0.0 @payments_data[:summary].reduce({}) do |all_info, data| all_info[data[:marketplace]] = data[:fx_rate] @payments_full_amount += data[:total_payment].gsub(/,/, '').to_f all_info end end |
#full_amount ⇒ Object
23 24 25 26 |
# File 'lib/app_earnings/amazon/reporter.rb', line 23 def full_amount total = @reports.reduce(0.0) { |a, e| a + e.amount.to_f } total - refunds end |
#generate ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/app_earnings/amazon/reporter.rb', line 37 def generate @reports = [] @data.each do |raw_data| if raw_data[:report_type] == :earnings by_apps = raw_data[:details].group_by { |element| element[:app] } .sort_by { |app| app } by_apps.each do |key, application| @reports << AmazonReport.new(key, application, @exchange_info) end end end end |
#refunds ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/app_earnings/amazon/reporter.rb', line 28 def refunds @earnings_data[:summary].reduce(0.0) do |sum, marketplace| currency = marketplace[:refunds_currency_code] amount = marketplace[:refunds].gsub(/\(|\)/, '').to_f amount = amount * @exchange_info[currency].to_f if currency != 'USD' sum + amount end end |
#render_as(format = 'text') ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/app_earnings/amazon/reporter.rb', line 61 def render_as(format = 'text') case format when 'text' as_text when 'json' as_json end end |
#report_as(format = 'text') ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/app_earnings/amazon/reporter.rb', line 51 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 |