-
Notifications
You must be signed in to change notification settings - Fork 0
Usage Examples
If you are new to the poller gem, it is highly recommended that you check out the poller README and Wiki first.
Please note that you can find examples for all JSON related Matchers in the unit and integration tests located in the /spec folder (eg in json_path_has_object_spec.rb).
This matcher allows us to check for the presence of a JSONPath (given as String) in a JSON document that we periodically read from a URI.
require 'poller/poller_json'
matcher = Matchers::JSON::DocumentContainsJSONPath.new('$geoplugin_countryName')
poller = Poller::HTTP::HttpPoller.new('http://www.geoplugin.net/json.gp?ip=216.113.168.141', matcher, 5.0, 1.0)
# timeout 5 seconds, poll once per second
poller.check
Setting up the poller works in the same way for all matchers and is described in more detail in the poller README. The following examples therefore omit the poller part and are focused only on the setup of the matcher.
In case you are not yet familiar with the JSON structures 'object', 'array' and 'value', checkout JSON for an explanation.
Use this matcher to test if a JSONPath evaluates to a given JSON value.
require 'poller/poller_json'
matcher = Matchers::JSON::JSONPathHasValue.new('$geoplugin_currencyCode', 'USD')
This matcher can be used to look for a specific JSON array at a given JSONPath. The array in the example below is an array of JSON objects.
require 'poller/poller_json'
json_array = '[{"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}]'
matcher = Matchers::JSON::JSONPathHasArray.new('$menu.popup.menuitem', json_array)
Use this matcher in order to test for a specific JSON object at a given JSONPath.
require 'poller/poller_json'
json_object = '{"value": "Close", "onclick": "CloseDoc()"}'
matcher = Matchers::JSON::JSONPathHasObject.new('$menu.popup.menuitem[2]', json_object)