Skip to content

Instantly share code, notes, and snippets.

@shaabans
Last active August 29, 2015 14:04
Show Gist options
  • Save shaabans/5ebe289b49cfae0787dc to your computer and use it in GitHub Desktop.
Save shaabans/5ebe289b49cfae0787dc to your computer and use it in GitHub Desktop.
Spree API: Add Product, Order, LineItem
require 'rest-client'
require 'pp'
@sku = 'SKU4'
@order_no = '204'
@spree_url = 'http://localhost:3001'
@api_token = 'c06f3e32ae0ff72a9ba6ac496a83ede90119797f4c1f4887'
def post resource, data
begin
response = RestClient.post @spree_url + '/api/' + resource, data,
:content_type => :json, :accept => :json,
:'X-Spree-Token' => @api_token
return JSON.parse response
rescue
pp response
end
end
product = post 'products',
{
"product" => {
"sku" => @sku,
"price" => 15.99,
"shipping_category_id" => 0,
"name" => "Test Thingie",
"description" => "Voluptatibus dolor testicus."
}
}
puts "Created product with id = #{product['id']} and variant_id = #{product['master']['id']}."
order = post 'orders',
{
"order" => {
"number" => @order_no,
"user_id" => 1,
"completed_at" => "2014-07-31 22:23:47"
}
}
puts "Created order with number = #{order['number']} and state = #{order['state']}."
li = post "orders/#{order['number']}/line_items",
{
"line_item" => {
"quantity" => 22,
"variant_id" => product['master']['id']
}
}
puts "Created line_item with quantity = #{li['quantity']} and variant_id = #{li['variant_id']}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment