Skip to content

Instantly share code, notes, and snippets.

@qubbit
Created October 17, 2019 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qubbit/36a2086615acf800e7e9a123f9ff57ca to your computer and use it in GitHub Desktop.
Save qubbit/36a2086615acf800e7e9a123f9ff57ca to your computer and use it in GitHub Desktop.
Script to show my uber monthly fare total
#! /usr/bin/ruby
require 'json'
require 'time'
json = File.read('trips.json')
data = JSON(json, symbolize_names: true)
CURRENCY_CODE_MAP = {'USD'=> '$', 'EUR'=> '€', 'GBP' => '£'}
def format_date(date)
"#{date.month}/#{date.year}"
end
def format_date2(date)
"#{date.month}/#{date.day}/#{date.year}"
end
result = data.map do |d|
next if d[:status] == 'CANCELED'
amount = d[:trip][:clientFare]
currency = CURRENCY_CODE_MAP.fetch(d[:trip][:currencyCode], 'Unknown currency')
date = Time.parse(d[:trip][:requestTime])
{amount: amount, amount_formatted: "#{currency}#{amount}", date: date}
end.sort_by { |e| e[:date] }.reduce({}) do |acc, f|
month = format_date(f[:date])
if acc.key?(month)
acc[month] << f
else
acc[month] = [f]
end
acc
end
result.keys.each do |k|
puts
puts k
puts '-------'
result[k].each do |t|
puts "#{format_date2(t[:date])}\t#{t[:amount_formatted]}"
end
total = result[k].map{ |t| t[:amount] }.reduce(:+)
puts "Total: $#{total}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment