lib/jsonpath.rb in jsonpath-0.4.2 vs lib/jsonpath.rb in jsonpath-0.5.0

- old
+ new

@@ -1,12 +1,15 @@ require 'strscan' +require 'multi_json' require 'jsonpath/proxy' require 'jsonpath/enumerable' require 'jsonpath/version' class JsonPath + PATH_ALL = '$..*' + attr_reader :path def initialize(path, opts = nil) @opts = opts scanner = StringScanner.new(path) @@ -47,25 +50,31 @@ @path.last << token end end end - def on(object) - enum_on(object).to_a + def on(obj_or_str) + enum_on(obj_or_str).to_a end - def first(object) - enum_on(object).first + def first(obj_or_str, *args) + enum_on(obj_or_str).first(*args) end - def enum_on(object) - JsonPath::Enumerable.new(self, object, @opts) + def enum_on(obj_or_str, mode = nil) + JsonPath::Enumerable.new(self, self.class.process_object(obj_or_str), mode, @opts) end + alias_method :[], :enum_on - def self.on(object, path, opts = nil) - self.new(path, opts).on(object) + def self.on(obj_or_str, path, opts = nil) + self.new(path, opts).on(process_object(obj_or_str)) end - def self.for(obj) - Proxy.new(obj) + def self.for(obj_or_str) + Proxy.new(process_object(obj_or_str)) + end + + private + def self.process_object(obj_or_str) + obj_or_str.is_a?(String) ? MultiJson.decode(obj_or_str) : obj_or_str end end