Skip to content

Instantly share code, notes, and snippets.

@cpjk
Created January 20, 2016 05:11
Show Gist options
  • Save cpjk/3f6a5cacfd0a8a29f406 to your computer and use it in GitHub Desktop.
Save cpjk/3f6a5cacfd0a8a29f406 to your computer and use it in GitHub Desktop.
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