Class: RHACK::Google
Constant Summary collapse
- URI =
{ :translate => "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s", :search => "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&hl=ru&q=%s", :detect => "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=%s" }
- Shortcuts =
- Langs =
%w{ af sq am ar hy az eu be bn bh bg my ca chr zh zh-CN zh-TW hr cs da dv nl en eo et tl fi fr gl ka de el gn gu iw hi hu is id iu it ja kn kk km ko ku ky lo lv lt mk ms ml mt mr mn ne no or ps fa pl pt-PT pa ro ru sa sr sd si sk sl es sw sv tg ta tl te th bo tr uk ur uz ug vi }
Class Method Summary collapse
Instance Method Summary collapse
- #detect(text, wait = !block_given?,, &block) ⇒ Object
-
#initialize(service = :search, frame = nil) ⇒ Google
constructor
A new instance of Google.
- #search(text, opts = {}, &block) ⇒ Object
- #translate(text, to, from = nil, &block) ⇒ Object
Constructor Details
#initialize(service = :search, frame = nil) ⇒ Google
Returns a new instance of Google.
107 108 109 |
# File 'lib/rhack/clients/examples.rb', line 107 def initialize(service=:search, frame=nil) super service, frame, :json => true end |
Class Method Details
.search(*args, &block) ⇒ Object
150 |
# File 'lib/rhack/clients/examples.rb', line 150 def self.search(*args, &block) new.search *args, &block end |
.tr(*args, &block) ⇒ Object
151 |
# File 'lib/rhack/clients/examples.rb', line 151 def self.tr(*args, &block) new(:translate).translate *args, &block end |
Instance Method Details
#detect(text, wait = !block_given?,, &block) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/rhack/clients/examples.rb', line 121 def detect(text, wait=!block_given?, &block) text = text.is(String) ? text[0...600] : text[0] uri = URI[:detect] % CGI.escape(text) @f.run(uri, :proc_result => block, :wait => wait) {|page| (data = page.hash.responseData.b) && data.language } end |
#search(text, opts = {}, &block) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/rhack/clients/examples.rb', line 111 def search(text, opts={}, &block) text = "site:#{opts[:site]} #{text}" if opts[:site] uri = URI.search % CGI.escape(text) @f.run(uri, :proc_result => block) {|page| if data = page.hash.responseData.b data.results.map! {|res| [res.unescapedUrl, res.titleNoFormatting, res.content]} end }#.res end |
#translate(text, to, from = nil, &block) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rhack/clients/examples.rb', line 129 def translate(text, to, from=nil, &block) text = text.split_to_blocks(600, :syntax) if !text.is Array if !from if block_given? return detect(text) {|from| yield translate(text, to, from)} else return translate(text, to, detect(text).res) end end res = [] i = 0 text.each_with_index {|b, j| @f.run(URI.translate%[CGI.escape(text[j]), from, to], :proc_result => block, :wait => false) {|page| res[j] = (data = page.hash.responseData.b and data.translatedText) (i += 1) == text.size ? res*"\n" : :skip } } Curl.wait if !block_given? res*"\n" end |