Class: Google::Search

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/google-search/version.rb,
lib/google-search/response.rb,
lib/google-search/item/web.rb,
lib/google-search/item/book.rb,
lib/google-search/item/blog.rb,
lib/google-search/item/news.rb,
lib/google-search/item/base.rb,
lib/google-search/search/web.rb,
lib/google-search/item/local.rb,
lib/google-search/item/image.rb,
lib/google-search/item/video.rb,
lib/google-search/search/blog.rb,
lib/google-search/search/book.rb,
lib/google-search/search/news.rb,
lib/google-search/search/base.rb,
lib/google-search/item/patent.rb,
lib/google-search/search/video.rb,
lib/google-search/search/local.rb,
lib/google-search/search/image.rb,
lib/google-search/search/patent.rb,
lib/google-search/search/mixins/filter.rb,
lib/google-search/search/mixins/order_by.rb,
lib/google-search/search/mixins/safety_level.rb

Direct Known Subclasses

Blog, Book, Image, Local, News, Patent, Video, Web

Defined Under Namespace

Modules: Filter, OrderBy, SafetyLevel Classes: Blog, Book, Error, Image, Item, Local, News, Patent, Response, Video, Web

Constant Summary collapse

VERSION =
'1.0.2'
URI =

– Constants ++

'https://www.google.com/uds'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Search

Initialize search type with options. Optionally a block may be passed, and the Search instance will be yielded to it.

Yields:

  • (_self)

Yield Parameters:

Raises:


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/google-search/search/base.rb', line 79

def initialize options = {}, &block
  @type = self.class.to_s.split('::').last.downcase.to_sym
  @version = options.delete(:version) || 1.0
  @offset = options.delete(:offset) || 0
  @size = options.delete(:size) || :large
  @language = options.delete(:language) || :en
  @query = options.delete(:query)
  @api_key = options.delete(:api_key) || :notsupplied
  @options = options
  raise Error, 'Do not initialize Google::Search; Use a subclass such as Google::Search::Web' if @type == :search
  yield self if block
end

Instance Attribute Details

#api_keyObject

API Key. Defaults to :notsupplied


56
57
58
# File 'lib/google-search/search/base.rb', line 56

def api_key
  @api_key
end

#languageObject

Language. Defaults to :en


41
42
43
# File 'lib/google-search/search/base.rb', line 41

def language
  @language
end

#offsetObject

Offset. Defaults to 0


36
37
38
# File 'lib/google-search/search/base.rb', line 36

def offset
  @offset
end

#optionsObject (readonly)

Additional options. All those listed above are deleted. The remaining represent query string key / value pairs.


72
73
74
# File 'lib/google-search/search/base.rb', line 72

def options
  @options
end

#queryObject

Query. Defaults to nil


51
52
53
# File 'lib/google-search/search/base.rb', line 51

def query
  @query
end

#sentObject

Weither or not a search request has been sent.


46
47
48
# File 'lib/google-search/search/base.rb', line 46

def sent
  @sent
end

#sizeObject

Size. Defaults to :large

- :small = 4
- :large = 8

65
66
67
# File 'lib/google-search/search/base.rb', line 65

def size
  @size
end

#typeObject

Search type symbol.


31
32
33
# File 'lib/google-search/search/base.rb', line 31

def type
  @type
end

#versionObject

Version. Defaults to 1.0


26
27
28
# File 'lib/google-search/search/base.rb', line 26

def version
  @version
end

Class Method Details

.json_decode(string) ⇒ Object

Decode JSON string.


198
199
200
# File 'lib/google-search/search/base.rb', line 198

def self.json_decode string
  JSON.parse string
end

.size_for(sym) ⇒ Object

Return int for size sym.


183
184
185
186
# File 'lib/google-search/search/base.rb', line 183

def self.size_for sym
  { :small => 4,
    :large => 8 }[sym]
end

.url_encode(string) ⇒ Object

Url encode string.


205
206
207
208
209
# File 'lib/google-search/search/base.rb', line 205

def self.url_encode string
  string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/) {
    '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
  }.tr(' ', '+')
end

Instance Method Details

#all_itemsObject Also known as: all

Return all items.


115
116
117
# File 'lib/google-search/search/base.rb', line 115

def all_items
  select { true }
end

#each_item(&block) ⇒ Object Also known as: each

Iterate each item with block.


103
104
105
106
107
108
109
# File 'lib/google-search/search/base.rb', line 103

def each_item &block
  response = self.next.response
  if response.valid?
    response.each { |item| yield item }
    each_item &block
  end
end

#each_response(&block) ⇒ Object

Set a response block which is called every time #get_response is called. Useful for reporting etc.


96
97
98
# File 'lib/google-search/search/base.rb', line 96

def each_response &block
  @each_response = block
end

#get_hashObject

Return hash parsed from the raw JSON response.


161
162
163
# File 'lib/google-search/search/base.rb', line 161

def get_hash
  Search.json_decode get_raw
end

#get_rawObject

Return raw JSON response string.


153
154
155
156
# File 'lib/google-search/search/base.rb', line 153

def get_raw
  @sent = true
  open(get_uri).read
end

#get_responseObject Also known as: response

Return Response object wrapping the JSON response hash.


169
170
171
172
173
174
175
176
177
# File 'lib/google-search/search/base.rb', line 169

def get_response
  raw = get_raw
  hash = Search.json_decode raw
  hash['responseSize'] = size
  response = Response.new hash
  response.raw = raw
  @each_response.call response if @each_response
  response
end

#get_uriObject

Return uri.


123
124
125
126
127
# File 'lib/google-search/search/base.rb', line 123

def get_uri
  URI + "/G#{@type}Search?" + 
    (get_uri_params + options.to_a).
      map { |key, value| "#{key}=#{Search.url_encode(value)}" unless value.nil? }.compact.join('&')
end

#get_uri_paramsObject

:nodoc:


131
132
133
134
135
136
137
138
139
140
# File 'lib/google-search/search/base.rb', line 131

def get_uri_params
  validate(:query) { |query| query.respond_to?(:to_str) && !query.to_str.empty? }
  validate(:version) { |version| Numeric === version }
  [[:start, offset],
  [:rsz, size],
  [:hl, language],
  [:key, api_key],
  [:v, version],
  [:q, query]]
end

#nextObject

Prepare for next request.


145
146
147
148
# File 'lib/google-search/search/base.rb', line 145

def next
  @offset += Search.size_for(size) if sent
  self
end

#validate(meth, &block) ⇒ Object

:nodoc:

Raises:


190
191
192
193
# File 'lib/google-search/search/base.rb', line 190

def validate meth, &block
  value = send meth
  raise Error, "invalid #{type} #{meth} #{value.inspect}", caller unless yield value
end