Class: Dalziel::Matchers::JSONPatternMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/dalziel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



79
80
81
# File 'lib/dalziel.rb', line 79

def body
  @body
end

#json_expressionObject (readonly)

Returns the value of attribute json_expression.



79
80
81
# File 'lib/dalziel.rb', line 79

def json_expression
  @json_expression
end

#requestObject (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

Returns:

  • (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_messageObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dalziel.rb', line 98

def failure_message
  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

Returns:

  • (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