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

#load_json(relative_path) ⇒ Object



26
27
28
29
30
31
# File 'lib/json_spec/helpers.rb', line 26

def load_json(relative_path)
  missing_json_directory! if JsonSpec.directory.nil?
  path = File.join(JsonSpec.directory, relative_path)
  missing_json_file!(path) unless File.exist?(path)
  File.read(path)
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