Module: JsonSpec::Helpers

Instance Method Summary collapse

Instance Method Details

#generate_normalized_json(ruby) ⇒ Object



19
20
21
22
23
24
# File 'lib/json_spec/helpers.rb', line 19

def generate_normalized_json(ruby)
  case ruby
  when Hash, Array then JSON.pretty_generate(ruby)
  else ruby.to_json
  end
end

#normalize_json(json, path = nil) ⇒ Object



14
15
16
17
# File 'lib/json_spec/helpers.rb', line 14

def normalize_json(json, path = nil)
  ruby = parse_json(json, path)
  generate_normalized_json(ruby)
end

#parse_json(json, path = nil) ⇒ Object



7
8
9
10
11
12
# File 'lib/json_spec/helpers.rb', line 7

def parse_json(json, path = nil)
  ruby = MultiJson.decode(%([#{json}])).first
  value_at_json_path(ruby, path)
rescue MultiJson::DecodeError
  MultiJson.decode(json)
end