Class: Dalziel::Matchers::JSONPatternMatcher
- Inherits:
-
Object
- Object
- Dalziel::Matchers::JSONPatternMatcher
- Defined in:
- lib/dalziel.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#json_expression ⇒ Object
readonly
Returns the value of attribute json_expression.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #does_not_match? ⇒ Boolean
- #failure_message ⇒ Object
-
#initialize(json_expression) ⇒ JSONPatternMatcher
constructor
A new instance of JSONPatternMatcher.
- #matches?(json) ⇒ Boolean
Constructor Details
#initialize(json_expression) ⇒ JSONPatternMatcher
Returns a new instance of JSONPatternMatcher.
81 82 83 |
# File 'lib/dalziel.rb', line 81 def initialize(json_expression) @json_expression = json_expression end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
79 80 81 |
# File 'lib/dalziel.rb', line 79 def body @body end |
#json_expression ⇒ Object (readonly)
Returns the value of attribute json_expression.
79 80 81 |
# File 'lib/dalziel.rb', line 79 def json_expression @json_expression end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
79 80 81 |
# File 'lib/dalziel.rb', line 79 def request @request end |
Instance Method Details
#does_not_match? ⇒ Boolean
85 86 87 |
# File 'lib/dalziel.rb', line 85 def does_not_match?(*) fail "Inverted matching is not implemented with this matcher" end |
#failure_message ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dalziel.rb', line 98 def if @not_parsable_json original = Dalziel.indent(@original.inspect) error = "#{@not_parsable_json.class}: #{@not_parsable_json}" "Couldn't parse the following:\n\n%s\n\n%s" % [ original, error ] else json = Dalziel.indent(JSON.pretty_generate(@hash)) type = @original.is_a?(String) ? "JSON" : @original.class.to_s "Got the following %s:\n\n%s\n\n%s" % [ type, json, matcher.last_error ] end end |
#matches?(json) ⇒ Boolean
89 90 91 92 93 94 95 96 |
# File 'lib/dalziel.rb', line 89 def matches?(json) @original = json @hash = json.is_a?(String) ? JSON.parse(json) : json matcher =~ @hash rescue JSON::ParserError => error @not_parsable_json = error false end |