Module: CucumberApi::Response
- Includes:
- RestClient::Response
- Defined in:
- lib/cucumber-api/response.rb
Overview
Extension of RestClient::Response with support for JSON path traversal and validation
Class Method Summary collapse
-
.create(response) ⇒ Response
Create a Response with JSON path support.
Instance Method Summary collapse
-
#get(json_path, json = nil) ⇒ Object
Retrieve value of the first JSON element with given JSON path.
-
#get_as_type(json_path, type, json = nil) ⇒ Object
Retrieve value of the first JSON element with given JSON path as given type or ‘object’ required type.
-
#get_as_type_or_null(json_path, type, json = nil) ⇒ Object
Retrieve value of the first JSON element with given JSON path as given type, with nil value allowed or ‘object’ required type.
-
#has(json_path, json = nil) ⇒ true, false
Check if given JSON path exists.
-
#to_json_s ⇒ String
Retrieve pretty JSON response for logging.
Class Method Details
.create(response) ⇒ Response
Create a Response with JSON path support
15 16 17 18 19 |
# File 'lib/cucumber-api/response.rb', line 15 def self.create response result = response result.extend Response result end |
Instance Method Details
#get(json_path, json = nil) ⇒ Object
Retrieve value of the first JSON element with given JSON path
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cucumber-api/response.rb', line 37 def get json_path, json=nil if json.nil? json = JSON.parse body end results = JsonPath.new(json_path).on(json) if results.empty? raise %/Expected json path '#{json_path}' not found\n#{to_json_s}/ end results.first end |
#get_as_type(json_path, type, json = nil) ⇒ Object
Retrieve value of the first JSON element with given JSON path as given type or ‘object’ required type
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cucumber-api/response.rb', line 56 def get_as_type json_path, type, json=nil value = get json_path, json case type when 'numeric' valid = value.is_a? Numeric when 'array' valid = value.is_a? Array when 'string' valid = value.is_a? String when 'boolean' valid = !!value == value when 'numeric_string' valid = value.is_a?(Numeric) or value.is_a?(String) when 'object' valid = value.is_a? Hash else raise %/Invalid expected type '#{type}'/ end unless valid raise %/Expect '#{json_path}' as a '#{type}' but was '#{value.class}'\n#{to_json_s}/ end value end |
#get_as_type_or_null(json_path, type, json = nil) ⇒ Object
Retrieve value of the first JSON element with given JSON path as given type, with nil value allowed or ‘object’ required type
89 90 91 92 |
# File 'lib/cucumber-api/response.rb', line 89 def get_as_type_or_null json_path, type, json=nil value = get json_path, json value.nil? ? value : get_as_type(json_path, type, json) end |
#has(json_path, json = nil) ⇒ true, false
Check if given JSON path exists
25 26 27 28 29 30 |
# File 'lib/cucumber-api/response.rb', line 25 def has json_path, json=nil if json.nil? json = JSON.parse body end not JsonPath.new(json_path).on(json).empty? end |
#to_json_s ⇒ String
Retrieve pretty JSON response for logging
96 97 98 99 100 101 102 |
# File 'lib/cucumber-api/response.rb', line 96 def to_json_s if ENV['cucumber_api_verbose'] == 'true' JSON.pretty_generate(JSON.parse to_s) else '' end end |