Created
April 14, 2010 18:52
-
-
Save Vetal4eg/366173 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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