Sha256: 5209542e6185b14aae1e03158371340df02d42fe990d9df41822e6d80d009e45

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

# -*- coding: utf-8 -*-
#
# @file 
# @brief  クエリーの解析
# @author ongaeshi
# @date   2010/10/21

require 'rubygems'
require 'rack'

module Grenweb
  class Query
    include Rack::Utils
    attr_reader :query_string
  
    OPTIONS = [
               ['package',  'p'],
               ['filepath', 'fpath', 'f'],
               ['suffix',   's'],
              ]

    def initialize(request)
      @query_string = unescape(request.path_info.gsub(/\A\/|\/\z/, ''))
      init_hash
      parse
    end

    def escape_html
      Rack::Utils::escape_html(@query_string)
    end

    def empty?
      keywords.size == 0 && packages.size == 0 && fpaths.size == 0 && suffixs.size == 0
    end

    def keywords
      @hash['keywords']
    end

    def packages
      calc_param(0)
    end

    def fpaths
      calc_param(1)
    end

    def suffixs
      calc_param(2)
    end

    private

    def calc_param(index)
      OPTIONS[index].inject([]){|result, item| result.concat @hash[item] }
    end

    def init_hash
      @hash = {}
      @hash['keywords'] = []

      OPTIONS.flatten.each do |key|
        @hash[key] = []
      end
    end

    def parse
      kp = OPTIONS.flatten.join('|')
      parts = @query_string.scan(/(?:(#{kp}):)?(?:"(.+)"|(\S+))/)

      parts.each do |key, quoted_value, value|
        text = quoted_value || value
        unless (key)
          @hash['keywords'] << text
        else
          @hash[key] << text
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
codestock-0.1.1 lib/cdweb/query.rb
codestock-0.1.0 lib/grenweb/query.rb
gren-0.3.2 lib/grenweb/query.rb
gren-0.3.1 lib/grenweb/query.rb
gren-0.3.0 lib/grenweb/query.rb