xml-to-hash 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 31d59b40fb3dc7a1871e7c57e920ada8ddb17409
4
+ data.tar.gz: 8ac9f306b1583f3337df70137d45b116a7f55cad
5
+ SHA512:
6
+ metadata.gz: 1a34928339763a712c247ac5a4422d35145ad3bb19b0c2b1f40aa21b16255db10b5983831a12304948d94004b273fd68019d8900231fc686982f13b3fc564020
7
+ data.tar.gz: 65f3a4203ff7d1401b517a27b8a69bdaf69f07b756a82d2eb61991b3477ef0a75dea09b7919d389c27582610ee0bc47cc58173787a9c3fd4b8b297a28b630a01
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xml-to-hash.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Maarten Trompper
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,159 @@
1
+ # XML to Hash
2
+
3
+ Ruby gem to convert XML into Hash (and into JSON).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'xml-to-hash'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install xml-to-hash
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ include 'xml/to/hash'
25
+
26
+ xml_string = STR_XML = <<-EOS
27
+ <?xml version="1.0" encoding="UTF-8" ?>
28
+ <!DOCTYPE author [
29
+ <!ELEMENT author (#PCDATA)>
30
+ <!ENTITY MyParamEntity "Has been expanded">
31
+ <!ENTITY js "Me">
32
+ ]>
33
+ <myRoot>
34
+ some text
35
+ <!--
36
+ In comments we can use ]]>
37
+ <
38
+ &, ', and ", but %MyParamEntity; will not be expanded-->
39
+ <![CDATA[
40
+ Character Data block <!-- <, & ' " --> *and* %MyParamEntity;
41
+ ]]>
42
+ <?linebreak?>
43
+ <deeper xmlns="lol://some-namespace" how-deep="very-deep">randomtext
44
+ <even
45
+ lol:my-attr="just an attribute"
46
+ xmlns:lol=\'lol://my.name.space/\' deeper="true">O</even></deeper>
47
+ </myRoot>
48
+ EOS
49
+
50
+ xml = Nokogiri::XML STR_XML
51
+ hash = xml.to_hash
52
+
53
+ puts JSON.pretty_generate(hash)
54
+ ```
55
+
56
+ produces
57
+
58
+ ```json
59
+ {
60
+ "type": "element",
61
+ "name": "myRoot",
62
+ "children": [
63
+ {
64
+ "type": "text",
65
+ "content": "\n some text\n "
66
+ },
67
+ {
68
+ "type": "comment",
69
+ "content": "\n In comments we can use ]]>\n <\n &, ', and \", but %MyParamEntity; will not be expanded"
70
+ },
71
+ {
72
+ "type": "text",
73
+ "content": "\n "
74
+ },
75
+ {
76
+ "type": "cdata",
77
+ "content": "\n Character Data block <!-- <, & ' \" --> *and* %MyParamEntity; \n "
78
+ },
79
+ {
80
+ "type": "text",
81
+ "content": "\n "
82
+ },
83
+ {
84
+ "type": "pi",
85
+ "name": "linebreak"
86
+ },
87
+ {
88
+ "type": "text",
89
+ "content": "\n "
90
+ },
91
+ {
92
+ "type": "element",
93
+ "attrs": [
94
+ {
95
+ "name": "how-deep",
96
+ "value": "very-deep"
97
+ }
98
+ ],
99
+ "name": "deeper",
100
+ "namespace": {
101
+ "href": "lol://some-namespace"
102
+ },
103
+ "children": [
104
+ {
105
+ "type": "text",
106
+ "content": "randomtext\n "
107
+ },
108
+ {
109
+ "type": "element",
110
+ "attrs": [
111
+ {
112
+ "name": "my-attr",
113
+ "value": "just an attribute",
114
+ "namespace": {
115
+ "href": "lol://my.name.space/",
116
+ "prefix": "lol"
117
+ }
118
+ },
119
+ {
120
+ "name": "deeper",
121
+ "value": "true"
122
+ }
123
+ ],
124
+ "name": "even",
125
+ "namespace": {
126
+ "href": "lol://some-namespace"
127
+ },
128
+ "children": [
129
+ {
130
+ "type": "text",
131
+ "content": "O"
132
+ }
133
+ ]
134
+ }
135
+ ]
136
+ },
137
+ {
138
+ "type": "text",
139
+ "content": " \n"
140
+ }
141
+ ]
142
+ }
143
+ ```
144
+
145
+ ## Development
146
+
147
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
148
+
149
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
150
+
151
+ ## Contributing
152
+
153
+ Bug reports and pull requests are welcome on GitHub at https://github.com/digitalheir/xml-to-hash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
154
+
155
+
156
+ ## License
157
+
158
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
159
+
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "xml/to/hash"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,150 @@
1
+ require 'xml/to/hash/version'
2
+ require 'nokogiri'
3
+
4
+ module Xml
5
+ module To
6
+ module Hash
7
+ end
8
+ end
9
+ end
10
+ module Nokogiri
11
+ module XML
12
+ class Node
13
+
14
+ # Returns node type as symbol instead of integer
15
+ def self.get_type int
16
+ case int
17
+ # Element node type, see Nokogiri::XML::Node#element?
18
+ when Nokogiri::XML::Node::ELEMENT_NODE
19
+ return :element
20
+ # Attribute node type
21
+ when Nokogiri::XML::Node::ATTRIBUTE_NODE
22
+ return :attribute
23
+ # Text node type, see Nokogiri::XML::Node#text?
24
+ when Nokogiri::XML::Node::TEXT_NODE
25
+ return :text
26
+ # CDATA node type, see Nokogiri::XML::Node#cdata?
27
+ when Nokogiri::XML::Node::CDATA_SECTION_NODE
28
+ return :cdata
29
+ # Entity reference node type
30
+ when Nokogiri::XML::Node::ENTITY_REF_NODE
31
+ return :entity_ref
32
+ # Entity node type
33
+ when Nokogiri::XML::Node::ENTITY_NODE
34
+ return :entity
35
+ # PI node type
36
+ when Nokogiri::XML::Node::PI_NODE
37
+ return :pi
38
+ # Comment node type, see Nokogiri::XML::Node#comment?
39
+ when Nokogiri::XML::Node::COMMENT_NODE
40
+ return :comment
41
+ # Document node type, see Nokogiri::XML::Node#xml?
42
+ when Nokogiri::XML::Node::DOCUMENT_NODE
43
+ return :document
44
+ # Document type node type
45
+ when Nokogiri::XML::Node::DOCUMENT_TYPE_NODE
46
+ return :document_type
47
+ # Document fragment node type
48
+ when Nokogiri::XML::Node::DOCUMENT_FRAG_NODE
49
+ return :document_fraf
50
+ # Notation node type
51
+ when Nokogiri::XML::Node::NOTATION_NODE
52
+ return :notation
53
+ # HTML document node type, see Nokogiri::XML::Node#html?
54
+ when Nokogiri::XML::Node::HTML_DOCUMENT_NODE
55
+ return :html_doc
56
+ # DTD node type
57
+ when Nokogiri::XML::Node::DTD_NODE
58
+ return :dtd
59
+ # Element declaration type
60
+ when Nokogiri::XML::Node::ELEMENT_DECL
61
+ return :element_declaration
62
+ # Attribute declaration type
63
+ when Nokogiri::XML::Node::ATTRIBUTE_DECL
64
+ return :attribute_declaration
65
+ # Entity declaration type
66
+ when Nokogiri::XML::Node::ENTITY_DECL
67
+ return :entity_declaration
68
+ # Namespace declaration type
69
+ when Nokogiri::XML::Node::NAMESPACE_DECL
70
+ return :namespace_declaration
71
+ # XInclude start type
72
+ when Nokogiri::XML::Node::XINCLUDE_START
73
+ return :xinclude_start
74
+ # XInclude end type
75
+ when Nokogiri::XML::Node::XINCLUDE_END
76
+ return :xinclude_end
77
+ # DOCB document node type
78
+ when Nokogiri::XML::Node::DOCB_DOCUMENT_NODE
79
+ return :docb_document
80
+ else
81
+ return int
82
+ end
83
+ end
84
+
85
+ # Serialize this Node to a hash
86
+ #
87
+ # Example:
88
+ # >> Nokogiri::XML '<xml>hello</xml>'
89
+ # => {:type=>:element, :name=>"xml", :children=>[{:type=>:text, :content=>"hello"}]}
90
+ def to_hash
91
+ Node.obj_for_node root
92
+ end
93
+
94
+ private
95
+ # Given a Nokigiri XML node, create a Ruby hash
96
+ def self.obj_for_node node
97
+ ret = {
98
+ type: get_type(node.node_type),
99
+ }
100
+ if node.attributes and node.attributes.length > 0
101
+ ret[:attrs] = []
102
+ node.attributes
103
+ node.attributes.each do |key|
104
+ attr = key[1]
105
+ attr_o = {
106
+ name: key[0],
107
+ value: attr.content
108
+ }
109
+ if attr.namespace
110
+ attr_o[:namespace] = namespace_hash(attr.namespace)
111
+ end
112
+ ret[:attrs] << attr_o
113
+ end
114
+ end
115
+ # Treat elements a little bit differently
116
+ case node.node_type
117
+ when Nokogiri::XML::Node::ELEMENT_NODE, Nokogiri::XML::Node::PI_NODE
118
+ ret[:name] = node.name
119
+ else
120
+ ret[:content]= node.content
121
+ end
122
+ if node.namespace
123
+ ret[:namespace] = namespace_hash(node.namespace)
124
+ end
125
+
126
+ # Recurse into children
127
+ if node.children and node.children.length > 0
128
+ unless node.element?
129
+ puts "W-What? Node had children, but was not an element (but a #{get_type node.node_type})"
130
+ end
131
+ ret[:children]=[]
132
+ node.children.each do |child|
133
+ ret[:children] << obj_for_node(child)
134
+ end
135
+ end
136
+ ret
137
+ end
138
+
139
+ def self.namespace_hash(namespace)
140
+ hash = {
141
+ href: namespace.href
142
+ }
143
+ if namespace.prefix
144
+ hash[:prefix] = namespace.prefix
145
+ end
146
+ hash
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,7 @@
1
+ module Xml
2
+ module To
3
+ module Hash
4
+ VERSION = '0.9.0'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xml/to/hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'xml-to-hash'
8
+ spec.version = Xml::To::Hash::VERSION
9
+ spec.authors = ['Maarten Trompper']
10
+ spec.email = ['m.f.a.trompper@uva.nl']
11
+
12
+ spec.summary = %q{Transparently convert XML documents to Ruby hashes (...and JSON, and beyond)}
13
+ spec.description = %q{This gem add a to_hash method to Nokogiri XML nodes into a Ruby hash. We generate a hash,
14
+ in which all keys are constants.
15
+ NOTE: This gem ignores fancy stuff like doctypes and entity declarations.}
16
+ spec.homepage = 'https://github.com/digitalheir/ruby-xml-to-hash'
17
+ spec.license = 'MIT'
18
+
19
+ # # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
20
+ # # delete this section to allow pushing this gem to any host.
21
+ # if spec.respond_to?(:metadata)
22
+ # spec.metadata['allowed_push_host'] = "http://mygemserver.com"
23
+ # else
24
+ # raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
25
+ # end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.10'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec', '~> 2.4'
35
+
36
+ spec.add_runtime_dependency 'nokogiri', '~> 1.6.6.2', '>= 1.6.6.2'
37
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xml-to-hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Maarten Trompper
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.6.6.2
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.6.6.2
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: 1.6.6.2
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.6.6.2
75
+ description: "This gem add a to_hash method to Nokogiri XML nodes into a Ruby hash.
76
+ We generate a hash, \nin which all keys are constants. \nNOTE: This gem ignores
77
+ fancy stuff like doctypes and entity declarations."
78
+ email:
79
+ - m.f.a.trompper@uva.nl
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - ".gitignore"
85
+ - ".rspec"
86
+ - ".travis.yml"
87
+ - CODE_OF_CONDUCT.md
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - bin/console
93
+ - bin/setup
94
+ - lib/xml/to/hash.rb
95
+ - lib/xml/to/hash/version.rb
96
+ - xml-to-hash.gemspec
97
+ homepage: https://github.com/digitalheir/ruby-xml-to-hash
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.7
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Transparently convert XML documents to Ruby hashes (...and JSON, and beyond)
121
+ test_files: []