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

Instance Method Summary collapse

Class Method Details

.create(response) ⇒ Response

Create a Response with JSON path support

Parameters:

  • response (RestClient::Response)

    original response

Returns:



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

Parameters:

  • json_path (String)

    a valid JSON path expression

  • json (String) (defaults to: nil)

    optional JSON from which to apply JSON path, default to response body

Returns:

  • (Object)

    value of first retrieved JSON element in form of Ruby object

Raises:

  • (Exception)

    if JSON path is invalid or no matching JSON element found



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

Parameters:

  • json_path (String)

    a valid JSON path expression

  • type (String)

    required type, possible values are ‘numeric’, ‘array’, ‘string’, ‘boolean’, ‘numeric_string’

  • json (String) (defaults to: nil)

    optional JSON from which to apply JSON path, default to response body

Returns:

  • (Object)

    value of first retrieved JSON element in form of given type

Raises:

  • (Exception)

    if JSON path is invalid or no matching JSON element found or matching element does not match



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

Parameters:

  • json_path (String)

    a valid JSON path expression

  • type (String)

    required type, possible values are ‘numeric’, ‘array’, ‘string’, ‘boolean’, ‘numeric_string’

  • json (String) (defaults to: nil)

    optional JSON from which to apply JSON path, default to response body

Returns:

  • (Object)

    value of first retrieved JSON element in form of given type or nil

Raises:

  • (Exception)

    if JSON path is invalid or no matching JSON element found or matching element does not match



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

Parameters:

  • json_path (String)

    a valid JSON path expression

  • json (String) (defaults to: nil)

    optional JSON from which to check JSON path, default to response body

Returns:

  • (true, false)

    true if JSON path is valid and exists, false otherwise



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_sString

Retrieve pretty JSON response for logging

Returns:

  • (String)

    pretty JSON response if verbose setting is true, empty string otherwise



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