Sha256: c00495672db16b163c5fc8007947e43d9b833ab299e8d73d56988426a6186e32

Contents?: true

Size: 1.02 KB

Versions: 10

Compression:

Stored size: 1.02 KB

Contents

module Thredded
  class SearchParser
    def initialize(query)
      @query = query
      @keywords = ['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.gsub(' ', '').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

10 entries across 10 versions & 1 rubygems

Version Path
thredded-0.0.12 lib/thredded/search_parser.rb
thredded-0.0.10 lib/thredded/search_parser.rb
thredded-0.0.9 lib/thredded/search_parser.rb
thredded-0.0.8 lib/thredded/search_parser.rb
thredded-0.0.7 lib/thredded/search_parser.rb
thredded-0.0.6 lib/thredded/search_parser.rb
thredded-0.0.5 lib/thredded/search_parser.rb
thredded-0.0.4 lib/thredded/search_parser.rb
thredded-0.0.3 lib/thredded/search_parser.rb
thredded-0.0.1 lib/thredded/search_parser.rb