Sha256: 853a214243cbdefc3c0c0bcf6d93cf310a516fcba8ae3e5f5fe664d9249d67ee

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

class JsonParser::Crawler
  attr_reader :post_process, :path, :case_type, :compact, :default

  def initialize(path, case_type: :lower_camel, compact: false, default: nil, &block)
    @case_type = case_type
    @compact = compact
    @default = default
    @path = path.map { |p| change_case(p) }
    @post_process = block
  end

  def crawl(json, index = 0)
    return wrap(json) if is_ended?(index)
    return crawl_array(json, index) if json.is_a? Array

    crawl(fetch(json, index), index + 1)
  rescue NoMethodError
    wrap(default)
  end

  private

  def fetch(json, index)
    key = path[index]
    json.key?(key) ? json[key] : json[key.to_sym]
  end

  def is_ended?(index)
    index >= path.size
  end

  def wrap(json)
    post_process.call(json)
  end

  def change_case(key)
    case case_type
    when :lower_camel
      key.camelize(:lower)
    when :upper_camel
      key.camelize(:upper)
    when :snake
      key.underscore
    end
  end

  def crawl_array(array, index)
    array.map { |j| crawl(j, index) }.tap do |a|
      a.compact! if compact
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_parser-1.3.0 lib/json_parser/crawler.rb