badgerfish 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 949fb19dd5603d3ff35d87cc36fab906297e6e4f
4
+ data.tar.gz: 0964dae1408eeac8e2855bc8582f797a07dc1927
5
+ SHA512:
6
+ metadata.gz: 843ebb8e5fe3a655f685276d54219ab62a59f1890457854818013fa3ba8f1a2377f13d602a2d1c19104b4401ba0e097e4e6ce215562133ad1d6bfbde10844b6d
7
+ data.tar.gz: f77052c028b60c635e5305d5b21097e823ad892c372201b216802ea1afa07235621ae9bf1f4ee660f30c6381226a606bce6affc285eb89735b103a563ac080fa
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in badgerfish.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Michael Sievers
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Badgerfish [![Build Status](https://travis-ci.org/msievers/badgerfish.png)](https://travis-ci.org/msievers/badgerfish) [![Dependency Status](https://gemnasium.com/msievers/badgerfish.png)](https://gemnasium.com/msievers/badgerfish)
2
+
3
+ BadgerFish is a convention for translating an XML document into a JSON object.
4
+
5
+ The rules
6
+
7
+ * element names become object properties
8
+ * text content of elements goes in the $ property of an object
9
+ * nested elements become nested properties
10
+ * multiple elements at the same level become array elements
11
+ * attributes go in properties whose names begin with @
12
+ * active namespaces for an element go in the element's @xmlns property
13
+ * the default namespace URI goes in @xmlns.$
14
+ * other namespaces go in other properties of @xmlns
15
+ * elements with namespace prefixes become object properties, too
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ gem 'badgerfish'
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install badgerfish
30
+
31
+ ## Usage
32
+
33
+ ```ruby
34
+ require 'badgerfish'
35
+
36
+ parser = Badgerfish::Parser.new
37
+ xml = '<alice charlie="david">bob</alice>'
38
+ badgerfish_hash = parser.load(xml)
39
+ ```
40
+
41
+ ## Missing features
42
+
43
+ Does not handle child node namespaces as suggested by http://badgerfish.ning.com (case 9).
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
52
+
53
+ ## Links
54
+
55
+ * http://badgerfish.ning.com
56
+ * http://wiki.open311.org/index.php?title=JSON_and_XML_Conversion#The_Badgerfish_Convention
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = 'test/test_*.rb'
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'badgerfish/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'badgerfish'
8
+ spec.version = Badgerfish::VERSION
9
+ spec.authors = ['Michael Sievers']
10
+ spec.email = ['michael_sievers@web.de']
11
+ spec.description = %q{Badgerfish xml to json convention for ruby}
12
+ spec.summary = %q{Badgerfish xml to json convention for ruby}
13
+ spec.homepage = "https://github.com/msievers/badgerfish"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'ox', '~> 2.0.8'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'minitest', '~> 5.0.6'
25
+ spec.add_development_dependency 'multi_json', '~> 1.7.9'
26
+ spec.add_development_dependency 'oj', '~> 2.1.4'
27
+ spec.add_development_dependency 'pry', '~> 0.9.12.2'
28
+ spec.add_development_dependency 'pry-nav', '~> 0.2.3'
29
+ spec.add_development_dependency 'rake'
30
+ end
data/lib/badgerfish.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'badgerfish/from_xml'
2
+ require 'badgerfish/version'
3
+
4
+ module Badgerfish
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'badgerfish/parser'
2
+
3
+ module Badgerfish
4
+ end
@@ -0,0 +1,54 @@
1
+ require 'ox'
2
+ require 'stringio'
3
+
4
+ module Badgerfish
5
+ class OxSaxParser < Ox::Sax
6
+
7
+ def load(xml)
8
+ @result = @root = {}
9
+ @parents = []
10
+ Ox.sax_parse(self, StringIO.new(xml))
11
+ @result
12
+ end
13
+
14
+ #
15
+ # sax callbacks
16
+ #
17
+ def start_element(name)
18
+ new_element = {}
19
+
20
+ if @root[name].nil?
21
+ @root[name] = new_element
22
+ else
23
+ @root[name] = [@root[name]] unless @root[name].is_a?(Array)
24
+ @root[name].push new_element
25
+ end
26
+
27
+ @parents.push @root
28
+ @root = new_element
29
+ end
30
+
31
+ def end_element(name)
32
+ @root = @parents.pop
33
+ end
34
+
35
+ def attr(name, value)
36
+ unless name.to_s.start_with? 'xmlns'
37
+ @root["@#{name}"] = value
38
+ else
39
+ @root['@xmlns'] ||= {}
40
+
41
+ if name.to_s.start_with? 'xmlns:'
42
+ @root['@xmlns'][name[6, name.length]] = value
43
+ else
44
+ @root['@xmlns']['$'] = value
45
+ end
46
+ end
47
+ end
48
+
49
+ def text(value)
50
+ @root['$'] = value
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,15 @@
1
+ require 'badgerfish/ox_sax_parser'
2
+
3
+ module Badgerfish
4
+ class Parser
5
+
6
+ def initialize()
7
+ @parser = Badgerfish::OxSaxParser.new
8
+ end
9
+
10
+ def load(xml)
11
+ @parser.load(xml)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Badgerfish
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,146 @@
1
+ require File.expand_path '../test_helper.rb', __FILE__
2
+
3
+ class TestBadgerfish < Minitest::Test
4
+ def setup
5
+ @parser = Badgerfish::Parser.new
6
+ end
7
+
8
+ def test_badgerfish_ning_com_nr_2
9
+ xml = '<alice>bob</alice>'
10
+ expected_json = <<-json
11
+ {
12
+ "alice":{
13
+ "$":"bob"
14
+ }
15
+ }
16
+ json
17
+
18
+ assert_badgerfish xml, expected_json
19
+ end
20
+
21
+ def test_badgerfish_ning_com_nr_3
22
+ xml = '<alice><bob>charlie</bob><david>edgar</david></alice>'
23
+ expected_json = <<-json
24
+ {
25
+ "alice":{
26
+ "bob":{
27
+ "$":"charlie"
28
+ },
29
+ "david":{
30
+ "$":"edgar"
31
+ }
32
+ }
33
+ }
34
+ json
35
+
36
+ assert_badgerfish xml, expected_json
37
+ end
38
+
39
+ def test_badgerfish_ning_com_nr_4
40
+ xml = '<alice><bob>charlie</bob><bob>david</bob></alice>'
41
+ expected_json = <<-json
42
+ {
43
+ "alice":{
44
+ "bob":[
45
+ {
46
+ "$":"charlie"
47
+ },
48
+ {
49
+ "$":"david"
50
+ }
51
+ ]
52
+ }
53
+ }
54
+ json
55
+
56
+ assert_badgerfish xml, expected_json
57
+ end
58
+
59
+ def test_badgerfish_ning_com_nr_5
60
+ xml = '<alice charlie="david">bob</alice>'
61
+ expected_json = <<-json
62
+ {
63
+ "alice":{
64
+ "@charlie":"david",
65
+ "$":"bob"
66
+ }
67
+ }
68
+ json
69
+
70
+ assert_badgerfish xml, expected_json
71
+ end
72
+
73
+ def test_badgerfish_ning_com_nr_7
74
+ xml = '<alice xmlns="http://some-namespace">bob</alice>'
75
+ expected_json = <<-json
76
+ {
77
+ "alice":{
78
+ "@xmlns":{
79
+ "$":"http://some-namespace"
80
+ },
81
+ "$":"bob"
82
+ }
83
+ }
84
+ json
85
+
86
+ assert_badgerfish xml, expected_json
87
+ end
88
+
89
+ def test_badgerfish_ning_com_nr_8
90
+ xml = <<-xml
91
+ <alice xmlns="http://some-namespace" xmlns:charlie="http://some-other-namespace">bob</alice>
92
+ xml
93
+ expected_json = <<-json
94
+ {
95
+ "alice":{
96
+ "@xmlns":{
97
+ "$":"http://some-namespace",
98
+ "charlie":"http://some-other-namespace"
99
+ },
100
+ "$":"bob"
101
+ }
102
+ }
103
+ json
104
+
105
+ assert_badgerfish xml, expected_json
106
+ end
107
+
108
+ def test_badgerfish_ning_com_nr_9
109
+ skip("child node namespaces not supported yet")
110
+
111
+ xml = <<-xml
112
+ <alice xmlns="http://some-namespace" xmlns:charlie="http://some-other-namespace">
113
+ <bob>david</bob>
114
+ <charlie:edgar>frank</charlie:edgar>
115
+ </alice>
116
+ xml
117
+
118
+ expected_json = <<-json
119
+ {
120
+ "alice":{
121
+ "@xmlns":{
122
+ "$":"http://some-namespace",
123
+ "charlie":"http://some-other-namespace"
124
+ },
125
+ "bob":{
126
+ "@xmlns":{
127
+ "$":"http://some-namespace",
128
+ "charlie":"http://some-other-namespace"
129
+ },
130
+ "$":"david"
131
+ },
132
+ "charlie:edgar":{
133
+ "$":"frank",
134
+ "@xmlns":{
135
+ "$":"http://some-namespace",
136
+ "charlie":"http://some-other-namespace"
137
+ }
138
+ }
139
+ }
140
+ }
141
+ json
142
+
143
+ assert_badgerfish xml, expected_json
144
+ end
145
+
146
+ end
@@ -0,0 +1,14 @@
1
+ # silence that minitest related error about minitest/autorun (since minitest version >= 5)
2
+ gem 'minitest'
3
+
4
+ require 'badgerfish'
5
+ require 'minitest/autorun'
6
+ require 'multi_json'
7
+ require 'pry'
8
+
9
+ def assert_badgerfish(xml, json)
10
+ json = MultiJson.dump(MultiJson.load(json)) # sanitize json
11
+ badgerfish_json = MultiJson.dump(@parser.load(xml))
12
+
13
+ assert_equal(json, badgerfish_json)
14
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: badgerfish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Sievers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ox
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 5.0.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 5.0.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: multi_json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.7.9
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.7.9
69
+ - !ruby/object:Gem::Dependency
70
+ name: oj
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.1.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.1.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.9.12.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 0.9.12.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-nav
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: 0.2.3
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 0.2.3
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Badgerfish xml to json convention for ruby
126
+ email:
127
+ - michael_sievers@web.de
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .travis.yml
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - badgerfish.gemspec
139
+ - lib/badgerfish.rb
140
+ - lib/badgerfish/from_xml.rb
141
+ - lib/badgerfish/ox_sax_parser.rb
142
+ - lib/badgerfish/parser.rb
143
+ - lib/badgerfish/version.rb
144
+ - test/test_badgerfish.rb
145
+ - test/test_helper.rb
146
+ homepage: https://github.com/msievers/badgerfish
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.0.6
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Badgerfish xml to json convention for ruby
170
+ test_files:
171
+ - test/test_badgerfish.rb
172
+ - test/test_helper.rb