Skip to content

Instantly share code, notes, and snippets.

# Takes a product id, and a map of variant ids and quantities
def update_inventory(id, vid_quantities)
variants = vid_quantities.map do |vid, qty|
{ "id" => vid, "inventory_quantity" => qty }
end
data = {
"product" => {
"variants" => variants
}
}
> Shopify.request(:get, "products.json?fields=id,title")
=> {"products"=>[{"id"=>2938468037, "title"=>"Ghostbusters Inflatable Stay Puft Marshmallow Man Costume"}]}
Obtained credit - credits remaining: 39
Obtained credit - credits remaining: 38
Obtained credit - credits remaining: 37
Obtained credit - credits remaining: 36
Obtained credit - credits remaining: 35
Obtained credit - credits remaining: 34
Obtained credit - credits remaining: 33
Obtained credit - credits remaining: 32
Obtained credit - credits remaining: 31
Obtained credit - credits remaining: 30
# Run 42 threads to hit the call limit
42.times.each do
Thread.new { Shopify.get_product(1234567890) }
end
# use a shared lock and credits_remaining
@lock = Mutex.new
@credits_remaining = 40
@shopify_url = ENV["SHOPIFY_URL"]
# Sends a request to Shopify, blocking until credits are available
def self.request(method, path, params={})
params[:headers] = {"Content-Type" => "application/json"}
# wait for a credit
> response.headers["HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT"]
"1/40"
// Setup an App namespace for all other Javascript to hook to.
var App = {};
// Watch is a utility for hooking into form changes
//
// Example:
// App.Watch.init("#company-filter", function(el){});
App.Watch = (function() {
function init(el, handler) {
$(el).on("propertychange keyup input paste", function() {
@atomgiant
atomgiant / gist:8247276
Last active January 2, 2016 03:59
JS Module
var App = App || {};
App.Commentable = function(options) {
var options = $.extend({
log_tail: 'is the greatest'
}, options);
return {
log: function(msg) {
console.log(msg + ' ' + options.log_tail);
}
@atomgiant
atomgiant / paperclip
Created August 27, 2013 19:58
Paperclip attachment settings for width / height and thumbnail determination
# contains common attachment methods
# NOTE: this currently assumes the attachment is called 'attachment'
module Attachable
# determines if an attachment may have a thumbnail
def thumbnailable?
!(attachment_content_type =~ %r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png|pdf)$}).nil?
end
def image_dimensions(max=400)
@atomgiant
atomgiant / gist:6270147
Created August 19, 2013 15:03
Notification box css for full width
<style>
#notification-box {
position: absolute;
top: 25px; left: 0;
width: 100%;
padding: 0; margin: 0;
}
</style>
<div id="notification-box">