Sha256: 16b6118e8adc88eaa85a1dbbe0ba9133639c48dd148a1c49e09fec54a57c6fdd

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

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, '')

        if keyword_scan.present?
          keyword_scan.each do |term|
            keyword_term = term.delete(' ').split(':')

            if found_terms_hash[keyword].nil?
              found_terms_hash[keyword] = []
            end

            found_terms_hash[keyword] << keyword_term[1]
          end
        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

2 entries across 2 versions & 1 rubygems

Version Path
thredded-0.3.1 lib/thredded/search_parser.rb
thredded-0.3.0 lib/thredded/search_parser.rb