Created
January 20, 2016 05:11
-
-
Save cpjk/3f6a5cacfd0a8a29f406 to your computer and use it in GitHub Desktop.
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
require 'net/http' | |
require 'json' | |
HOST = 'shopicruit.myshopify.com' | |
PRODUCT_PATH = '/products.json' | |
def get_computer_and_keyboard_cost() | |
json_str = Net::HTTP.get HOST, PRODUCT_PATH | |
json_dict = JSON.parse json_str | |
filtered_products = json_dict["products"] | |
.select { |product| ["Keyboard", "Computer"].include? product["product_type"] } | |
variants = filtered_products.map { |product| product["variants"] }.flatten | |
variants_by_weight = variants.sort_by { |variant| variant["grams"] } | |
total_weight_in_grams = 0 | |
total_cost_in_dollars = 0 | |
variants_by_weight.each do |variant| | |
weight_in_grams = variant["grams"].to_f | |
price_in_dollars = variant["price"].to_f | |
break if total_weight_in_grams + weight_in_grams > 100_000 | |
total_weight_in_grams += weight_in_grams | |
total_cost_in_dollars += price_in_dollars | |
end | |
return total_cost_in_dollars | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment