Module: YamlRpc

Defined in:
lib/yamlrpc/client.rb,
lib/yamlrpc.rb,
lib/yamlrpc/helpers.rb

Overview

Simple RPC client on YAML

Example usage:

yamlrpc = YamlRpc.new('http://rubyforge.org/projects/yamlrpc/')
yamlrpc.foo({
  :bar => 'foo-bar'
)

will HTTP POST

{
  :bar => YAML::dump('foo-bar')
}

to rubyforge.org/projects/yamlrpc/foo

On the server side you can use (Ruby on Rails):

YAML::load(params[:yamlrpc])[:foo]

or in PHP:

$data = syck_load($_POST['yamlrpc']);
$data['foo']

See examples in /examples directory

Defined Under Namespace

Classes: Client

Constant Summary collapse

VERSION =
'1.0.4'

Class Method Summary collapse

Class Method Details

.decode(params, fields = nil) ⇒ Object

Helper method to decode passed arguments on the server side.

Please note this is trivial task and you don’t have to use this helper function to decode POST data on the server. You can even use different language to parse YAML, like PHP.

  • params Hash

  • fields If nil, will decode all passed params; otherwise only passed list of keys



13
14
15
16
17
18
19
# File 'lib/yamlrpc/helpers.rb', line 13

def decode(params, fields = nil)
  r = {}
  fields = params.keys unless fields
  fields = fields.is_a?(Array) ? fields : [ fields ]
  fields.each { |k| r[k] = YAML::load(params[k] || "--- \n" ) }
  r
end

.decode_to_ary(params, fields) ⇒ Object

Helper method to decode arguments on the server side.

Returns a list of decoded values so you can use:

image, description, keywords = YamlRpc.decode_to_ary(params, [:image, :description, :keywords])

  • params Hash

  • fields Optional, single field name or array of field names, ie. [:foo, :bar]



29
30
31
32
33
34
# File 'lib/yamlrpc/helpers.rb', line 29

def decode_to_ary(params, fields)
  r = nil
  fields = fields.is_a?(Array) ? fields : [ fields ]
  r = fields.map { |k| YAML::load(params[k] || "--- \n") }
  r.size > 1 ? r : r.first
end