Sha256: 965b909832247a3e8d623047421ac76e995efd27e3f64756b1aca88c1ad588af

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

class JsonParser::Fetcher
  include Sinclair::OptionsParser

  attr_reader :path, :json, :instance

  delegate :after, :flatten, to: :options_object
  delegate :wrap, to: :wrapper

  def initialize(json, instance, path:, **options)
    @path = path.to_s.split('.')
    @json = json
    @instance = instance
    @options = options
  end

  def fetch
    value = crawler.value(json)
    value.flatten! if flatten && value.respond_to?(:flatten!)
    value = instance.send(after, value) if after
    value
  end

  private

  def crawler
    @crawler ||= buidl_crawler
  end

  def buidl_crawler
    JsonParser::Crawler.new(crawler_options) do |value|
      wrap(value)
    end
  end

  def crawler_options
    options.slice(:case_type, :compact, :default).merge(path: path)
  end

  def wrapper
    @wrapper ||= build_wrapper
  end

  def build_wrapper
    JsonParser::Wrapper.new(wrapper_options)
  end

  def wrapper_options
    options.slice(:clazz, :type)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_parser-1.3.1 lib/json_parser/fetcher.rb