Skip to content

Instantly share code, notes, and snippets.

@Vetal4eg
Created Apr 14, 2010
Embed
What would you like to do?
require 'net/http'
require "rexml/document"
include REXML # so that we don't have to prefix everything with REXML::...
API_KEY, PASSWORD = "your_api_key", "your_password"
doc = Document.new
doc << XMLDecl.new('1.0', 'UTF-8')
root = doc.add_element 'product'
type = root.add_element "product-type"
body = root.add_element "body"
title = root.add_element "title"
tags = root.add_element "tags"
vendor = root.add_element "vendor"
sku = root.add_element "sku"
type.text = "Art Print"
body.text = "Body text"
title.text = "Title text"
tags.text = "animal,water"
vendor.text = "My name"
sku.text = "123456"
shopify_url = 'https://your-shop-domain.myshopify.com/admin/products.xml'
# prepare a buffer that the document will be written to
xml_string = ''
# pretty print with 2 spaces as indentation
doc.write(xml_string, 2)
url = URI.parse(shopify_url)
request = Net::HTTP::Post.new(url.path)
# use basic authentication
request.basic_auth(API_KEY, PASSWORD)
request.body = xml_string
request.content_type = 'text/xml'
response = Net::HTTP.start(url.host) {|http| http.request(request)}
puts xml_string
puts response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment