Skip to content

Instantly share code, notes, and snippets.

@romerobrjp
Created January 24, 2017 19:50
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 romerobrjp/505765ce181b59649d97f43ca84cf080 to your computer and use it in GitHub Desktop.
Save romerobrjp/505765ce181b59649d97f43ca84cf080 to your computer and use it in GitHub Desktop.
Shopify challenge
# 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 your reply.
require 'net/http'
require 'json'
class Shopicruit
def main
shopify_json_uri = 'http://shopicruit.myshopify.com/products.json?page=1'
puts
puts "*** Initializing system, please wait ***"
all_products = get_products(shopify_json_uri)
puts
while true do
puts
puts "----- -------------------------- ----- "
puts "----- Shopify Store 'Shopicruit' ----- "
puts "----- -------------------------- ----- "
puts
puts " 0 - Exit."
puts " 1 - Calculate total price of products."
puts " 2 - Calculate total price of all Wallets and Lamps."
puts
option = gets.chomp
case option
when '0'
bye
break
when '1'
print "What type of product? "
answer = gets.chomp
begin
do_challenge(answer, all_products)
rescue Exception
puts "There are no products of type '#{answer}', sorry. Try another one. Press ENTER to continue."
gets.chomp
next
end
puts
print "Wanna calculate another product? (y/n): "
if gets.chomp == 'n'
bye
break
end
when '2'
begin
total_count = 0
total_sum = 0.0
wallets_hash = do_challenge('Wallet', all_products)
lamps_hash = do_challenge('Lamp', all_products)
total_count += wallets_hash[:count]
total_count += lamps_hash[:count]
total_sum += wallets_hash[:price_sum]
total_sum += lamps_hash[:price_sum]
puts "Total price of Wallets + Lamps: #{total_count} products for $#{total_sum}"
puts
rescue Exception
puts "There are no products of type '#{answer}', sorry. Try another one. Press ENTER to continue."
gets.chomp
next
end
else
next
end
end
end
def do_challenge(product_type, all_products)
raise 'Inexistent product type' if product_type == nil or product_type == ''
filtered_products = filter_products_by_type(all_products, product_type)
products_count = filtered_products.length
products_price_sum = get_products_price(filtered_products).round(2)
puts
puts "Total #{product_type}s: #{products_count}"
puts "Total price: #{products_price_sum}"
puts
result = Hash.new
result = {count: products_count, price_sum: products_price_sum}
end
private
def get_products(url)
uri = URI(url)
response = Net::HTTP.get(uri)
json = JSON.parse(response)
products_json = json['products']
end
def filter_products_by_type(all_products, product_type)
filtered_products = Array.new
all_products.each_with_index do |product, product_index|
if product.has_value? product_type
filtered_products << product
end
end
raise Exception, "There are no products of type #{product_type}" if filtered_products.length == 0
filtered_products
end
def get_products_price(filtered_products)
product_price_sum = 0.0
filtered_products.each_with_index do |product, product_index|
product['variants'].each_with_index do |variant, variant_index|
product_price_sum += variant['price'].to_f
end
end
product_price_sum
end
def bye
puts
puts "----- --------------------- ----- "
puts "----- You are welcome, bye! ----- "
puts "----- --------------------- ----- "
puts
end
end
s = Shopicruit.new
s.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment