Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created March 27, 2010 14:36
Show Gist options
  • Save francescoagati/346110 to your computer and use it in GitHub Desktop.
Save francescoagati/346110 to your computer and use it in GitHub Desktop.
drupal client with xmlrpc
require 'rubygems'
require 'xmlrpc/client'
# an example of client for drupal made with xmlrpc
class Drupal
def initialize(opt={},&blk)
@client = XMLRPC::Client.new( opt[:host] , "/?q=services/xmlrpc")
@sess=@client.call("system.connect", 1)
@sess=@client.call("user.login",@sess["sessid"],opt[:user],opt[:password])
exec_block(blk) if blk!=nil
end
def session
@sess
end
def node(nid)
@client.call("node.get",@sess["sessid"],nid)
end
def view(name,params_list)
@client.call("views.get",@sess["sessid"],name.to_s,"",params_list.split(","))
end
def node_view(name,params_list)
res=view(name,params_list)
res.map {|el| node(el['nid']) }
end
def user(uid)
@client.call("user.get",@sess["sessid"],uid)
end
def exec_block(blk)
blk.call(self) if blk.arity>0
instance_eval(&blk) if blk.arity==-1
end
class << self
def test_login(&blk)
self.new(:host => "localhost", :user => 'admin', :password=> 'admin',&blk)
end
end
end
Drupal.new :host => "localhost", :user => 'admin', :password=> 'admin' do
p node(1)
end
Drupal.new :host => "localhost", :user => 'admin', :password=> 'admin' do |site|
p site.node(1)
end
Drupal.test_login do
p node(1)
p user(1)
end
Drupal.test_login do |site|
p site.node(1)
p site.user(1)
end
site=Drupal.test_login
site.node(1).instance_eval {
self["user_content"]=site.user self["uid"]
p self
}
nodes=site.view("contents",'page').map {|nd| site.node(nd['nid']) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment