Created
August 23, 2020 16:44
-
-
Save neilgupta/4c4458989f086811f8262d0692ca1875 to your computer and use it in GitHub Desktop.
Tovala meals history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Print out your last 5 tovala deliveries, along with who the chef was and whether it was a best seller/chef's pick or not | |
# Add "csv" arg to get in csv format for easy adding to spreadhsheet (`./tovala.rb csv`) | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
require 'date' | |
USERID = 'YOUR TOVALA USER ID' | |
TOKEN = 'YOUR TOVALA TOKEN' | |
uri = URI.parse("https://api.beta.tovala.com/v1/users/#{USERID}/orderHistory?limit=5&offset=0") | |
request = Net::HTTP::Get.new(uri) | |
request["Authorization"] = "Bearer #{TOKEN}" | |
req_options = { | |
use_ssl: uri.scheme == "https", | |
} | |
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| | |
http.request(request) | |
end | |
if ARGV[0] == 'csv' | |
JSON.parse(response.body)['data'].reverse.each do |f| | |
next unless f['fulfillment'] | |
date = Date.parse(f['term']['start']).strftime("%b %-d") | |
f['fulfillment']['meals'].uniq{|m| m['id']}.each do |m| | |
tags = m['tags'].map{|t| t['title']} | |
is_chefs_pick = tags.include?("Chef's Pick") ? 'x' : '' | |
is_best_seller = tags.include?("Best Seller") ? 'x' : '' | |
puts [date, m['title'], m['subtitle'].sub(/^with /, ''), m['created_by'], is_chefs_pick, is_best_seller].join(',') | |
end | |
puts | |
end | |
else | |
JSON.parse(response.body)['data'].reverse.each do |f| | |
next unless f['fulfillment'] | |
date = Date.parse f['term']['start'] | |
puts "🚚 Meals for week of #{date.strftime("%b %-d")}" | |
puts | |
f['fulfillment']['meals'].uniq{|m| m['id']}.each do |m| | |
puts " 🥗 #{m['title']} #{m['subtitle']}" | |
puts " 👩🍳 #{m['created_by']}" | |
print " " | |
puts m['tags'].filter{|t| ["Chef's Pick", "Best Seller"].include?(t['title'])}.map{|t| "🌟 #{t['title']}"}.join(', ') | |
puts | |
end | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment