smacks-apricoteatsgorilla 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
File without changes
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,61 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'hpricot'
3
+
4
+ # ApricotEatsGorilla converts SOAP response messages to Ruby hashes.
5
+ # It's based on CobraVsMongoose but uses Hpricot instead of REXML and it
6
+ # also doesn't follow the BadgerFish convention.
7
+ class ApricotEatsGorilla
8
+
9
+ # Converts a given SOAP response message into a Ruby Hash.
10
+ def self.soap_response_to_hash(xml, root_node = nil)
11
+ xml = prepare_xml(xml)
12
+ root_node ||= "//return"
13
+ doc = Hpricot.XML(xml)
14
+ xml_node_to_hash doc.at(root_node)
15
+ end
16
+
17
+ private
18
+
19
+ # Converts XML nodes into a Ruby Hash. Recursively calls itself to grab
20
+ # nested XML nodes and values.
21
+ def self.xml_node_to_hash(node)
22
+ this_node = {}
23
+
24
+ node.each_child do |child|
25
+ if child.children.nil?
26
+ key, value = child.name, nil
27
+ elsif child.children.size == 1 && child.children.first.text?
28
+ key, value = child.name, string_to_bool?(child.children.first.raw_string)
29
+ else
30
+ key, value = child.name, xml_node_to_hash(child)
31
+ end
32
+
33
+ current = this_node[key]
34
+ case current
35
+ when Array:
36
+ this_node[key] << value
37
+ when nil
38
+ this_node[key] = value
39
+ else
40
+ this_node[key] = [current.dup, value]
41
+ end
42
+ end
43
+
44
+ this_node
45
+ end
46
+
47
+ # Helper to remove line breaks and whitespace between XML nodes.
48
+ def self.prepare_xml(xml)
49
+ xml = xml.gsub(/\n+/, "")
50
+ xml = xml.gsub(/(>)\s*(<)/, '\1\2')
51
+ end
52
+
53
+ # Helper to convert "true" and "false" strings to boolean values.
54
+ # Returns the original string in case it doesn't match "true" or "false".
55
+ def self.string_to_bool?(string)
56
+ return true if string == "true"
57
+ return false if string == "false"
58
+ string
59
+ end
60
+
61
+ end
@@ -0,0 +1,67 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require File.join(File.dirname(__FILE__), "..", "lib", "apricoteatsgorilla")
5
+
6
+ class ApricotEatsGorillaTest < Test::Unit::TestCase
7
+
8
+ def test_soap_response_example
9
+ xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
10
+ <soap:Body>
11
+ <ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/">
12
+ <return>
13
+ <authValue>
14
+ <token>secret</token>
15
+ <client>example</client>
16
+ </authValue>
17
+ </return>
18
+ </ns2:authenticateResponse>
19
+ </soap:Body>
20
+ </soap:Envelope>'
21
+ expected = {'authValue' => {'token' => 'secret', 'client' => 'example'}}
22
+
23
+ result = ApricotEatsGorilla.soap_response_to_hash(xml)
24
+ assert_equal expected, result
25
+ end
26
+
27
+ def test_that_boolean_string_values_are_converted_to_actual_boolean_objects
28
+ xml = '<root><yes>true</yes><no>false</no><txt>something</txt></root>'
29
+ expected = {'yes' => true, 'no' => false, 'txt' => 'something'}
30
+
31
+ result = ApricotEatsGorilla.soap_response_to_hash(xml, '//root')
32
+ assert_equal expected, result
33
+ end
34
+
35
+ def test_that_empty_element_tags_are_converted_to_nil
36
+ xml = '<contact><name>Jungle Julia</name><email /><phone/></contact>'
37
+ expected = {'name' => 'Jungle Julia', 'email' => nil, 'phone' => nil}
38
+
39
+ result = ApricotEatsGorilla.soap_response_to_hash(xml, '//contact')
40
+ assert_equal expected, result
41
+ end
42
+
43
+ def test_that_attributes_get_removed
44
+ xml = '<todo><paint subject="chair" color="#000000">black</paint></todo>'
45
+ expected = {'paint' => 'black'}
46
+
47
+ result = ApricotEatsGorilla.soap_response_to_hash(xml, '//todo')
48
+ assert_equal expected, result
49
+ end
50
+
51
+ def test_that_node_groups_with_text_values_get_converted_to_arrays
52
+ xml = '<root><items>first</items><items>second</items></root>'
53
+ expected = {'items' => ['first', 'second']}
54
+
55
+ result = ApricotEatsGorilla.soap_response_to_hash(xml, '//root')
56
+ assert_equal expected, result
57
+ end
58
+
59
+ def test_that_node_groups_with_nesting_get_converted_to_arrays_with_hashes
60
+ xml = '<root><items><name>first</name></items><items><name>second</name></items></root>'
61
+ expected = {'items' => [{'name' => 'first'}, {'name' => 'second'}]}
62
+
63
+ result = ApricotEatsGorilla.soap_response_to_hash(xml, '//root')
64
+ assert_equal expected, result
65
+ end
66
+
67
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smacks-apricoteatsgorilla
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Harrington
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.6.164
24
+ version:
25
+ description: Apricot eats Gorilla converts SOAP response messages to Ruby hashes
26
+ email:
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.markdown
33
+ files:
34
+ - README.markdown
35
+ - VERSION.yml
36
+ - lib/apricoteatsgorilla.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/smacks/apricoteatsgorilla
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --inline-source
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Apricot eats Gorilla converts SOAP response messages to Ruby hashes
64
+ test_files:
65
+ - tests/apricoteatsgorilla_test.rb