smacks-apricoteatsgorilla 0.3.61 → 0.3.91

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,6 +47,21 @@ class ApricotEatsGorilla
47
47
  # Flag to enable optional sorting of Hash keys.
48
48
  attr_accessor :sort_keys
49
49
 
50
+ # Flag to disable conversion of tag names to lowerCamelCase.
51
+ attr_accessor :disable_tag_names_to_lower_camel_case
52
+
53
+ # Flag to disable conversion of Hash keys to snake_case.
54
+ attr_accessor :disable_hash_keys_to_snake_case
55
+
56
+ # Flag to disable conversion of Hash keys to Symbols.
57
+ attr_accessor :disable_hash_keys_to_symbol
58
+
59
+ # Array of XML nodes to add a namespace to.
60
+ attr_accessor :nodes_to_namespace
61
+
62
+ # The namespace for nodes in :nodes_to_namespace.
63
+ attr_accessor :node_namespace
64
+
50
65
  # Shortcut method for translating between XML Strings and Ruby Hashes.
51
66
  # Delegates to xml_to_hash in case +source+ is of type String or delegates
52
67
  # to hash_to_xml in case +source+ is of type Hash. Returns nil otherwise.
@@ -66,6 +81,12 @@ class ApricotEatsGorilla
66
81
  end
67
82
  end
68
83
 
84
+ # Yields this class object in case a +block+ was given. Nice way for setting
85
+ # multiple options at once.
86
+ def setup
87
+ yield self if block_given?
88
+ end
89
+
69
90
  # Converts a given +xml+ String into a Ruby Hash. Starts parsing at root
70
91
  # node by default. The optional +root_node+ parameter can be used to specify
71
92
  # a custom root node to start parsing at via XPath (Hpricot search).
@@ -151,6 +172,18 @@ class ApricotEatsGorilla
151
172
  end
152
173
  end
153
174
 
175
+ # Converts a given +string+ from CamelCase/lowerCamelCase to snake_case.
176
+ def to_snake_case(string)
177
+ string = string.gsub(/[A-Z]+/, '\1_\0').downcase
178
+ string = string[1, string.length-1] if string[0, 1] == "_"
179
+ string
180
+ end
181
+
182
+ # Converts a given +string+ from snake_case to lowerCamelCase.
183
+ def to_lower_camel_case(string)
184
+ string.to_s.gsub(/_(.)/) { $1.upcase }
185
+ end
186
+
154
187
  private
155
188
 
156
189
  # Actual implementation for xml_to_hash. Takes and iterates through a given
@@ -171,7 +204,9 @@ class ApricotEatsGorilla
171
204
  key, value = child.name, xml_node_to_hash(child)
172
205
  end
173
206
 
174
- key = to_snake_case(remove_namespace(key)).intern
207
+ key = remove_namespace(key)
208
+ key = to_snake_case(key) unless disable_hash_keys_to_snake_case
209
+ key = key.intern unless disable_hash_keys_to_symbol
175
210
  current = this_node[key]
176
211
  case current
177
212
  when Array
@@ -194,16 +229,16 @@ class ApricotEatsGorilla
194
229
  # * +item+ - A Hash value to translate into an XML String.
195
230
  def nested_data_to_xml(name, item)
196
231
  case item
197
- when String
198
- tag(name) { item }
232
+ when String, Symbol
233
+ tag(name) { item.to_s }
199
234
  when Array
200
235
  item.map { |subitem| nested_data_to_xml(name, subitem) }.join
201
236
  when Hash
202
237
  tag(name) do
