module RSpec::Oj::Helpers

Public Instance Methods

generate_normalized_json(ruby) click to toggle source
# File lib/rspec/oj/helpers.rb, line 28
def generate_normalized_json(ruby)
  case ruby
  when Hash, Array
    ::Oj.dump(ruby, mode: :compat)
  else
    ::Oj.to_json(ruby, mode: :compat)
  end
end
load_json(relative_path) click to toggle source
# File lib/rspec/oj/helpers.rb, line 37
def load_json(relative_path)
  missing_json_directory! unless RSpec::Oj.directory
  path = File.join(RSpec::Oj.directory, relative_path)
  missing_json_file!(path) unless File.exist?(path)
  File.read(path)
end
normalize_json(json, path = nil) click to toggle source
# File lib/rspec/oj/helpers.rb, line 23
def normalize_json(json, path = nil)
  ruby = parse_json(json, path)
  generate_normalized_json(ruby)
end
parse_json(json, path = nil) click to toggle source
# File lib/rspec/oj/helpers.rb, line 10
def parse_json(json, path = nil)
  return parse_json(generate_normalized_json(json), path) unless json.is_a?(String)

  ruby = ::Oj.load("[#{json}]", mode: :compat).first
  value_at_json_path(ruby, path)
rescue EncodingError
  begin
    ::Oj.load(json)
  rescue ::Oj::ParseError
    json
  end
end

Private Instance Methods

missing_json_directory!() click to toggle source
# File lib/rspec/oj/helpers.rb, line 66
def missing_json_directory!
  raise RSpec::Oj::MissingDirectory
end
missing_json_file!(path) click to toggle source
# File lib/rspec/oj/helpers.rb, line 70
def missing_json_file!(path)
  raise RSpec::Oj::MissingFile, path
end
missing_json_path!(path) click to toggle source
# File lib/rspec/oj/helpers.rb, line 62
def missing_json_path!(path)
  raise RSpec::Oj::MissingPath, path
end
value_at_json_path(ruby, path) click to toggle source
# File lib/rspec/oj/helpers.rb, line 46
def value_at_json_path(ruby, path)
  return ruby unless path

  path.split('/').reduce(ruby) do |memo, key|
    case memo
    when Hash
      memo.fetch(key) { missing_json_path!(path) }
    when Array
      missing_json_path!(path) unless /^\d+$/.match?(key)
      memo.fetch(key.to_i) { missing_json_path!(path) }
    else
      missing_json_path!(path)
    end
  end
end