xmlize 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +0 -0
- data/History.txt +4 -0
- data/README.txt +58 -0
- data/lib/xmlize.rb +8 -0
- data/lib/xmlize/array.rb +0 -0
- data/lib/xmlize/hash.rb +22 -0
- data/lib/xmlize/object.rb +66 -0
- metadata +71 -0
data/CHANGELOG
ADDED
File without changes
|
data/History.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
= YamlRpc -- advanced RPC client on YAML
|
2
|
+
|
3
|
+
Supports:
|
4
|
+
|
5
|
+
* Data types (String, Regexp, Arrays, Hashes etc.)
|
6
|
+
* Passing images, PDFs and other binary data so easy you don't even think about it
|
7
|
+
* HTTP redirects
|
8
|
+
* HTTP Authentication
|
9
|
+
|
10
|
+
Simple to use:
|
11
|
+
|
12
|
+
client = YamlRpc::Client.new('http://localhost:3000/api/basic')
|
13
|
+
|
14
|
+
client.add_foo_bar( :foo => 3, :bar => 5 ) # -> { :result => 8 }
|
15
|
+
client.add(7, 11) # -> 18
|
16
|
+
client.upload(:image => open('image.gif', 'rb').read) # -> uploads image
|
17
|
+
|
18
|
+
Example add_foo_bar POSTs:
|
19
|
+
|
20
|
+
http://localhost:3000/api/basic/add_foo_bar
|
21
|
+
|
22
|
+
passing POST data:
|
23
|
+
|
24
|
+
yamlrpc=---%20%0A:foo:%203%0A:bar:%205%0A
|
25
|
+
|
26
|
+
which can be captured in any language like PHP, Java or Ruby, ie. Ruby on Rails:
|
27
|
+
|
28
|
+
class Api::BasicController < ApplicationController
|
29
|
+
def add_foo_bar
|
30
|
+
@yamlrpc = YAML.load(params[:yamlrpc])
|
31
|
+
render :text => (@yamlrpc[:foo] + @yamlrpc[:bar]).to_yaml
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Result is returned as YAML as well and automatically decoded.
|
36
|
+
|
37
|
+
See /examples dir for more examples.
|
38
|
+
|
39
|
+
== Download
|
40
|
+
|
41
|
+
The latest version:
|
42
|
+
|
43
|
+
* http://svn.mirekrusin.com/ruby/yamlrpc/trunk
|
44
|
+
|
45
|
+
== Installation
|
46
|
+
|
47
|
+
gem install yamlrpc
|
48
|
+
|
49
|
+
== License
|
50
|
+
|
51
|
+
This library is released under the LGPL license.
|
52
|
+
|
53
|
+
== Support
|
54
|
+
|
55
|
+
The homepage is located under http://www.rubyforge/projects/yamlrpc
|
56
|
+
|
57
|
+
For other information, feel free to ask on the ruby-talk mailing list
|
58
|
+
(which is mirrored to comp.lang.ruby) or contact mailto:ruby@mirekrusin.com
|
data/lib/xmlize.rb
ADDED
data/lib/xmlize/array.rb
ADDED
File without changes
|
data/lib/xmlize/hash.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
class Hash
|
3
|
+
|
4
|
+
def tokens_for_xmlize(tag, attributes = {})
|
5
|
+
attr_tokens = []
|
6
|
+
other_tokens = []
|
7
|
+
tag ||= 'object'
|
8
|
+
(self.merge(attributes)).each do |k, v|
|
9
|
+
if (k == :tag)
|
10
|
+
tag = v
|
11
|
+
elsif (k == :innerHTML)
|
12
|
+
other_tokens.push(['TEXT', v])
|
13
|
+
elsif (k.to_s[0..0] == '@')
|
14
|
+
attr_tokens.push(['ATTR', k.to_s[1..-1], v])
|
15
|
+
else
|
16
|
+
other_tokens = other_tokens.concat(v.tokens_for_xmlize(k))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
[['START', tag]] + attr_tokens + other_tokens + [['END', tag]]
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
|
2
|
+
class Object
|
3
|
+
|
4
|
+
def xmlize(tag, attributes = {}, options = {})
|
5
|
+
options[:add_xml_decl] = options.has_key?(:add_xml_decl) ? true : options[:add_xml_decl]
|
6
|
+
options[:version] = options.has_key?(:version) ? options[:version] : '1.0'
|
7
|
+
options[:encoding] = options.has_key?(:encoding) ? options[:encoding] : 'utf-8'
|
8
|
+
options[:standalone] = options.has_key?(:standalone) ? options[:standalone] : nil
|
9
|
+
options[:rails] = options.has_key?(:rails) ? options[:rails] : false
|
10
|
+
chunks = []
|
11
|
+
state = 'STARTDOC'
|
12
|
+
tokens_for_xmlize(tag, attributes).each do |token|
|
13
|
+
case token.first
|
14
|
+
when 'START'
|
15
|
+
if state == 'STARTDOC'
|
16
|
+
if (options[:add_xml_decl])
|
17
|
+
chunks.push("<?xml version=\"#{options[:version]}\" encoding=\"#{options[:encoding]}\"?>")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
chunks.push(">") if state == 'IN_START'
|
21
|
+
name = token[1]
|
22
|
+
name = name.gsub(/_/, '-') if options[:rails]
|
23
|
+
chunks.push "<#{name}"
|
24
|
+
state = 'IN_START'
|
25
|
+
|
26
|
+
when 'ATTR'
|
27
|
+
value = token[2].gsub(/\"/, '"')
|
28
|
+
chunks.push " #{token[1]}=\"#{value}\""
|
29
|
+
state = 'IN_START'
|
30
|
+
|
31
|
+
when 'TEXT'
|
32
|
+
chunks.push(">") if state == 'IN_START'
|
33
|
+
text = token[1]
|
34
|
+
[
|
35
|
+
[/&/, '&'],
|
36
|
+
[/\"/, """],
|
37
|
+
[/>/, ">"],
|
38
|
+
[/</, "<"]
|
39
|
+
].each { |e| text.gsub! e[0], e[1] }
|
40
|
+
chunks.push(text)
|
41
|
+
state = 'IN_TEXT'
|
42
|
+
|
43
|
+
when 'END'
|
44
|
+
if (state == 'IN_START')
|
45
|
+
chunks.push(' />')
|
46
|
+
else
|
47
|
+
name = token[1]
|
48
|
+
name = name.gsub /_/, '-' if (options[:rails])
|
49
|
+
chunks.push("</#{name}>")
|
50
|
+
end
|
51
|
+
state = 'IN_END'
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
chunks.join('')
|
56
|
+
end
|
57
|
+
|
58
|
+
def tokens_for_xmlize(tag, attributes = {})
|
59
|
+
tokens = [['START', tag]]
|
60
|
+
tokens.concat attributes.map { |k, v| ['ATTR', k, v] }
|
61
|
+
tokens << ['TEXT', to_s]
|
62
|
+
tokens << ['END', tag]
|
63
|
+
tokens
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xmlize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mirek Rusin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-10-17 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.7.0
|
24
|
+
version:
|
25
|
+
description: Convert Ruby objects to XML
|
26
|
+
email: ruby@mirekrusin.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- History.txt
|
33
|
+
- README.txt
|
34
|
+
files:
|
35
|
+
- lib/xmlize
|
36
|
+
- lib/xmlize.rb
|
37
|
+
- lib/xmlize/array.rb
|
38
|
+
- lib/xmlize/hash.rb
|
39
|
+
- lib/xmlize/object.rb
|
40
|
+
- CHANGELOG
|
41
|
+
- History.txt
|
42
|
+
- README.txt
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://rubyforge.com/projects/xmlize
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: xmlize
|
66
|
+
rubygems_version: 1.2.0
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: Convert Ruby objects to XML
|
70
|
+
test_files: []
|
71
|
+
|