Module: JsonSpec::Helpers
Instance Method Summary (collapse)
- - (Object) json_at_path(json, path)
- - (Object) json_path_to_keys(path)
- - (Object) missing_json_path!(path)
- - (Object) parse_json_value(json)
- - (Object) pretty_json_value(ruby)
- - (Object) ruby_at_json_path(json, path)
Instance Method Details
- (Object) json_at_path(json, path)
5 6 7 |
# File 'lib/json_spec/helpers.rb', line 5 def json_at_path(json, path) pretty_json_value(ruby_at_json_path(json, path)) end |
- (Object) json_path_to_keys(path)
26 27 28 |
# File 'lib/json_spec/helpers.rb', line 26 def json_path_to_keys(path) path.to_s.gsub(/(?:^\/|\/$)/, "").split("/").map{|k| k =~ /^\d+$/ ? k.to_i : k } end |
- (Object) missing_json_path!(path)
37 38 39 |
# File 'lib/json_spec/helpers.rb', line 37 def missing_json_path!(path) raise JsonSpec::MissingPathError.new(path) end |
- (Object) parse_json_value(json)
30 31 32 33 34 35 |
# File 'lib/json_spec/helpers.rb', line 30 def parse_json_value(json) JSON.parse(%({"root":#{json}}))["root"] rescue JSON::ParserError # Re-raise more appropriate parsing error JSON.parse(json) end |
- (Object) pretty_json_value(ruby)
9 10 11 12 13 14 15 |
# File 'lib/json_spec/helpers.rb', line 9 def pretty_json_value(ruby) case ruby when Hash, Array then JSON.pretty_generate(ruby) when NilClass then "null" else ruby.inspect end end |
- (Object) ruby_at_json_path(json, path)
17 18 19 20 21 22 23 24 |
# File 'lib/json_spec/helpers.rb', line 17 def ruby_at_json_path(json, path) json_path_to_keys(path).inject(parse_json_value(json)) do |value, key| case value when Hash, Array then value.fetch(key){ missing_json_path!(path) } else missing_json_path!(path) end end end |