Class: Thredded::SearchParser

Inherits:
Object
  • Object
show all
Defined in:
lib/thredded/search_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ SearchParser

Returns a new instance of SearchParser.



4
5
6
7
# File 'lib/thredded/search_parser.rb', line 4

def initialize(query)
  @query = query
  @keywords = %w(in by order)
end

Instance Method Details

#parseObject



9
10
11
12
13
# File 'lib/thredded/search_parser.rb', line 9

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

#parse_keywordsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/thredded/search_parser.rb', line 15

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 unless keyword_scan.present?
    keyword_scan.each do |term|
      found_terms_hash[keyword] ||= []
      found_terms_hash[keyword] << term.delete(' ').split(':')[1]
    end
  end

  found_terms_hash
end

#parse_textObject



33
34
35
36
37
38
# File 'lib/thredded/search_parser.rb', line 33

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