Class: Thredded::SearchParser

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/thredded/search_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ SearchParser

Returns a new instance of SearchParser.



5
6
7
8
# File 'app/models/concerns/thredded/search_parser.rb', line 5

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

Instance Method Details

#parseObject



10
11
12
13
14
# File 'app/models/concerns/thredded/search_parser.rb', line 10

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

#parse_keywordsObject



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

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

#parse_textObject



34
35
36
37
38
39
# File 'app/models/concerns/thredded/search_parser.rb', line 34

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