Last active
October 2, 2015 21:06
-
-
Save acbilimoria/058994b564ef073d6a5f to your computer and use it in GitHub Desktop.
Shopify Shop Circuit
This file contains hidden or 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
#You've discovered the Shopify Store 'Shopicruit'. Since you're obsessed with lamps and wallets, | |
#you want to buy every single lamp and wallet variant they have to offer. By inspecting the | |
#Javascript calls on the store you discovered that the shop lists products at | |
#http://shopicruit.myshopify.com/products.json. Write a program that calculates how much all | |
#lamps and wallets would cost you. Attach your program (any language) and answer in the cover letter | |
#field below." | |
require 'unirest' | |
require 'bigdecimal' | |
require 'bigdecimal/util' | |
response = Unirest.get('https://shopicruit.myshopify.com/products.json') | |
products = response.body["products"] | |
number_of_lamps = 0 | |
number_of_wallets = 0 | |
cost_of_lamps = 0 | |
cost_of_wallets = 0 | |
products.each do |product| | |
if (product["product_type"] == "Wallet") || (product["product_type"] == "wallet") | |
product["variants"].each do |variant| | |
wallet_cost = variant["price"].to_d | |
number_of_wallets += 1 | |
cost_of_wallets += wallet_cost | |
puts "Purchased #{variant["type"]} #{product["title"]} for $#{wallet_cost.round(2).to_f}." | |
end | |
elsif (product["product_type"] == "Lamp") || (product["product_type"] == "lamp") | |
product["variants"].each do |variant| | |
lamp_cost = variant["price"].to_d | |
number_of_lamps += 1 | |
cost_of_lamps += lamp_cost | |
puts "Purchased #{variant["type"]} #{product["title"]} for $#{lamp_cost.round(2).to_f}." | |
end | |
end | |
end | |
total_cost = (cost_of_lamps + cost_of_wallets) | |
total_items = (number_of_lamps + number_of_wallets) | |
puts "Cool! You bought #{number_of_lamps} lamps for $#{cost_of_lamps.round(2).to_f}, and | |
#{number_of_wallets} wallets for $#{cost_of_wallets.round(2).to_f}. That's #{total_items} things | |
for only $#{total_cost.round(2).to_f}!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment