soaspec 0.0.79 → 0.0.80

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc958fe0aac536d439cbc015ff650574599df2e1
4
- data.tar.gz: 8010383ef455985729cc65abf1992afbd42749c9
3
+ metadata.gz: da0b04fa67877430f80b6850de438eddff6ebec0
4
+ data.tar.gz: 6ab2a8342e27068d1e8004ba2d37f3183222f7e2
5
5
  SHA512:
6
- metadata.gz: 3f330c9ae4275aa3d545aa523bc6abe20cd2a3a733430fefaf2dd23dfcac087232fda5ee64058e67aeab74cb53b341e4b205e14ab543136dc8e40bbcbfefb6e1
7
- data.tar.gz: abcb93f6733a20288210f4175b44c94d83c91331e1008e7a2ed07c759ad475b9534542b93f888ff09bfd0767f64662629cac24f8cf311bd60caee8e9635e334d
6
+ metadata.gz: de6404bc0e90325c946b83cb4d98283f272b72747474892bb07aa2ef11387ea54972762ca27598e60fc58dc50494a60377fcea4c0bc3a0e12169b10bba57a463
7
+ data.tar.gz: 656f597d657333dd8c2159d03a3608dedef0026f2d2017ed0a5ce468067c4918a2e850a3c8e12f8bab6bd09cb3f99fcd520068a221280c4d7c558c9f6abec795
data/ChangeLog CHANGED
@@ -1,3 +1,9 @@
1
+ Version 0.0.80
2
+ * Enhancements
3
+ * `values_from_path` method on `RestHandler` to easily extract multiple values for an xpath or json path
4
+ * Bug fix
5
+ * Support response that has white space surrounding message
6
+
1
7
  Version 0.0.79
2
8
  * Enhancements
3
9
  * `values_from_path` method on `SoapHandler` to easily extract multiple values for an xpath
@@ -66,6 +66,22 @@ class SoaspecVirtualServer < Sinatra::Application
66
66
  JSON.generate result
67
67
  end
68
68
 
69
+ get '/test/multiple_json' do
70
+ <<-BOOKS
71
+ {"store":
72
+ {"bicycle":
73
+ {"price":19.95, "color":"red"},
74
+ "book":[
75
+ {"price":8.95, "category":"reference", "title":"Sayings of the Century", "author":"Nigel Rees"},
76
+ {"price":12.99, "category":"fiction", "title":"Sword of Honour", "author":"Evelyn Waugh"},
77
+ {"price":8.99, "category":"fiction", "isbn":"0-553-21311-3", "title":"Moby Dick", "author":"Herman Melville","color":"blue"},
78
+ {"price":22.99, "category":"fiction", "isbn":"0-395-19395-8", "title":"The Lord of the Rings", "author":"Tolkien"}
79
+ ]
80
+ }
81
+ }
82
+ BOOKS
83
+ end
84
+
69
85
  patch '/test/puppy/:id' do |id|
70
86
  request_hash = JSON.parse(request.body.string)
71
87
  Soaspec::TestServer::PuppyService.data[id.to_i][:Name] = request_hash['Name']
@@ -280,21 +280,32 @@ module Soaspec
280
280
  # Returns the value at the provided xpath
281
281
  # @param [RestClient::Response] response
282
282
  # @param [String] xpath
283
- # @return [String] Value inside element found through Xpath
284
- def xpath_value_for(response: nil, xpath: nil, attribute: nil)
283
+ # @return [Enumerable] Value inside element found through Xpath
284
+ def xpath_elements_for(response: nil, xpath: nil, attribute: nil)
285
285
  raise ArgumentError unless response && xpath
286
286
  raise "Can't perform XPATH if response is not XML" unless Interpreter.response_type_for(response) == :xml
287
- result =
288
- if Soaspec.strip_namespaces? && !xpath.include?(':')
289
- temp_doc = Nokogiri.parse response.body
290
- temp_doc.remove_namespaces!
291
- temp_doc.xpath(xpath).first
292
- else
293
- Nokogiri.parse(response.body).xpath(xpath).first
294
- end
295
- raise NoElementAtPath, "No value at Xpath '#{xpath}'" unless result
296
- return result.inner_text if attribute.nil?
297
- result.attributes[attribute].inner_text
287
+ xpath = "//*[@#{attribute}]" unless attribute.nil?
288
+ if xpath[0] != '/'
289
+ xpath = convert_to_pascal_case(xpath) if pascal_keys?
290
+ xpath = '//' + xpath
291
+ end
292
+ if Soaspec.strip_namespaces? && !xpath.include?(':')
293
+ temp_doc = Nokogiri.parse response.body
294
+ temp_doc.remove_namespaces!
295
+ temp_doc.xpath(xpath)
296
+ else
297
+ Nokogiri.parse(response.body).xpath(xpath)
298
+ end
299
+ end
300
+
301
+ # @return [Enumerable] List of values matching JSON path
302
+ def json_path_values_for(response, path, attribute: nil)
303
+ raise 'JSON does not support attributes' if attribute
304
+ if path[0] != '$'
305
+ path = convert_to_pascal_case(path) if pascal_keys?
306
+ path = '$..' + path
307
+ end
308
+ JsonPath.on(response.body, path)
298
309
  end
299
310
 
300
311
  # Based on a exchange, return the value at the provided xpath
@@ -305,22 +316,14 @@ module Soaspec
305
316
  # @return [String] Value at Xpath
306
317
  def value_from_path(response, path, attribute: nil)
307
318
  path = path.to_s
308
-
309
319
  case Interpreter.response_type_for(response)
310
320
  when :xml
311
- path = "//*[@#{attribute}]" unless attribute.nil?
312
- if path[0] != '/'
313
- path = convert_to_pascal_case(path) if pascal_keys?
314
- path = '//' + path
315
- end
316
- xpath_value_for(response: response, xpath: path, attribute: attribute)
321
+ result = xpath_elements_for(response: response, xpath: path, attribute: attribute).first
322
+ raise NoElementAtPath, "No value at Xpath '#{path}'" unless result
323
+ return result.inner_text if attribute.nil?
324
+ return result.attributes[attribute].inner_text
317
325
  when :json
318
- raise 'JSON does not support attributes' if attribute
319
- if path[0] != '$'
320
- path = convert_to_pascal_case(path) if pascal_keys?
321
- path = '$..' + path
322
- end
323
- matching_values = JsonPath.on(response.body, path)
326
+ matching_values = json_path_values_for(response, path, attribute: attribute)
324
327
  raise NoElementAtPath, "Element in #{response.body} not found with path '#{path}'" if matching_values.empty?
325
328
  matching_values.first
326
329
  when :hash
@@ -330,6 +333,19 @@ module Soaspec
330
333
  end
331
334
  end
332
335
 
336
+ # @return [Enumerable] List of values returned from path
337
+ def values_from_path(response, path, attribute: nil)
338
+ path = path.to_s
339
+ case Interpreter.response_type_for(response)
340
+ when :xml
341
+ xpath_elements_for(response: response, xpath: path, attribute: attribute).map(&:inner_text)
342
+ when :json
343
+ json_path_values_for(response, path, attribute: attribute)
344
+ else
345
+ raise "Unable to interpret type of #{response.body}"
346
+ end
347
+ end
348
+
333
349
  # Convenience methods for once off usage of a REST request
334
350
  class << self
335
351
 
@@ -188,7 +188,7 @@ module Soaspec
188
188
 
189
189
  # @return [Enumerable] List of values returned from path
190
190
  def values_from_path(response, path, attribute: nil)
191
- xpath_elements_for(response: response, xpath: path, attribute: attribute).map(&:inner_text) # { |e| e.inner_text }
191
+ xpath_elements_for(response: response, xpath: path, attribute: attribute).map(&:inner_text)
192
192
  end
193
193
 
194
194
  # alias elements xpath_elements_for
@@ -6,7 +6,7 @@ class Interpreter
6
6
  # @return [Symbol] Type of provided response
7
7
  def self.response_type_for(response)
8
8
  if response.is_a? String
9
- case response.body[0]
9
+ case response.strip.body[0]
10
10
  when '<'
11
11
  :xml
12
12
  when '{', '['
@@ -1,4 +1,6 @@
1
1
  <note date="2008-01-10">
2
2
  <to>Tove</to>
3
3
  <from>Jani</from>
4
+ <comment_line>First comment</comment_line>
5
+ <comment_line>Second comment</comment_line>
4
6
  </note>
@@ -1,3 +1,3 @@
1
1
  module Soaspec
2
- VERSION = '0.0.79'.freeze
2
+ VERSION = '0.0.80'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soaspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.79
4
+ version: 0.0.80
5
5
  platform: ruby
6
6
  authors:
7
7
  - SamuelGarrattIQA