Class: GScraper::Search::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/gscraper/search/query.rb

Direct Known Subclasses

AJAXQuery, WebQuery

Constant Summary collapse

SUB_DOMAIN =

Web Search sub-domain

'www'
DEFAULT_HOST =

Default host to submit queries to

"#{SUB_DOMAIN}.#{Hosts::PRIMARY_DOMAIN}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|query| ... } ⇒ Query

Creates a new query.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :search_host (String) — default: www.google.com

    The host to submit queries to.

  • :query (String)

    The search query.

  • :language (Symbol, String) — default: Languages.native

    The search language.

  • :link (String)

    Search for results which link to the specified URI.

  • :related (String)

    Search for results which relate to the specified URI.

  • :info (String)

    Return information about the specified URI.

  • :site (String)

    Limit results to the specified site.

  • :filetype (String)

    Limit results to those with the specified file-type.

  • :allintitle (Array, String)

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

  • :intitle (String)

    Search for results with the keyword appearing in the title.

  • :allintext (Array, String)

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

  • :intext (String)

    Search for results with the keyword appearing in the text.

  • :allinanchor (Array, String)

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

  • :inanchor (String)

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

  • :exact_phrase (String)

    Search for results containing the specified exact phrase.

  • :with_words (Array, String)

    Search for results containing all of the specified words.

  • :without_words (Array, String)

    Search for results not containing any of the specified words.

  • :numeric_range (Range, Array, String)

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

  • :define (String)

    Search for results containing the definition of the specified keyword.

  • :load_balance (Boolean) — default: false

    Specifies whether to distribute queries accross multiple Google domains.

Yields:

  • (query)

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

Yield Parameters:

  • query (Query)

    The new query.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# 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

Instance Attribute Details

#allintextObject

Search 'allintext' modifier



73
74
75
# File 'lib/gscraper/search/query.rb', line 73

def allintext
  @allintext
end

#allintitleObject

Search 'allintitle' modifier



61
62
63
# File 'lib/gscraper/search/query.rb', line 61

def allintitle
  @allintitle
end

#allinurlObject

Search 'allinurl' modifier



67
68
69
# File 'lib/gscraper/search/query.rb', line 67

def allinurl
  @allinurl
end

#defineObject

Search for results containing the definitions of the keywords



91
92
93
# File 'lib/gscraper/search/query.rb', line 91

def define
  @define
end

#exact_phraseObject

Search for results containing the exact phrase



79
80
81
# File 'lib/gscraper/search/query.rb', line 79

def exact_phrase
  @exact_phrase
end

#filetypeObject

Search 'filetype' modifier



58
59
60
# File 'lib/gscraper/search/query.rb', line 58

def filetype
  @filetype
end

#infoObject

Search 'info' modifier



52
53
54
# File 'lib/gscraper/search/query.rb', line 52

def info
  @info
end

#intextObject

Search 'intext' modifier



76
77
78
# File 'lib/gscraper/search/query.rb', line 76

def intext
  @intext
end

#intitleObject

Search 'intitle' modifier



64
65
66
# File 'lib/gscraper/search/query.rb', line 64

def intitle
  @intitle
end

#inurlObject

Search 'inurl' modifier



70
71
72
# File 'lib/gscraper/search/query.rb', line 70

def inurl
  @inurl
end

#languageObject

The search language



43
44
45
# File 'lib/gscraper/search/query.rb', line 43

def language
  @language
end

Search 'link' modifier



46
47
48
# File 'lib/gscraper/search/query.rb', line 46

def link
  @link
end

#numeric_rangeObject

Search for results containing numbers between the range



88
89
90
# File 'lib/gscraper/search/query.rb', line 88

def numeric_range
  @numeric_range
end

#queryObject

Search query



40
41
42
# File 'lib/gscraper/search/query.rb', line 40

def query
  @query
end

Search 'related' modifier



49
50
51
# File 'lib/gscraper/search/query.rb', line 49

def related
  @related
end

#search_hostString

The host to submit queries to.

Returns:

  • (String)

    The host to submit queries to.

Since:

  • 0.4.0



215
216
217
218
219
220
221
# 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

#siteObject

Search 'site' modifier



55
56
57
# File 'lib/gscraper/search/query.rb', line 55

def site
  @site
end

#with_wordsObject

Search for results with the words



82
83
84
# File 'lib/gscraper/search/query.rb', line 82

def with_words
  @with_words
end

#without_wordsObject

Search for results with-out the words



85
86
87
# File 'lib/gscraper/search/query.rb', line 85

def without_words
  @without_words
end

Instance Method Details

#expressionString

The query expression.

Returns:

  • (String)

    The expression representing the query.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# 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

#format_modifier(value) ⇒ String (protected)

Formats the value for a search modifier.

Parameters:

  • The (Regexp, String)

    value for the search modifier.

Returns:

  • (String)

    The formatted value.



302
303
304
305
306
307
308
309
# 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) ⇒ String (protected)

Formats the value(s) for a search option.

Parameters:

  • The (Array, Regexp, String)

    value(s) for the search modifier.

Returns:

  • (String)

    The formatted value.



320
321
322
# File 'lib/gscraper/search/query.rb', line 320

def format_options(value)
  Array(value).map(&method(:format_modifier)).join(' ')
end