cobravsmongoose 0.0.1 → 0.0.2

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.
data/CHANGES CHANGED
@@ -1,2 +1,6 @@
1
+ == 0.0.2 (2006-06-28)
2
+ * Added direct XML <-> JSON conversion
3
+ * Can accept REXML::Document or anything that responds to to_s for XML
4
+
1
5
  == 0.0.1 (2006-04-16)
2
6
  * Initial release.
@@ -4,6 +4,7 @@ require 'cgi'
4
4
  #
5
5
  # CobraVsMongoose translates between XML documents and Ruby hashes according to the
6
6
  # rules of the BadgerFish convention (see http://badgerfish.ning.com/).
7
+ # It can also convert directly between XML and JSON using the JSON library.
7
8
  #
8
9
  class CobraVsMongoose
9
10
 
@@ -13,7 +14,9 @@ class CobraVsMongoose
13
14
  class << self
14
15
 
15
16
  #
16
- # Returns a Hash corresponding to the data structure of the given XML string.
17
+ # Returns a Hash corresponding to the data structure of the given XML,
18
+ # which should be a REXML::Document or anything that responds to to_s
19
+ # with a string of valid XML.
17
20
  #
18
21
  # E.g.
19
22
  # xml = '<alice><bob>charlie</bob><bob>david</bob></alice>'
@@ -21,7 +24,11 @@ class CobraVsMongoose
21
24
  # # => { "alice" => { "bob" => [{ "$" => "charlie" }, { "$" => "david" }] } }
22
25
  #
23
26
  def xml_to_hash(xml)
24
- doc = REXML::Document.new(xml)
27
+ if xml.respond_to?(:root_node)
28
+ doc = xml
29
+ else
30
+ doc = REXML::Document.new(xml.to_s)
31
+ end
25
32
  return xml_node_to_hash(doc.root_node)
26
33
  end
27
34
 
@@ -41,6 +48,23 @@ class CobraVsMongoose
41
48
  return nested_data_to_xml(hash.keys.first, hash.values.first)
42
49
  end
43
50
 
51
+ #
52
+ # Returns a JSON string corresponding to the given XML, the constraints
53
+ # for which are as for xml_to_hash.
54
+ #
55
+ def xml_to_json(xml)
56
+ require 'json'
57
+ return xml_to_hash(xml).to_json
58
+ end
59
+
60
+ #
61
+ # Returns an XML string corresponding to the given JSON string.
62
+ #
63
+ def json_to_xml(json)
64
+ require 'json'
65
+ return hash_to_xml(JSON.parse(json))
66
+ end
67
+
44
68
  #
45
69
  # The sort_keys class attribute is useful for testing, when a predictable order
46
70
  # is required in the generated XML. By setting CobraVsMongoose.sort_keys to true,
@@ -3,8 +3,8 @@ require 'cobravsmongoose'
3
3
  require 'test/unit'
4
4
  begin
5
5
  require 'test/unit/xml'
6
- rescue
7
- puts 'You need to install Test::Unit::XML (http://testunitxml.rubyforge.org/) for this test.'
6
+ rescue LoadError
7
+ $stderr.puts 'You need to install Test::Unit::XML (http://testunitxml.rubyforge.org/) for this test.'
8
8
  end
9
9
  require File.dirname(__FILE__) + '/test_data'
10
10
 
@@ -0,0 +1,29 @@
1
+ $: << File.dirname(__FILE__) + '/../lib/'
2
+ require 'cobravsmongoose'
3
+ require 'test/unit'
4
+ begin
5
+ require 'json'
6
+ rescue LoadError
7
+ $stderr.puts 'You need to install the JSON library for Ruby (http://json.rubyforge.org/)'
8
+ $stderr.puts 'to use and test the XML<->JSON features of Cobra vs Mongoose.'
9
+ end
10
+
11
+ class HashToXMLTest < Test::Unit::TestCase
12
+
13
+ def test_xml_to_json
14
+ xml = '<alice><bob>charlie</bob><bob>david</bob></alice>'
15
+ assert_equal(
16
+ '{"alice":{"bob":[{"$":"charlie"},{"$":"david"}]}}',
17
+ CobraVsMongoose.xml_to_json(xml)
18
+ )
19
+ end
20
+
21
+ def test_json_to_xml
22
+ json = '{"alice":{"$":"bob","@charlie":"david"}}'
23
+ assert_equal(
24
+ "<alice charlie='david'>bob</alice>",
25
+ CobraVsMongoose.json_to_xml(json)
26
+ )
27
+ end
28
+
29
+ end
@@ -16,6 +16,12 @@ class XMLToHashTest < Test::Unit::TestCase
16
16
  assert !COBRA_VS_MONGOOSE_TEST_DATA.empty?
17
17
  assert methods.grep(/^test_/).length > COBRA_VS_MONGOOSE_TEST_DATA.keys.length
18
18
  end
19
+
20
+ def test_should_accept_a_rexml_document_source
21
+ expected, source = COBRA_VS_MONGOOSE_TEST_DATA[:nested_elements]
22
+ rexml_doc = REXML::Document.new(source)
23
+ assert_equal(expected, CobraVsMongoose.xml_to_hash(rexml_doc))
24
+ end
19
25
 
20
26
  def assert_xml_to_hash(name)
21
27
  expected, source = COBRA_VS_MONGOOSE_TEST_DATA[name]
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: cobravsmongoose
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.1
7
- date: 2006-04-16 00:00:00 +01:00
6
+ version: 0.0.2
7
+ date: 2006-06-28 00:00:00 +01:00
8
8
  summary: Translates XML to and from Ruby Hash objects, following the BadgerFish convention.
9
9
  require_paths:
10
10
  - lib
@@ -31,6 +31,7 @@ files:
31
31
  - lib/cobravsmongoose.rb
32
32
  - test/all.rb
33
33
  - test/hash_to_xml_test.rb
34
+ - test/json_test.rb
34
35
  - test/test_data.rb
35
36
  - test/xml_to_hash_test.rb
36
37
  - README