Class: GScraper::Search::Query
- Inherits:
-
Object
- Object
- GScraper::Search::Query
- Defined in:
- lib/gscraper/search/query.rb
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
-
#allintext ⇒ Object
Search 'allintext' modifier.
-
#allintitle ⇒ Object
Search 'allintitle' modifier.
-
#allinurl ⇒ Object
Search 'allinurl' modifier.
-
#define ⇒ Object
Search for results containing the definitions of the keywords.
-
#exact_phrase ⇒ Object
Search for results containing the exact phrase.
-
#filetype ⇒ Object
Search 'filetype' modifier.
-
#info ⇒ Object
Search 'info' modifier.
-
#intext ⇒ Object
Search 'intext' modifier.
-
#intitle ⇒ Object
Search 'intitle' modifier.
-
#inurl ⇒ Object
Search 'inurl' modifier.
-
#language ⇒ Object
The search language.
-
#link ⇒ Object
Search 'link' modifier.
-
#numeric_range ⇒ Object
Search for results containing numbers between the range.
-
#query ⇒ Object
Search query.
-
#related ⇒ Object
Search 'related' modifier.
-
#search_host ⇒ String
The host to submit queries to.
-
#site ⇒ Object
Search 'site' modifier.
-
#with_words ⇒ Object
Search for results with the words.
-
#without_words ⇒ Object
Search for results with-out the words.
Instance Method Summary collapse
-
#expression ⇒ String
The query expression.
-
#format_modifier(value) ⇒ String
protected
Formats the value for a search modifier.
-
#format_options(value) ⇒ String
protected
Formats the value(s) for a search option.
-
#initialize(options = {}) {|query| ... } ⇒ Query
constructor
Creates a new query.
Constructor Details
#initialize(options = {}) {|query| ... } ⇒ Query
Creates a 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(={}) @search_host = .fetch(:search_host,DEFAULT_HOST) @query = [:query] @language = .fetch(:language,Languages.native) @link = [:link] @related = [:related] @info = [:info] @site = [:site] @filetype = [:filetype] @allintitle = [:allintitle] @intitle = [:intitle] @allinurl = [:allinurl] @inurl = [:inurl] @allintext = [:allintext] @intext = [:intext] @allinanchor = [:allinanchor] @inanchor = [:inanchor] @exact_phrase = [:exact_phrase] @with_words = [:with_words] @without_words = [:without_words] @numeric_range = [:numeric_range] @define = [:define] @load_balance = .fetch(:load_balance,false) yield self if block_given? end |
Instance Attribute Details
#allintext ⇒ Object
Search 'allintext' modifier
73 74 75 |
# File 'lib/gscraper/search/query.rb', line 73 def allintext @allintext end |
#allintitle ⇒ Object
Search 'allintitle' modifier
61 62 63 |
# File 'lib/gscraper/search/query.rb', line 61 def allintitle @allintitle end |
#allinurl ⇒ Object
Search 'allinurl' modifier
67 68 69 |
# File 'lib/gscraper/search/query.rb', line 67 def allinurl @allinurl end |
#define ⇒ Object
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_phrase ⇒ Object
Search for results containing the exact phrase
79 80 81 |
# File 'lib/gscraper/search/query.rb', line 79 def exact_phrase @exact_phrase end |
#filetype ⇒ Object
Search 'filetype' modifier
58 59 60 |
# File 'lib/gscraper/search/query.rb', line 58 def filetype @filetype end |
#info ⇒ Object
Search 'info' modifier
52 53 54 |
# File 'lib/gscraper/search/query.rb', line 52 def info @info end |
#intext ⇒ Object
Search 'intext' modifier
76 77 78 |
# File 'lib/gscraper/search/query.rb', line 76 def intext @intext end |
#intitle ⇒ Object
Search 'intitle' modifier
64 65 66 |
# File 'lib/gscraper/search/query.rb', line 64 def intitle @intitle end |
#inurl ⇒ Object
Search 'inurl' modifier
70 71 72 |
# File 'lib/gscraper/search/query.rb', line 70 def inurl @inurl end |
#language ⇒ Object
The search language
43 44 45 |
# File 'lib/gscraper/search/query.rb', line 43 def language @language end |
#link ⇒ Object
Search 'link' modifier
46 47 48 |
# File 'lib/gscraper/search/query.rb', line 46 def link @link end |
#numeric_range ⇒ Object
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 |
#query ⇒ Object
Search query
40 41 42 |
# File 'lib/gscraper/search/query.rb', line 40 def query @query end |
#related ⇒ Object
Search 'related' modifier
49 50 51 |
# File 'lib/gscraper/search/query.rb', line 49 def @related end |
#search_host ⇒ String
The host to submit queries to.
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 |
#site ⇒ Object
Search 'site' modifier
55 56 57 |
# File 'lib/gscraper/search/query.rb', line 55 def site @site end |
#with_words ⇒ Object
Search for results with the words
82 83 84 |
# File 'lib/gscraper/search/query.rb', line 82 def with_words @with_words end |
#without_words ⇒ Object
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
#expression ⇒ String
The query expression.
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? } = lambda { |name| ops = (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) .call(:allintitle) append_modifier.call(:intitle) .call(:allinurl) append_modifier.call(:inurl) .call(:allintext) append_modifier.call(:intext) .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.
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.
320 321 322 |
# File 'lib/gscraper/search/query.rb', line 320 def (value) Array(value).map(&method(:format_modifier)).join(' ') end |