Sha256: 99eafccb082b5a00920d6012729908e63d88ab390aa5f1f45b852a50faeaa287

Contents?: true

Size: 1.87 KB

Versions: 37

Compression:

Stored size: 1.87 KB

Contents

module JsonPointer
  class Evaluator
    def initialize(data)
      @data = data
    end

    def evaluate(original_path)
      path = original_path

      # the leading # can either be included or not
      path = path[1..-1] if path[0] == "#"

      # special case on "" or presumably "#"
      if path.empty?
        return @data
      end

      if path[0] != "/"
        raise %{Path must begin with a leading "/": #{original_path}.}
      end

      path_parts = split(path)
      evaluate_segment(@data, path_parts)
    end

    private

    def evaluate_segment(data, path_parts)
      if path_parts.empty?
        data
      elsif data == nil
        # spec doesn't define how to handle this, so we'll return `nil`
        nil
      else
        key = transform_key(path_parts.shift)
        if data.is_a?(Array)
          unless key =~ /^\d+$/
            raise %{Key operating on an array must be a digit or "-": #{key}.}
          end
          evaluate_segment(data[key.to_i], path_parts)
        else
          evaluate_segment(data[key], path_parts)
        end
      end
    end

    # custom split method to account for blank segments
    def split(path)
      parts = []
      last_index = 0
      while index = path.index("/", last_index)
        if index == last_index
          parts << ""
        else
          parts << path[last_index...index]
        end
        last_index = index + 1
      end
      # and also get that last segment
      parts << path[last_index..-1]
      # it should begin with a blank segment from the leading "/"; kill that
      parts.shift
      parts
    end

    def transform_key(key)
      # ~ has special meaning to JSON pointer to allow keys containing "/", so
      # perform some transformations first as defined by the spec
      # first as defined by the spec
      key = key.gsub('~1', '/')
      key = key.gsub('~0', '~')
      key
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
json_schema-0.1.3 lib/json_pointer/evaluator.rb
json_schema-0.1.2 lib/json_pointer/evaluator.rb
json_schema-0.1.1 lib/json_pointer/evaluator.rb
json_schema-0.1.0 lib/json_pointer/evaluator.rb
json_schema-0.0.20 lib/json_pointer/evaluator.rb
json_schema-0.0.19 lib/json_pointer/evaluator.rb
json_schema-0.0.18 lib/json_pointer/evaluator.rb
json_schema-0.0.17 lib/json_pointer/evaluator.rb
json_schema-0.0.16 lib/json_pointer/evaluator.rb
json_schema-0.0.15 lib/json_pointer/evaluator.rb
json_schema-0.0.14 lib/json_pointer/evaluator.rb
json_schema-0.0.13 lib/json_pointer/evaluator.rb
json_schema-0.0.12 lib/json_pointer/evaluator.rb
json_schema-0.0.11 lib/json_pointer/evaluator.rb
json_schema-0.0.10 lib/json_pointer/evaluator.rb
json_schema-0.0.9 lib/json_pointer/evaluator.rb
json_schema-0.0.7 lib/json_pointer/evaluator.rb