203
238
  opt_order(item).map { |tag, value|
204
239
  case value
205
- when String
206
- tag(tag) { value }
240
+ when String, Symbol
241
+ tag(tag) { value.to_s }
207
242
  when Array
208
243
  value.map { |subitem| nested_data_to_xml(tag, subitem) }.join
209
244
  when Hash
@@ -222,6 +257,10 @@ class ApricotEatsGorilla
222
257
  # * +name+ - The name of the XML tag.
223
258
  # * +attributes+ - Optional. Hash of attributes for the XML tag.
224
259
  def tag(name, attributes = {})
260
+ name = to_lower_camel_case(name) unless disable_tag_names_to_lower_camel_case
261
+ if nodes_to_namespace.kind_of? Array
262
+ name = "#{node_namespace}:#{name}" if node_namespace && nodes_to_namespace.include?(name)
263
+ end
225
264
  return "<#{name} />" unless block_given?
226
265
 
227
266
  attr = opt_order(attributes).map { |k, v| %Q( xmlns:#{k}="#{v}") }.to_s
@@ -231,20 +270,15 @@ class ApricotEatsGorilla
231
270
 
232
271
  # Removes line breaks and whitespace between tags from a given +xml+ String.
233
272
  def clean_xml(xml)
234
- xml = xml.gsub(/\n+/, "")
235
- xml.gsub(/(>)\s*(<)/, '\1\2')
273
+ xml.gsub!(/\n+/, "")
274
+ xml.gsub!(/(>)\s*(<)/, '\1\2')
275
+ xml
236
276
  end
237
277
 
238
278
  # Removes the namespace from a given XML +tag+.
239
279
  def remove_namespace(tag)
240
- tag.sub(/.+:(.+)/, '\1')
241
- end
242
-
243
- # Converts a given +string+ from CamelCase/lowerCamelCase to snake_case.
244
- def to_snake_case(string)
245
- string = string.gsub(/[A-Z]+/, '\1_\0').downcase
246
- string = string[1, string.length-1] if string[0, 1] == "_"
247
- string
280
+ tag.sub!(/.+:(.+)/, '\1')
281
+ tag
248
282
  end
249
283
 
250
284
  # Checks to see if a given +string+ matches "true" or "false" and converts
@@ -3,10 +3,17 @@ require File.join(File.dirname(__FILE__), "..", "helper")
3
3
  class HashToXmlTest < Test::Unit::TestCase
4
4
 
5
5
  context "Calling hash_to_xml" do
6
- setup { ApricotEatsGorilla.sort_keys = true }
6
+ setup do
7
+ ApricotEatsGorilla.setup do |s|
8
+ s.sort_keys = true
9
+ s.disable_tag_names_to_lower_camel_case = false
10
+ s.disable_hash_keys_to_snake_case = false
11
+ s.disable_hash_keys_to_symbol = false
12
+ end
13
+ end
7
14
 
8
15
  context "with a Hash consisting of a single key-value-pair" do
9
- should "returns an XML String containing one node and a value" do
16
+ should "return an XML String containing one node and a value" do
10
17
  hash = { "apricot" => "eats gorilla" }
11
18
  expected = "<apricot>eats gorilla</apricot>"
12
19
 
@@ -16,7 +23,7 @@ class HashToXmlTest < Test::Unit::TestCase
16
23
  end
17
24
 
18
25
  context "with a Hash containing another Hash" do
19
- should "returns an XML String representing the given structure" do
26
+ should "return an XML String representing the given structure" do
20
27
  hash = { "apricot" => { "eats" => "gorilla", "drinks" => "beer" } }
21
28
  expected = "<apricot><drinks>beer</drinks><eats>gorilla</eats></apricot>"
22
29
 
@@ -26,7 +33,7 @@ class HashToXmlTest < Test::Unit::TestCase
26
33
  end
27
34
 
28
35
  context "with a Hash containing a Hash containing an Array" do
29
- should "returns an XML String representing the given structure" do
36
+ should "return an XML String representing the given structure" do
30
37
  hash = { "apricot" => { "eats" => [ "gorilla", "snake" ] } }
31
38
  expected = "<apricot><eats>gorilla</eats><eats>snake</eats></apricot>"
32
39
 
@@ -36,7 +43,7 @@ class HashToXmlTest < Test::Unit::TestCase
36
43
  end
37
44
 
38
45
  context "with a Hash containing a Hash containing an Array containing a Hash" do
39
- should "returns an XML String representing the given structure" do
46
+ should "return an XML String representing the given structure" do
40
47
  hash = { "apricot" =>
41
48
  { "eats" => [ { "lotsOf" => "gorillas" }, { "justSome" => "snakes" } ]
42
49
  } }
@@ -47,6 +54,38 @@ class HashToXmlTest < Test::Unit::TestCase
47
54
  assert_equal expected, result
48
55
  end
49
56
  end
57
+
58
+ context "with a Hash containing Symbols" do
59
+ should "returns an XML String with Symbols converted into Strings" do
60
+ hash = { :apricot => { :eats => [ :gorilla, "snake" ] } }
61
+ expected = "<apricot><eats>gorilla</eats><eats>snake</eats></apricot>"
62
+
63
+ result = ApricotEatsGorilla.hash_to_xml(hash)
64
+ assert_equal expected, result
65
+ end
66
+ end
67
+
68
+ context "with a Hash containing snake_case keys" do
69
+ should "convert snake_case Hash keys to lowerCamelCase" do
70
+ hash = { :apricot => { :eats => { :lots_of => "gorillas" } } }
71
+ expected = "<apricot><eats><lotsOf>gorillas</lotsOf></eats></apricot>"
72
+
73
+ result = ApricotEatsGorilla.hash_to_xml(hash)
74
+ assert_equal expected, result
75
+ end
76
+
77
+ context "and converting snake_case tag names to lowerCamelCase turned off" do
78
+ setup { ApricotEatsGorilla.disable_tag_names_to_lower_camel_case = true }
79
+
80
+ should "not convert snake_case tag names to lowerCamelCase" do
81
+ hash = { :apricot => { :eats => { :lots_of => "gorillas" } } }
82
+ expected = "<apricot><eats><lots_of>gorillas</lots_of></eats></apricot>"
83
+
84
+ result = ApricotEatsGorilla.hash_to_xml(hash)
85
+ assert_equal expected, result
86
+ end
87
+ end
88
+ end
50
89
  end
51
90
 
52
91
  end
@@ -3,7 +3,14 @@ require File.join(File.dirname(__FILE__), "..", "helper")
3
3
  class ShortcutMethodTest < Test::Unit::TestCase
4
4
 
5
5
  context "Calling []" do
6
- setup { ApricotEatsGorilla.sort_keys = true }
6
+ setup do
7
+ ApricotEatsGorilla.setup do |s|
8
+ s.sort_keys = true
9
+ s.disable_tag_names_to_lower_camel_case = false
10
+ s.disable_hash_keys_to_snake_case = false
11
+ s.disable_hash_keys_to_symbol = false
12
+ end
13
+ end
7
14
 
8
15
  context "with an XML String" do
9
16
  should "return a Hash containing the XML content" do
@@ -3,7 +3,14 @@ require File.join(File.dirname(__FILE__), "..", "helper")
3
3
  class XmlToHashTest < Test::Unit::TestCase
4
4
 
5
5
  context "Calling xml_to_hash" do
6
- setup { ApricotEatsGorilla.sort_keys = true }
6
+ setup do
7
+ ApricotEatsGorilla.setup do |s|
8
+ s.sort_keys = true
9
+ s.disable_tag_names_to_lower_camel_case = false
10
+ s.disable_hash_keys_to_snake_case = false
11
+ s.disable_hash_keys_to_symbol = false
12
+ end
13
+ end
7
14
 
8
15
  context "with a SOAP response example and a custom root node" do
9
16
  should "return a Hash containing the XML content" do
@@ -56,14 +63,48 @@ class XmlToHashTest < Test::Unit::TestCase
56
63
  end
57
64
  end
58
65
 
59
- context "with XML containing nodes with the same name" do
60
- should "group these nodes into an Array" do
61
- xml = "<root><items>first</items><items>second</items></root>"
62
- expected = { :items => [ "first", "second" ] }
66
+ context "with XML containing lowerCamelCase nodes" do
67
+ should "convert lowerCamelCase nodes to snake_case" do
68
+ xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
69
+ expected = { :first_name => "Jungle", :last_name => "Julia" }
70
+
71
+ result = ApricotEatsGorilla.xml_to_hash(xml)
72
+ assert_equal expected, result
73
+ end
74
+
75
+ context "and converting lowerCamelCase Hash keys to snake_case turned off" do
76
+ setup { ApricotEatsGorilla.disable_hash_keys_to_snake_case = true }
77
+
78
+ should "not convert lowerCamelCase Hash keys to snake_case" do
79
+ xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
80
+ expected = { :firstName => "Jungle", :lastName => "Julia" }
81
+
82
+ result = ApricotEatsGorilla.xml_to_hash(xml)
83
+ assert_equal expected, result
84
+ end
85
+ end
86
+ end
87
+
88
+ context "with some XML" do
89
+ should "convert Hash keys to Symbols" do
90
+ xml = "<contact><name>Jungle Julia</name><address/></contact>"
91
+ expected = { :name => "Jungle Julia", :address => nil }
63
92
 
64
93
  result = ApricotEatsGorilla.xml_to_hash(xml)
65
94
  assert_equal expected, result
66
95
  end
96
+
97
+ context "and converting Hash keys to Symbols turned off" do
98
+ setup { ApricotEatsGorilla.disable_hash_keys_to_symbol = true }
99
+
100
+ should "not convert Hash keys to Symbols" do
101
+ xml = "<contact><name>Jungle Julia</name><address/></contact>"
102
+ expected = { "name" => "Jungle Julia", "address" => nil }
103
+
104
+ result = ApricotEatsGorilla.xml_to_hash(xml)
105
+ assert_equal expected, result
106
+ end
107
+ end
67
108
  end
68
109
  end
69
110
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smacks-apricoteatsgorilla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.61
4
+ version: 0.3.91
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Harrington