class GScraper::Search::Query

Constants

DEFAULT_HOST

Default host to submit queries to

SUB_DOMAIN

Web Search sub-domain

Attributes

allintext[RW]

Search ‘allintext’ modifier

allintitle[RW]

Search ‘allintitle’ modifier

allinurl[RW]

Search ‘allinurl’ modifier

define[RW]

Search for results containing the definitions of the keywords

exact_phrase[RW]

Search for results containing the exact phrase

filetype[RW]

Search ‘filetype’ modifier

info[RW]

Search ‘info’ modifier

intext[RW]

Search ‘intext’ modifier

intitle[RW]

Search ‘intitle’ modifier

inurl[RW]

Search ‘inurl’ modifier

language[RW]

The search language

numeric_range[RW]

Search for results containing numbers between the range

query[RW]

Search query

search_host[W]

The host to submit queries to

site[RW]

Search ‘site’ modifier

with_words[RW]

Search for results with the words

without_words[RW]

Search for results with-out the words

Public Class Methods

new(options={}) { |self| ... } click to toggle source

Creates a new query.

@param [Hash] options

Additional options.

@option options [String] :search_host (www.google.com)

The host to submit queries to.

@option options [String] :query

The search query.

@option options [Symbol, String] :language (Languages.native)

The search language.

@option options [String] :link

Search for results which link to the specified URI.

@option options [String] :related

Search for results which relate to the specified URI.

@option options [String] :info

Return information about the specified URI.

@option options [String] :site

Limit results to the specified site.

@option options [String] :filetype

Limit results to those with the specified file-type.

@option options [Array, String] :allintitle

Search for results with all of the keywords appearing in the
title.

@option options [String] :intitle

Search for results with the keyword appearing in the title.

@option options [Array, String] :allintext

Search for results with all of the keywords appearing in the text.

@option options [String] :intext

Search for results with the keyword appearing in the text.

@option options [Array, String] :allinanchor

Search for results with all of the keywords appearing in the
text of links.

@option options [String] :inanchor

Search for results with the keyword appearing in the text of
links.

@option options [String] :exact_phrase

Search for results containing the specified exact phrase.

@option options [Array, String] :with_words

Search for results containing all of the specified words.

@option options [Array, String] :without_words

Search for results not containing any of the specified words.

@option options [Range, Array, String] :numeric_range

Search for results contain numbers that fall within the
specified Range.

@option options [String] :define

Search for results containing the definition of the specified
keyword.

@option options [Boolean] :load_balance (false)

Specifies whether to distribute queries accross multiple Google
domains.

@yield [query]

If a block is given, it will be passed the new query.

@yieldparam [Query] query

The new query.

@return [Query]

The new query.
# File lib/gscraper/search/query.rb, line 174
def initialize(options={})
  @search_host = options.fetch(:search_host,DEFAULT_HOST)

  @query    = options[:query]
  @language = options.fetch(:language,Languages.native)

  @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]
  @allinanchor = options[:allinanchor]
  @inanchor    = options[:inanchor]

  @exact_phrase  = options[:exact_phrase]
  @with_words    = options[:with_words]
  @without_words = options[:without_words]

  @numeric_range = options[:numeric_range]
  @define        = options[:define]

  @load_balance = options.fetch(:load_balance,false)

  yield self if block_given?
end

Public Instance Methods

expression() click to toggle source

The query expression.

@return [String]

The expression representing the query.
# File lib/gscraper/search/query.rb, line 229
def expression
  expr = []

  append_modifier = lambda { |name|
    modifier = format_modifier(instance_variable_get("@#{name}"))

    expr << "#{name}:#{modifier}" unless modifier.empty?
  }

  append_options = lambda { |name|
    ops = format_options(instance_variable_get("@#{name}"))

    expr << "#{name}:#{ops}" unless ops.empty?
  }

  expr << @query if @query

  append_modifier.call(:link)
  append_modifier.call(:related)
  append_modifier.call(:info)
  append_modifier.call(:site)
  append_modifier.call(:filetype)

  append_options.call(:allintitle)
  append_modifier.call(:intitle)
  append_options.call(:allinurl)
  append_modifier.call(:inurl)
  append_options.call(:allintext)
  append_modifier.call(:intext)
  append_options.call(:allinanchor)
  append_modifier.call(:inanchor)

  append_modifier.call(:define)

  if @exact_phrase
    expr << "\"#{@exact_phrase}\""
  end

  case @with_words
  when String
    expr << @with_words
  when Enumerable
    expr << @with_words.join(' OR ')
  end

  case @without_words
  when String
    expr << @without_words
  when Enumerable
    expr << @without_words.map { |word| "-#{word}" }.join(' ')
  end

  case @numeric_range
  when String
    expr << @numeric_range
  when Range, Array
    expr << "#{@numeric_range.first}..#{@numeric_range.last}"
  end

  return expr.join(' ')
end
search_host() click to toggle source

The host to submit queries to.

@return [String]

The host to submit queries to.

@since 0.4.0

# File lib/gscraper/search/query.rb, line 215
def search_host
  if @load_balance
    Hosts::DOMAINS[rand(Hosts::DOMAINS.length)]
  else
    @search_host
  end
end

Protected Instance Methods

format_modifier(value) click to toggle source

Formats the value for a search modifier.

@param [Regexp, String]

The value for the search modifier.

@return [String]

The formatted value.
# File lib/gscraper/search/query.rb, line 302
def format_modifier(value)
  case value
  when Range
    value.source
  else
    value.to_s
  end
end
format_options(value) click to toggle source

Formats the value(s) for a search option.

@param [Array, Regexp, String]

The value(s) for the search modifier.

@return [String]

The formatted value.
# File lib/gscraper/search/query.rb, line 320
def format_options(value)
  Array(value).map(&method(:format_modifier)).join(' ')
end