Skip to content

Instantly share code, notes, and snippets.

@mcka1n
Created October 29, 2015 04:41
Show Gist options
  • Save mcka1n/7b97d43c7556dfa43311 to your computer and use it in GitHub Desktop.
Save mcka1n/7b97d43c7556dfa43311 to your computer and use it in GitHub Desktop.
shopicruit.rb, calculates how much all lamps and wallets would cost. Just run `ruby shopicruit.rb`
require 'net/http'
require 'json'
def get_products
url = URI.parse('http://shopicruit.myshopify.com/products.json')
req = Net::HTTP::Get.new(url.to_s)
response = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
raw_json = response.body
JSON.parse(raw_json)
end
def sum_variants variants_array
variants_array.map { |h| h['available'] ? h['price'].to_f : 0 }.reduce(:+)
end
def total_price product
product.map { |h| sum_variants(h['variants']) }.reduce(:+)
end
# Do the math
data = get_products
wallets = data['products'].select{|h| h['product_type'] == 'Wallet'}
lamps = data['products'].select{|h| h['product_type'] == 'Lamp'}
wallets_price = total_price wallets
lamps_price = total_price lamps
total_price = wallets_price + lamps_price
# Output
puts "==============================================================="
puts "============= Total prices for Wallets and Lamps =============="
puts "Cost of all wallets: #{wallets_price}"
puts "Cost of all lamps: #{lamps_price}"
puts " ------------"
puts "Total price: $ #{total_price}"
puts "=========================== End ==============================="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment