Skip to content

Instantly share code, notes, and snippets.

@guewen
Last active February 23, 2016 13:59
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save guewen/1875404 to your computer and use it in GitHub Desktop.
Save guewen/1875404 to your computer and use it in GitHub Desktop.
Magento API simple client in Ruby
# Copyright Camptocamp SA 2012
# License: AGPL (GNU Affero General Public License)[http://www.gnu.org/licenses/agpl-3.0.txt]
# Author Guewen Baconnier
require "xmlrpc/client"
require 'pp'
XMLRPC::Config::ENABLE_NIL_PARSER = true
XMLRPC::Config::ENABLE_NIL_CREATE = true
class MagentoAPI
attr_accessor :url, :api_user, :api_key
def initialize(base_url, api_user, api_key, options={})
@url = base_url << '/api/xmlrpc/'
@api_user = api_user
@api_key = api_key
@debug = options[:debug] || false
@client = init_client
end
def call(method, *arguments)
@client.call('call', session_id, method, arguments)
end
private
def init_client
client = XMLRPC::Client.new2(@url)
http_debug(@debug)
client.set_debug
client
end
def http_debug(active)
output = active ? $stderr : false
XMLRPC::Client.class_eval do
define_method :set_debug do
@http.set_debug_output(output)
end
end
end
def session_id
@client.call('login', @api_user, @api_key)
end
end
@guewen
Copy link
Author

guewen commented Feb 21, 2012

Example:

require 'magento_api'

magento = MagentoAPI.new('http://localhost/magento', 'API_USER', 'API_KEY', :debug => true)
order_infos = magento.call('sales_order.info', '100011892')

magento.call('catalog_product.update', 'SKU', {'meta_description' => 'test'})

@michaxze
Copy link

Thank you. It really is a great help! :)

@michaxze
Copy link

Btw, how to handle pagination?

@guewen
Copy link
Author

guewen commented Feb 12, 2013

Hi michaxze, as far as I know, the API magento itself does not support pagination.

@muhammadtaher
Copy link

Hello, How can I use a filter for orders to get orders that were created FROM a specific date?
Thank you.

@guewen
Copy link
Author

guewen commented Sep 7, 2015

Hi,

I did not tested but I think it should be something like:

order_infos = magento.call('sales_order.list', {{'created_at': {'from': '2015-09-07 00:00:00'}})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment