lib/gscraper/search/query.rb in gscraper-0.1.6 vs lib/gscraper/search/query.rb in gscraper-0.1.7

- old
+ new

@@ -27,10 +27,43 @@ attr_accessor :results_per_page # Search query attr_accessor :query + # Search 'link' modifier + attr_accessor :link + + # Search 'related' modifier + attr_accessor :related + + # Search 'info' modifier + attr_accessor :info + + # Search 'site' modifier + attr_accessor :site + + # Search 'filetype' modifier + attr_accessor :filetype + + # Search 'allintitle' modifier + attr_accessor :allintitle + + # Search 'intitle' modifier + attr_accessor :intitle + + # Search 'allinurl' modifier + attr_accessor :allinurl + + # Search 'inurl' modifier + attr_accessor :inurl + + # Search 'allintext' modifier + attr_accessor :allintext + + # Search 'intext' modifier + attr_accessor :intext + # Search for results containing the exact phrase attr_accessor :exact_phrase # Search for results with the words attr_accessor :with_words @@ -98,10 +131,24 @@ # def initialize(options={},&block) @results_per_page = (options[:results_per_page] || RESULTS_PER_PAGE) @query = options[:query] + + @link = options[:link] + @related = options[:related] + @info = options[:info] + @site = options[:site] + @filetype = options[:filetype] + + @allintitle = options[:allintitle] + @intitle = options[:intitle] + @allinurl = options[:allinurl] + @inurl = options[:inurl] + @allintext = options[:allintext] + @intext = options[:intext] + @exact_phrase = options[:exact_phrase] @with_words = options[:with_words] @without_words = options[:without_words] @language = options[:language] @@ -246,21 +293,58 @@ # # Returns the URL that represents the query. # def search_url url = URI(SEARCH_URL) + query_expr = [] - if @results_per_page - url.query_params['num'] = @results_per_page + set_param = lambda { |param,value| + url.query_params[param.to_s] = value if value + } + + append_modifier = lambda { |name| + modifier = instance_variable_get("@#{name}") + + query_expr << "#{name}:#{modifier}" if modifier + } + + join_ops = lambda { |name| + ops = instance_variable_get("@#{name}") + + if ops.kind_of?(Array) + query_expr << "#{name}:#{ops.join(' ')}" + elsif ops + query_expr << "#{name}:#{ops}" + end + } + + set_param.call('num',@results_per_page) + + query_expr << @query if @query + + append_modifier.call(:link) + append_modifier.call(:related) + append_modifier.call(:info) + append_modifier.call(:site) + append_modifier.call(:filetype) + + join_ops.call(:allintitle) + append_modifier.call(:intitle) + join_ops.call(:allinurl) + append_modifier.call(:inurl) + join_ops.call(:allintext) + append_modifier.call(:intext) + + unless query_expr.empty? + url.query_params['as_q'] = query_expr.join(' ') end - url.query_params['as_q'] = @query if @query - url.query_params['as_epq'] = @exact_phrase if @exact_phrase - url.query_params['as_oq'] = @with_words if @with_words - url.query_params['as_eq'] = @without_words if @without_words + set_param.call('as_epq',@exact_phrase) + set_param.call('as_oq',@with_words) + set_param.call('as_eq',@without_words) - url.query_params['lr'] = @language if @language - url.query_params['cr'] = @region if @region + set_param.call('lr',@language) + set_param.call('cr',@region) if @in_format url.query_params['as_ft'] = 'i' url.query_params['as_filtetype'] = @in_format elsif @not_in_format