Module: CucumberApi::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_and_check_value(json_path, type, value, json = nil) ⇒ Object
Retrieve value of the first JSON element with given JSON path as given type, and check for a given value required type or value.
-
#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
12 13 14 15 16 |
# File 'lib/cucumber-api/response.rb', line 12 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
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cucumber-api/response.rb', line 34 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cucumber-api/response.rb', line 53 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_and_check_value(json_path, type, value, json = nil) ⇒ Object
Retrieve value of the first JSON element with given JSON path as given type, and check for a given value required type or value
99 100 101 102 103 104 |
# File 'lib/cucumber-api/response.rb', line 99 def get_as_type_and_check_value json_path, type, value, json=nil v = get_as_type json_path, type, json if value != v.to_s raise %/Expect '#{json_path}' to be '#{value}' but was '#{v}'\n#{to_json_s}/ end 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
86 87 88 89 |
# File 'lib/cucumber-api/response.rb', line 86 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
22 23 24 25 26 27 |
# File 'lib/cucumber-api/response.rb', line 22 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
108 109 110 111 112 113 114 |
# File 'lib/cucumber-api/response.rb', line 108 def to_json_s if ENV['cucumber_api_verbose'] == 'true' JSON.pretty_generate(JSON.parse to_s) else '' end end |