Class: Specdiff::Plugins::Json

Inherits:
Object
  • Object
show all
Includes:
Specdiff::Plugin
Defined in:
lib/specdiff/plugins/json.rb

Class Method Summary collapse

Methods included from Specdiff::Plugin

included

Class Method Details

._json_parsable?(thing) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/specdiff/plugins/json.rb', line 14

def self._json_parsable?(thing)
  return false unless thing.is_a?(String)

  JSON.parse(thing)
  true
rescue JSON::ParserError
  false
end

.compatible?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/specdiff/plugins/json.rb', line 23

def self.compatible?(a, b)
  a.type == :json || b.type == :json
end

.detect_type(thing) ⇒ Object



10
11
12
# File 'lib/specdiff/plugins/json.rb', line 10

def self.detect_type(thing)
  thing.is_a?(String) && _json_parsable?(thing)
end

.diff(a, b) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/specdiff/plugins/json.rb', line 27

def self.diff(a, b)
  a_value = a.value
  b_value = b.value

  a_value = JSON.parse(a_value) if a.type == :json
  b_value = JSON.parse(b_value) if b.type == :json

  ::Specdiff.diff(a_value, b_value)
end

.idObject



6
7
8
# File 'lib/specdiff/plugins/json.rb', line 6

def self.id
  :json
end

.stringify(_diff) ⇒ Object



37
38
39
40
41
# File 'lib/specdiff/plugins/json.rb', line 37

def self.stringify(_diff)
  # since we recurse back into Specdiff, we don't need to stringify in this
  # plugin. the built in hash/array/text differs should do the stringification
  raise "#{self.class}::stringify was called, this should never happen"
end