mini_xml 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/mini_xml.rb +81 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTdkODlkNDExMjQ4ZTMyNzg4M2IwZWZkOTAyMTZkMTJkZDQ0YzNkZQ==
5
+ data.tar.gz: !binary |-
6
+ YTFmZjBhMjU2YThhNGJmMmVkM2MyOWQyODg0OTlkYWJhYzdlYzU3ZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjExZDE5NzE2MTcxMWYxOTMxMzE3NzYwZDcwMTFlOWYwOGMwZjI5M2UyNzE4
10
+ YjMyZDljMTI0YjU2YjQ2NGYzOGNhMGExOWY1NmI2NWM0MDhhNGQ4ZGM1ZDc3
11
+ NDNiZjEzYzA3NjI0MGY1ZTA4OGI0OTY3YmEyYWU3ZTViMTE3NGY=
12
+ data.tar.gz: !binary |-
13
+ MWM5ZjMyYzEzYTc5NjRkNTQyZDUyNDY5M2IwZDg3Mjk5Zjg0NmVhNjk3OGU1
14
+ YzVjOGVkODhjYzU4OWFhMTYyNzhhNDU2NzM1NmU2NzUwODBjYzM4YjQwZWMz
15
+ ODFhZmM3ZWQ3NjI1OTcyODc0NzhlMzdhZDcxOGI1ZWRlMGRiYzY=
data/lib/mini_xml.rb ADDED
@@ -0,0 +1,81 @@
1
+ #encoding: utf-8
2
+ class MiniXml
3
+ def initialize(out)
4
+ @out = out
5
+ end
6
+
7
+ def self.generate(&block)
8
+ out = []
9
+ MiniXml.new(out).instance_eval(&block)
10
+ out.join("")
11
+ end
12
+
13
+ def self.xml_from_data(data = {}, fields = [])
14
+ out = []
15
+ xml_attributes = {}
16
+ xml_attributes[:version] = (data[:xml_version] || '1.0')
17
+ xml_attributes[:encoding] = (data[:encoding] || 'UTF-8')
18
+ rss_version = data[:rss_version] || '2.0'
19
+ title_str = data[:title] || 'RSS'
20
+ description_str = data[:description] || ''
21
+ link_str = data[:link] || ''
22
+ generate do
23
+ xml(xml_attributes)
24
+ rss(version: rss_version) do
25
+ channel do
26
+ title {title_str}
27
+ description {description_str}
28
+ link {link_str}
29
+ data[:data].each do |da|
30
+ item do
31
+ if da.is_a?(Hash)
32
+ da.each do |k, v|
33
+ next if fields.size > 0 && !fields.include?(k)
34
+ p = proc {v}
35
+ send(k, &p)
36
+ end
37
+ else
38
+ raise 'fields argument can\'t empty!' if fields.empty?
39
+ fields.each do |m|
40
+ p = proc {da.send(m)}
41
+ puts da.title
42
+ send(m.to_s, &p)
43
+ end
44
+ end
45
+ nil
46
+ end
47
+ end
48
+ nil
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ def comment(content)
55
+ @out << "<!-- #{content} -->"
56
+ nil
57
+ end
58
+
59
+ def xml(tagname, attributes = {version: '1.0', encoding: 'UTF-8'})
60
+ @out << "<?xml#{generate_attributes(attributes)}?>"
61
+ nil
62
+ end
63
+
64
+ def generate_attributes(attributes_hash = {})
65
+ attributes_str = ""
66
+ attributes_hash.each {|key, val| attributes_str << " #{key}=\"#{val}\" "}
67
+ attributes_str.rstrip
68
+ end
69
+
70
+ def method_missing(tagname, attributes = {})
71
+ @out << "<#{tagname}"
72
+ @out << generate_attributes(attributes)
73
+ @out << ">"
74
+ if block_given?
75
+ content = yield
76
+ @out << content.to_s if content
77
+ @out << "</#{tagname}>"
78
+ end
79
+ nil
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_xml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - fxhover
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 一个简单的生成xml的gem,可以用于RSS订阅!
14
+ email: 736698959@qq.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/mini_xml.rb
20
+ homepage: http://rubygems.org/gems/mini_xml
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.6
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: mini_xml
44
+ test_files: []
45
+ has_rdoc: