Class: Google::Search
- Inherits:
-
Object
- Object
- Google::Search
- Includes:
- Enumerable
- Defined in:
- lib/google-search/version.rb,
lib/google-search/item/web.rb,
lib/google-search/response.rb,
lib/google-search/item/base.rb,
lib/google-search/item/blog.rb,
lib/google-search/item/book.rb,
lib/google-search/item/news.rb,
lib/google-search/item/image.rb,
lib/google-search/item/local.rb,
lib/google-search/item/video.rb,
lib/google-search/search/web.rb,
lib/google-search/item/patent.rb,
lib/google-search/search/base.rb,
lib/google-search/search/blog.rb,
lib/google-search/search/book.rb,
lib/google-search/search/news.rb,
lib/google-search/search/image.rb,
lib/google-search/search/local.rb,
lib/google-search/search/video.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
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 ++
'http://www.google.com/uds'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
API Key.
-
#language ⇒ Object
Language.
-
#offset ⇒ Object
Offset.
-
#options ⇒ Object
readonly
Additional options.
-
#query ⇒ Object
Query.
-
#sent ⇒ Object
Weither or not a search request has been sent.
-
#size ⇒ Object
Size.
-
#type ⇒ Object
Search type symbol.
-
#version ⇒ Object
Version.
Class Method Summary collapse
-
.json_decode(string) ⇒ Object
Decode JSON string.
-
.size_for(sym) ⇒ Object
Return int for size sym.
-
.url_encode(string) ⇒ Object
Url encode string.
Instance Method Summary collapse
-
#all_items ⇒ Object
(also: #all)
Return all items.
-
#each_item(&block) ⇒ Object
(also: #each)
Iterate each item with block.
-
#each_response(&block) ⇒ Object
Set a response block which is called every time #get_response is called.
-
#get_hash ⇒ Object
Return hash parsed from the raw JSON response.
-
#get_raw ⇒ Object
Return raw JSON response string.
-
#get_response ⇒ Object
(also: #response)
Return Response object wrapping the JSON response hash.
-
#get_uri ⇒ Object
Return uri.
-
#get_uri_params ⇒ Object
:nodoc:.
-
#initialize(options = {}) {|_self| ... } ⇒ Search
constructor
Initialize search type with options.
-
#next ⇒ Object
Prepare for next request.
-
#validate(meth, &block) ⇒ Object
:nodoc:.
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.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/google-search/search/base.rb', line 79 def initialize = {}, &block @type = self.class.to_s.split('::').last.downcase.to_sym @version = .delete(:version) || 1.0 @offset = .delete(:offset) || 0 @size = .delete(:size) || :large @language = .delete(:language) || :en @query = .delete(:query) @api_key = .delete(:api_key) || :notsupplied @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_key ⇒ Object
API Key. Defaults to :notsupplied
56 57 58 |
# File 'lib/google-search/search/base.rb', line 56 def api_key @api_key end |
#language ⇒ Object
Language. Defaults to :en
41 42 43 |
# File 'lib/google-search/search/base.rb', line 41 def language @language end |
#offset ⇒ Object
Offset. Defaults to 0
36 37 38 |
# File 'lib/google-search/search/base.rb', line 36 def offset @offset end |
#options ⇒ Object (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 end |
#query ⇒ Object
Query. Defaults to nil
51 52 53 |
# File 'lib/google-search/search/base.rb', line 51 def query @query end |
#sent ⇒ Object
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 |
#size ⇒ Object
Size. Defaults to :large
- :small = 4
- :large = 8
65 66 67 |
# File 'lib/google-search/search/base.rb', line 65 def size @size end |
#type ⇒ Object
Search type symbol.
31 32 33 |
# File 'lib/google-search/search/base.rb', line 31 def type @type end |
#version ⇒ Object
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_items ⇒ Object 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_hash ⇒ Object
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_raw ⇒ Object
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_response ⇒ Object 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_uri ⇒ Object
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 + .to_a). map { |key, value| "#{key}=#{Search.url_encode(value)}" unless value.nil? }.compact.join('&') end |
#get_uri_params ⇒ Object
: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 |
#next ⇒ Object
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:
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 |