Class: APIMatchers::Core::FindInJSON
- Inherits:
-
Object
- Object
- APIMatchers::Core::FindInJSON
- Defined in:
- lib/api_matchers/core/find_in_json.rb
Instance Attribute Summary collapse
-
#json ⇒ Object
readonly
Returns the value of attribute json.
Instance Method Summary collapse
- #find(options = {}) ⇒ Object
-
#initialize(json) ⇒ FindInJSON
constructor
A new instance of FindInJSON.
Constructor Details
#initialize(json) ⇒ FindInJSON
Returns a new instance of FindInJSON.
6 7 8 |
# File 'lib/api_matchers/core/find_in_json.rb', line 6 def initialize(json) @json = json end |
Instance Attribute Details
#json ⇒ Object (readonly)
Returns the value of attribute json.
4 5 6 |
# File 'lib/api_matchers/core/find_in_json.rb', line 4 def json @json end |
Instance Method Details
#find(options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/api_matchers/core/find_in_json.rb', line 10 def find(={}) expected_key = .fetch(:node).to_s expected_value = [:value] @json.each do |key, value| if key == expected_key unless expected_value.nil? if expected_value.is_a? DateTime or expected_value.is_a? Date expected_value = expected_value.to_s elsif expected_value.is_a? Time expected_value = expected_value.to_datetime.to_s end end return value if value == expected_value or expected_value.nil? end # do we have more to recurse through? keep_going = nil if value.is_a? Hash or value.is_a? Array keep_going = value # hash or array, keep going elsif value.nil? and key.is_a? Hash keep_going = key # the array was passed in and now in the key, keep going end if keep_going begin # ignore nodes where the key doesn't match return FindInJSON.new(keep_going).find(node: expected_key, value: expected_value) rescue ::APIMatchers::Core::Exceptions::KeyNotFound end end end # we did not find the requested key raise ::APIMatchers::Core::Exceptions::KeyNotFound.new("key was not found") end |