class Elasticfusion::Search::Query::ValueSanitizer

Public Class Methods

new(mapping) click to toggle source
# File lib/elasticfusion/search/query/value_sanitizer.rb, line 8
def initialize(mapping)
  @mapping = mapping
end

Public Instance Methods

value(value, field:) click to toggle source
# File lib/elasticfusion/search/query/value_sanitizer.rb, line 12
def value(value, field:)
  case @mapping[field.to_sym][:type]
  when 'keyword'
    value
  when 'integer'
    es_integer(value, field: field)
  when 'date'
    es_date(value, field: field)
  end
end

Private Instance Methods

es_date(string, field:) click to toggle source
# File lib/elasticfusion/search/query/value_sanitizer.rb, line 33
def es_date(string, field:)
  parsed = Chronic.parse(string)

  if parsed.nil?
    raise InvalidFieldValueError.new(field, string)
  else
    parsed.iso8601
  end
end
es_integer(string, field:) click to toggle source
# File lib/elasticfusion/search/query/value_sanitizer.rb, line 25
def es_integer(string, field:)
  if string.match? /\A[+-]?\d+\z/
    string
  else
    raise InvalidFieldValueError.new(field, string)
  end
end