Sha256: 2c703aca5361d71c18057591059ebcc1b954376934d5ff56603214ed06f85eac

Contents?: true

Size: 953 Bytes

Versions: 42

Compression:

Stored size: 953 Bytes

Contents

# frozen_string_literal: true

module Thredded
  class SearchParser
    def initialize(query)
      @query = query
      @keywords = %w[in by order]
    end

    def parse
      parsed_input = parse_keywords
      parsed_input['text'] = parse_text
      parsed_input
    end

    def parse_keywords
      found_terms_hash = {}

      @keywords.each do |keyword|
        regex = Regexp.new(keyword + '\s*:\s*\w+', Regexp::IGNORECASE)
        keyword_scan = @query.scan(regex)
        @query = @query.gsub(regex, '')

        next if keyword_scan.blank?
        keyword_scan.each do |term|
          found_terms_hash[keyword] ||= []
          found_terms_hash[keyword] << term.delete(' ').split(':')[1]
        end
      end

      found_terms_hash
    end

    def parse_text
      regex = Regexp.new('\"[^"]*\"')
      found_terms = @query.scan(regex)
      @query = @query.sub(regex, '')
      found_terms.concat(@query.split(/\s+/))
    end
  end
end

Version data entries

42 entries across 42 versions & 2 rubygems

Version Path
thredded-1.1.0 app/models/concerns/thredded/search_parser.rb
thredded-1.0.1 app/models/concerns/thredded/search_parser.rb
thredded-1.0.0 app/models/concerns/thredded/search_parser.rb
thredded-0.16.16 app/models/concerns/thredded/search_parser.rb
thredded-0.16.15 app/models/concerns/thredded/search_parser.rb
thredded-0.16.14 app/models/concerns/thredded/search_parser.rb
thredded-0.16.13 app/models/concerns/thredded/search_parser.rb
thredded-0.16.12 app/models/concerns/thredded/search_parser.rb
thredded-0.16.11 app/models/concerns/thredded/search_parser.rb
thredded-0.16.10 app/models/concerns/thredded/search_parser.rb
thredded-0.16.9 app/models/concerns/thredded/search_parser.rb
thredded-0.16.8 app/models/concerns/thredded/search_parser.rb
thredded-0.16.7 app/models/concerns/thredded/search_parser.rb
thredded-0.16.6 app/models/concerns/thredded/search_parser.rb
thredded-0.16.5 app/models/concerns/thredded/search_parser.rb
thredded-0.16.4 app/models/concerns/thredded/search_parser.rb
thredded-0.16.3 app/models/concerns/thredded/search_parser.rb
thredded-0.16.1 app/models/concerns/thredded/search_parser.rb
thredded-0.16.0 app/models/concerns/thredded/search_parser.rb
thredded-0.15.5 app/models/concerns/thredded/search_parser.rb