Shopify API Extensions

Published on June 15, 2009 by Jesse Storimer

Excited by the release of the Shopify platform, I have been playing a lot with the Shopify API and building some apps for the new App Store.

The Shopify API is super easy to work with, thanks to the fact that it provides an ActiveResource interface, and of course I write all of my apps in Ruby. In the course of playing with the API, I have needed some specific helpers not provided by the Shopify API, more specifically, things that ActiveResource doesn’t do well. So I added a few helpers to the Shopify API that others might find useful.

Base class

The official shopify_app plugin uses ActiveResource::Base when setting the site, username, password, etc. This can potentially cause problems if you are working with multiple ActiveResource API’s in your application. I have added a ShopifyAPI::Base class so that there is never any spillover of username’s and password’s between different ActiveResource API’s.

Batched find

Can be used like:

This is especially useful if you need to do something to all products, or all orders, or whatever, in a background task. There is no easy way to ask for all of a particular object using ActiveResource, so this method just asks for a batch at a time, and lets you work with each batch until there is no more.

By default, it uses a batch size of 100, but you can override that by passing in a limit param, like so:

This will work in the same but will fetch things in smaller batches instead of 100 at a time.

Count

ActiveResource does not provide a nice way to ask for a count of objects, nor does the Shopify API. Now you can do something like:

to get a count of all the orders for a particular shop. Once again this is actually finding all orders in batches and just recording the total number of orders returned.

Word of warning

Both of these methods involve fetching all of a particular type of object through the Shopify API. Since some shops may have hundreds or thousands of orders, products, etc. it is not wise to do this in a user-facing screen. These things should be reserved for background processes only.

Check out my fork of the shopify_app plugin on github to get these goodies.


Like what you read?

Join 2,000+ Ruby programmers improving their skills with exclusive content about digging deeper with sockets, processes, threads, and more - delivered to your inbox weekly.

I'll never send spam and you can unsubscribe any time.


comments powered by Disqus