Skip to content

Instantly share code, notes, and snippets.

@alexcheng1982
Created February 28, 2014 22:14
Show Gist options
  • Save alexcheng1982/9281109 to your computer and use it in GitHub Desktop.
Save alexcheng1982/9281109 to your computer and use it in GitHub Desktop.
require 'savon'
categories = []
def process(categories, category)
categories << {:id => get_category_value(category, 'category_id'),
:name => get_category_value(category, 'name')}
children = get_category_value(category, 'children')
return if children.nil? or children[:item].nil?
childrenItem = children[:item]
childrenItem = [childrenItem] unless childrenItem.kind_of?(Array)
childrenItem.each {|child| process(categories, child)}
end
def get_category_value(category, key)
items = category[:item].select {|item| item.has_key?(:key) and item[:key] == key }
return items[0][:value] unless items.empty?
end
client = Savon.client(wsdl: "http://magentolocal.com:8888/magento/api/soap/?wsdl")
sessionId = client.call(:login, :message => {:username=> 'demo', :apiKey=>'123456'}).body[:login_response][:login_return]
response = client.call(:call, :message => {:sessionId => sessionId, :resourcePath => 'catalog_category.tree'})
category = response.body[:call_response][:call_return]
process(categories, category)
puts categories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment