Kronk

DESCRIPTION:

Kronk runs diffs against data from live and cached http responses. Kronk was made possible by the sponsoring of AT&T Interactive.

FEATURES:

  • Parse and display or diff data from http response body and/or headers.

  • Include or exclude specific data points with file-glob-like paths.

  • Supports json, rails-ish xml, and plist.

  • Support for custom data parsers and diff formatters.

  • Launch IRB console with the retrieved response data.

  • Support for proxies, ssl, and basic auth.

  • Cookie/session handling.

  • Easy-to-read output (color or ascii, line numbers, etc).

  • URI-specific configuration.

  • Helper methods for test suites.

  • Query and logfile playback with custom output.

  • Yes, it works on Windows.

SYNOPSIS:

Check if your json response returns the same data as your xml variety:

$ kronk host.com/path.json host.com/path.xml

Compare body structure only (uses datatypes instead of values):

$ kronk host.com/path.json host.com/path.xml --struct

Call comparison with similar uri suffixes:

$ kronk host.com/path.json host.com/path.xml --suff '?page=1'

Compare response over ssl with the previous call:

$ kronk https://host.com/path --prev

Compare response with a local file:

$ kronk host.com/path.json ./saved_response.json

Do a simple text diff on the http response, including headers:

$ kronk host.com/A/path.json host.com/B/path.json --raw -i

Run it to display formatted data:

$ kronk host.com/path.json

Run it to display raw data with headers from the default host (localhost:3000). Default host is assumed when the path starts with “/” and no local files match the given path:

$ kronk /path.json --raw -i

Parse the data and run an IRB console with the response:

$ kronk http://host.com/path.json --irb

CONFIGURATION:

Kronk pulls it’s config from $HOME/.kronk/rc and supports the following:

Set the file to save the last http response retrieved in:

:cache_file: ~/.kronk_cache

Content types to match to a parser. Keys are used as a part of a regexp and values are evaluated by const_get.

:content_types:
  xml:   XMLParser
  plist: PlistParser
  json:  JSON

How to format the diff output. Supports the “special” values :ascii_diff and :color_diff or any string that will correctly resolve to a constant:

:diff_format: :ascii_diff

Adding User-Agent aliases is useful and simple!

:user_agents:
  win_ie9: Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.0)

Disabling cookies altogether may be done as so:

:use_cookies: false

Setting URI-specific config options - useful if you always need to send headers or use one-off configs. Each URI option key is dropped in a regexp to match against the given URIs so this also works for local files. See Kronk::Request.retrieve and Kronk::Response#selective_data for all options supported:

:uri_options:
  example.com:
    :parser:      XMLParser
    :http_method: POST

    :follow_redirects: true

    :ignore_data:
      - path/to/data/point

    :headers:
      X-Something: custom header value

    :query:
      offset: 1
      limit:  10

Require specific files or gems in the ruby runtime:

:requires:
  - kronk-my_custom_parser
  - kronk-my_custom_diff_formatter

Show line numbers in the output:

:show_lines: true

Assign a default host to use when none is specified:

:default_host: http://localhost:3000

Set the number of spaces for indentation:

:indentation: 2

Bash Completion:

Bash completion is available by sourcing the file returned by:

$ kronk --completion
[gem path]/script/kronk_completion

DATA TRAVERSING:

One of Kronk’s most powerful features is its ability to segregate data. From the command line, this is done by passing data paths after ‘–’:

$ kronk http://host.com -- data/path1 data/path2/1/child

The previous example will narrow down the parsed data returned to something like:

{
  "data": {
    "path1": "value1",
    "path2": [
      {
        "child": "child value"
      }
    ]
  }
}

If excluding data points is preferred, prepending a ‘-’ to the path will flag it to remove data:

$ kronk http://host.com -- -data/path2

{
  "data": {
    "path1": "value1",
    "path3": "value3",
    "path4": "value4"
  }
}

If you would like to exclude or include only items with a specific child attribute, you can do so by appending “/..” to the path.

$ kronk http://host.com -- data/path2/1/child/..

{
  "data": {
    "path2": [
      {
        "first_child": "first value",
        "child": "child value",
        "last_child": "last value"
      },
    ]
  }
}

Path matcher parsing may also be affected by appending Regexp options to the end of the path in the following fashion:

# Make path case insensitive
$ kronk host.com -- data/path2/1//i

{
  "data": {
    "PATH2": [
      "item at index 1"
    ],

    "path2": [
      {
        "first_child": "first value",
        "child": "child value",
        "last_child": "last value"
      }
    ]
  }
}

# Make path case insensitive and multiline
$ kronk host.com -- data/**=*foobar*//im

{
  "data": {
    "subdata": "test for\nFOOBAR\nmultiline"
  }
}

There are additionally a variety of wildcard and special characters that are supported:

  • * matches /.*/, meaning zero or more characters

  • **/ matches any key recursively down the data structure

  • ../ matches the parent of the previously matched item

  • 2..5, 2…6 and 2,3 match any Integer or String from 2 to 5

  • ? matches zero or one character

  • | effectively works as an “OR” character, matches /^val1|val2$/

  • = is used to match values and may be used in conjuction with a key or not

  • Parentheses may be used in conjunction with other special characters for a given path item, such as: /path(1|2)

  • \ escapes any special path character

Check out Kronk::Path and Kronk::DataSet for more details on data traversing.

Note: Bash may try to parse your data paths, especially if they start with wildcards or if they contain the pipe “|” character, so you may need to put single quotes around some of your paths.

REQUIREMENTS:

  • json gem

  • cookiejar gem

INSTALL:

$ gem install kronk

DEVELOPERS:

After checking out the source, run:

$ rake newb

This task will install any missing dependencies, run the tests/specs, and generate the RDoc.

LICENSE:

(The MIT License)

Copyright © 2010, 2011 Jeremie Castagna

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.