Class: RubyWebSearch::Google::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-web-search.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

SEARCH_BASE_URLS =
{  :web    => "http://ajax.googleapis.com/ajax/services/search/web",
  :local  => "http://ajax.googleapis.com/ajax/services/search/local",
  :video  => "http://ajax.googleapis.com/ajax/services/search/video",
  :blog   => "http://ajax.googleapis.com/ajax/services/search/blogs",
  :news   =>  "http://ajax.googleapis.com/ajax/services/search/news",
  :book   => "http://ajax.googleapis.com/ajax/services/search/books",
  :image  => "http://ajax.googleapis.com/ajax/services/search/images",
  :patent => "http://ajax.googleapis.com/ajax/services/search/patent"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Query

You can overwrite the query building process by passing the request url to use.

Params

query<String>
start_index<Integer>
size<Integer> number of results default: 4
filter
country_code<String> 2 letters language code for the country you want
    to limit to
language_code<String>  (Web only)
safe_search<String>    active, moderate or off. Default: active (web only)
custom_search_engine_id<String> optional argument supplying the unique id for
      the Custom Search Engine that should be used for the request (e.g., 000455696194071821846:reviews).
      (web only)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby-web-search.rb', line 63

def initialize(options={})
  if options[:custom_request_url]
    @custom_request_url = options[:request_url]
  else
    @query = options[:query]
    raise Google::Query::Error, "You need to pass a query" unless @query
    @cursor                   = options[:start_index] || 0
    @result_size              = options[:result_size]
    @filter                   = options[:filter]
    @type                     = options[:type]        || :web
    @country_code             = options[:country_code]
    @language_code            = options[:language_code] ? "lang_#{options[:language_code]}" : nil
    @safe_search              = options[:safe_search]
    @custom_search_engine_id  = options[:custom_search_engine_id]
    @version                  = options[:version] || "1.0"
    @referer                  = options[:referer] ||  "http://github.com/mattetti/"
    @size                     = options[:size] || 4
    @global                   = options[:global]
    @result_size              = "large" if size > 4  # increase the result set size to avoid making too many requests
    @size                     = 8 if (@result_size == "large" && size < 8)
  end
  @response ||= Response.new(:query => (query || custom_request_url), :size => size)
end

Instance Attribute Details

#country_codeObject

Returns the value of attribute country_code.



31
32
33
# File 'lib/ruby-web-search.rb', line 31

def country_code
  @country_code
end

#cursorObject

Returns the value of attribute cursor.



33
34
35
# File 'lib/ruby-web-search.rb', line 33

def cursor
  @cursor
end

#custom_request_urlObject

Returns the value of attribute custom_request_url.



33
34
35
# File 'lib/ruby-web-search.rb', line 33

def custom_request_url
  @custom_request_url
end

#custom_search_engine_idObject

Returns the value of attribute custom_search_engine_id.



32
33
34
# File 'lib/ruby-web-search.rb', line 32

def custom_search_engine_id
  @custom_search_engine_id
end

#filterObject

Returns the value of attribute filter.



31
32
33
# File 'lib/ruby-web-search.rb', line 31

def filter
  @filter
end

#globalObject

Returns the value of attribute global.



31
32
33
# File 'lib/ruby-web-search.rb', line 31

def global
  @global
end

#language_codeObject

Returns the value of attribute language_code.



31
32
33
# File 'lib/ruby-web-search.rb', line 31

def language_code
  @language_code
end

#queryObject

Returns the value of attribute query.



31
32
33
# File 'lib/ruby-web-search.rb', line 31

def query
  @query
end

#refererObject

Returns the value of attribute referer.



32
33
34
# File 'lib/ruby-web-search.rb', line 32

def referer
  @referer
end

#request_urlObject

Returns the value of attribute request_url.



32
33
34
# File 'lib/ruby-web-search.rb', line 32

def request_url
  @request_url
end

#responseObject

Returns the value of attribute response.



33
34
35
# File 'lib/ruby-web-search.rb', line 33

def response
  @response
end

#result_sizeObject

Returns the value of attribute result_size.



31
32
33
# File 'lib/ruby-web-search.rb', line 31

def result_size
  @result_size
end

#safe_searchObject

Returns the value of attribute safe_search.



32
33
34
# File 'lib/ruby-web-search.rb', line 32

def safe_search
  @safe_search
end

#sizeObject

Returns the value of attribute size.



33
34
35
# File 'lib/ruby-web-search.rb', line 33

def size
  @size
end

#start_indexObject

Returns the value of attribute start_index.



31
32
33
# File 'lib/ruby-web-search.rb', line 31

def start_index
  @start_index
end

#typeObject

Returns the value of attribute type.



32
33
34
# File 'lib/ruby-web-search.rb', line 32

def type
  @type
end

#versionObject

Returns the value of attribute version.



32
33
34
# File 'lib/ruby-web-search.rb', line 32

def version
  @version
end

Instance Method Details

#build_requestObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby-web-search.rb', line 87

def build_request
  if custom_request_url 
    custom_request_url
  else
    @request_url = "#{SEARCH_BASE_URLS[type]}?v=#{version}&q=#{CGI.escape(query)}"
    @request_url << "&rsz=#{result_size}" if result_size
    @request_url << "&start=#{cursor}" if cursor > 0
    @request_url << "&lr=#{language_code}" if language_code
    @request_url << "&hl=#{country_code}" if country_code
    @request_url << "&gl=#{global}" if global

    puts request_url if $RUBY_WEB_SEARCH_DEBUG
    request_url
  end
end

#build_requestsObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ruby-web-search.rb', line 103

def build_requests
  if custom_request_url 
    requests = [custom_request_url]
  else
    requests = []
    # create an array of requests based on the fact that google limits
    # us to 8 responses per request but let us use a cursor
    (size / 8.to_f).ceil.times do |n|
      url = "#{SEARCH_BASE_URLS[type]}?v=#{version}&q=#{CGI.escape(query)}"
      url << "&rsz=#{result_size}" if result_size
      url << "&lr=#{language_code}" if language_code
      url << "&hl=#{country_code}" if country_code
      url << "&gl=#{global}" if global
      url << "&start=#{cursor}"
      @cursor += 8
      requests << url
    end
    
    puts requests.inspect if $RUBY_WEB_SEARCH_DEBUG
    requests
  end
end

#executeObject

Makes the request to Google if a larger set was requested than what is returned, more requests are made until the correct amount is available



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ruby-web-search.rb', line 148

def execute
  threads = build_requests.map do |req|
    Thread.new do
       curl_request = ::Curl::Easy.new(req){ |curl| curl.headers["Referer"] = referer }
       curl_request.perform
       JSON.load(curl_request.body_str)
    end
  end
  threads.each do |t|
    response.process(t.value)
  end
  response.limit(size)
end

#execute_unthreadedObject

Makes the request to Google if a larger set was requested than what is returned, more requests are made until the correct amount is available



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ruby-web-search.rb', line 129

def execute_unthreaded
  @curl_request ||= ::Curl::Easy.new(){ |curl| curl.headers["Referer"] = referer }
  @curl_request.url = build_request
  @curl_request.perform
  results = JSON.load(@curl_request.body_str) 
  
  response.process(results)
  @cursor = response.results.size - 1
  if ((cursor + 1) < size && custom_request_url.nil?)
    puts "cursor: #{cursor} requested results size: #{size}" if $RUBY_WEB_SEARCH_DEBUG
    execute_unthreaded
  else
    response.limit(size)
  end
end