yamlrpc 0.1.080624130617 → 0.1.080630154212
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yamlrpc/client.rb +36 -7
- data/lib/yamlrpc/helpers.rb +35 -0
- data/lib/yamlrpc.rb +1 -0
- metadata +3 -2
data/lib/yamlrpc/client.rb
CHANGED
@@ -1,27 +1,43 @@
|
|
1
1
|
|
2
|
-
# Simple RPC client on YAML
|
2
|
+
# Simple RPC client on YAML
|
3
|
+
#
|
4
|
+
# Exacmple:
|
5
|
+
#
|
6
|
+
# yaml_rpc = YamlRpc.new('http://my.com/yaml_api/')
|
7
|
+
# yaml_rpc.invoice( :invoice_id => '123' )
|
8
|
+
#
|
9
|
+
# will POST { :invoice_id => YAML::dump('123') } value to http://my.com/yaml_api/invoice
|
10
|
+
#
|
11
|
+
# On the server side you can use YamlRpc.decode(params, [:invoice_id]) (in Ruby on Rails) to get decoded values.
|
3
12
|
module YamlRpc
|
4
13
|
|
5
14
|
class Client
|
6
15
|
|
7
16
|
attr_accessor :url
|
17
|
+
attr_accessor :form_data_root
|
8
18
|
|
9
|
-
|
19
|
+
# * <tt>url</tt> Root url, ie. "http://my.com/something/"; YamlRpc#some_method(:foo => 'bar') will POST to http://my.com/something/some_method with { :foo => 'bar' } params
|
20
|
+
# * <tt>options</tt>
|
21
|
+
# * <tt>:form_data_root</tt> Default to nil; if you want all params to have single root, ie. 'yaml', set :form_data_root => :yaml
|
22
|
+
def initialize(url, options = {})
|
10
23
|
@url = url
|
24
|
+
@form_data_root = options[:form_data_root]
|
11
25
|
end
|
12
26
|
|
27
|
+
# * <tt>method</tt> Method name
|
28
|
+
# * <tt>args</tt> Arguments, should always be a Hash
|
13
29
|
def method_missing(method, args)
|
14
30
|
post(method, args)
|
15
31
|
end
|
16
32
|
|
17
33
|
protected
|
18
34
|
|
19
|
-
# * <tt>
|
20
|
-
# * <tt>
|
21
|
-
def post(
|
22
|
-
url = URI.parse("#{@url}#{
|
35
|
+
# * <tt>add_url</tt> Path to be added to the YamlRpc#url (usually it's the method name), ie :invoice)
|
36
|
+
# * <tt>args</tt> Data to be passed as POST fields, ie: { :status => 'SUCCESS', :message => 'All ok' }
|
37
|
+
def post(add_url, args = {})
|
38
|
+
url = URI.parse("#{@url}#{add_url}")
|
23
39
|
req = Net::HTTP::Post.new(url.path)
|
24
|
-
req.set_form_data(
|
40
|
+
req.set_form_data(args_to_form_data(args), ';')
|
25
41
|
res = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
|
26
42
|
case res
|
27
43
|
when Net::HTTPSuccess
|
@@ -30,6 +46,19 @@ module YamlRpc
|
|
30
46
|
res.error!
|
31
47
|
end
|
32
48
|
end
|
49
|
+
|
50
|
+
# POST form data can be passed in two ways:
|
51
|
+
# 1) with common root (set YamlRpc#form_data_root to ie. :my_root)
|
52
|
+
# 2) without common root, each key is at the root of POST
|
53
|
+
def args_to_form_data(args)
|
54
|
+
r = {}
|
55
|
+
if @form_data_root
|
56
|
+
r[@form_data_root.to_sym] = YAML::dump(args)
|
57
|
+
else
|
58
|
+
args.each { |k, v| r[k.to_sym] = YAML::dump(v) }
|
59
|
+
end
|
60
|
+
r
|
61
|
+
end
|
33
62
|
|
34
63
|
end
|
35
64
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
module YamlRpc
|
3
|
+
|
4
|
+
class <<self
|
5
|
+
|
6
|
+
# Helper method to decode passed arguments on the server side.
|
7
|
+
#
|
8
|
+
# Please note this is trivial task and you don't have to use this helper function to decode POST data on the server.
|
9
|
+
# You can even use different language to parse YAML, like PHP.
|
10
|
+
#
|
11
|
+
# * <tt>params</tt> Hash
|
12
|
+
# * <tt>fields</tt> If nil, will decode all passed params; otherwise only passed list of keys
|
13
|
+
def decode(params, fields = nil)
|
14
|
+
r = {}
|
15
|
+
fields = params.keys unless fields
|
16
|
+
fields.each { |k| r[k] = YAML::load(params[k] || "--- \n" ) }
|
17
|
+
r
|
18
|
+
end
|
19
|
+
|
20
|
+
# Helper method to decode arguments on the server side.
|
21
|
+
#
|
22
|
+
# Returns a list of decoded values so you can use:
|
23
|
+
#
|
24
|
+
# image, description, keywords = YamlRpc.decode_to_ary(params, [:image, :description, :keywords])
|
25
|
+
#
|
26
|
+
# * <tt>params</tt> Hash
|
27
|
+
# * <tt>fields</tt> Optional, single field name or array of field names, ie. [:foo, :bar]
|
28
|
+
def decode_to_ary(params, fields)
|
29
|
+
fields = fields.is_a?(Array) ? fields : [ fields ]
|
30
|
+
fields.map { |k| YAML::load(params[k] || "--- \n") }
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/lib/yamlrpc.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yamlrpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.080630154212
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mirek Rusin
|
@@ -9,7 +9,7 @@ autorequire: yamlrpc
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-06-
|
12
|
+
date: 2008-06-30 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,6 +24,7 @@ extra_rdoc_files:
|
|
24
24
|
files:
|
25
25
|
- lib/yamlrpc
|
26
26
|
- lib/yamlrpc/client.rb
|
27
|
+
- lib/yamlrpc/helpers.rb
|
27
28
|
- lib/yamlrpc.rb
|
28
29
|
- README
|
29
30
|
has_rdoc: true
|