Sha256: 762e7a987c90cf5bc989ec3d8a872bad7beb34a61137e0b26dbdfb571167b87e

Contents?: true

Size: 745 Bytes

Versions: 5

Compression:

Stored size: 745 Bytes

Contents

module JsonHelper
  def resolve_json_path(json, template)
    if template.start_with?("$") then
      JsonPath.on(json, template)
    else
      paths = template.scan(/(\{(.*?)\})/).uniq
      return template if paths.empty?
      combinations = paths.map { |path| {path[0] => JsonPath.on(json, path[1])} }.reduce(&:merge)

      combinations = combinations.values[0].product(*combinations.values[1..-1]).map{ |x| combinations.keys.zip(x).to_h }

      combinations.map do |c|
        t = template+''
        c.keys.each do |key|
          t.gsub!(key, c[key])
        end
        t
      end
    end
  end

  def valid_json?(json)
    begin
      JSON.parse(json)
      return true
    rescue Exception => e
      return false
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
chaoite-0.1.9 lib/chaoite/json_helper.rb
chaoite-0.1.8 lib/chaoite/json_helper.rb
chaoite-0.1.7 lib/chaoite/json_helper.rb
chaoite-0.1.6 lib/chaoite/json_helper.rb
chaoite-0.1.5 lib/chaoite/json_helper.rb