class Lurker::Json::Parser

Public Class Methods

new(options = {}) click to toggle source
# File lib/lurker/json/parser.rb, line 14
def initialize(options = {})
  @root_schema = options[:root_schema]
  @parent_schema = options[:parent_schema]
  @parent_property = options[:parent_property]
  @polymorph_if_empty = options.fetch(:polymorph_if_empty, false)
  @uri = options[:uri] || @parent_schema&.uri
  @strategy = nil
end
plain(options = {}) click to toggle source
# File lib/lurker/json/parser.rb, line 5
def plain(options = {})
  new(options).plain
end
typed(options = {}) click to toggle source
# File lib/lurker/json/parser.rb, line 9
def typed(options = {})
  new(options).typed
end

Public Instance Methods

parse(payload) click to toggle source
# File lib/lurker/json/parser.rb, line 23
def parse(payload)
  parse_once { @strategy.new(schema_options_once).parse(payload) }
end
parse_property(property, payload) click to toggle source
# File lib/lurker/json/parser.rb, line 27
def parse_property(property, payload)
  options = schema_options_once.merge!(parent_property: property)
  parse_once { @strategy.new(options).parse(payload) }
end
plain(options = {}) click to toggle source
# File lib/lurker/json/parser.rb, line 32
def plain(options = {})
  @options = schema_options.merge!(options)
  @strategy = strategy_klass(:plain)
  self
end
typed(options = {}) click to toggle source
# File lib/lurker/json/parser.rb, line 38
def typed(options = {})
  @options = schema_options.merge!(options)
  @strategy = strategy_klass(:typed)
  self
end

Private Instance Methods

parse_once(&block) click to toggle source
# File lib/lurker/json/parser.rb, line 46
def parse_once(&block)
  raise 'Define parsing strategy [plain|typed] before using' unless @strategy.present?

  result = block.call
  @strategy = nil

  result
end
schema_options() click to toggle source
# File lib/lurker/json/parser.rb, line 66
def schema_options
  {
    uri: @uri, root_schema: @root_schema, polymorph_if_empty: @polymorph_if_empty,
    parent_schema: @parent_schema, parent_property: @parent_property
  }
end
schema_options_once() click to toggle source
# File lib/lurker/json/parser.rb, line 55
def schema_options_once
  options = @options.present? ? @options.dup : schema_options
  @options = {}

  options
end
strategy_klass(name) click to toggle source
# File lib/lurker/json/parser.rb, line 62
def strategy_klass(name)
  "lurker/json/parser/#{name}_strategy".camelize.constantize
end