Skip to content

Instantly share code, notes, and snippets.

@kwappa
Created Aug 6, 2010
Embed
What would you like to do?
# -*- coding: utf-8 -*-
require 'cgi'
require 'rexml/document'
require 'open-uri'
class Suggest
attr_reader :response
def initialize
@query_base = "http://www.google.co.jp/complete/search?output=toolbar&hl=ja&q=%s"
self
end
def request word
@response = open(sprintf(@query_base, CGI::escape(word))).read
self
end
def words
doc = REXML::Document.new @response
suggested_words = []
doc.get_elements('//toplevel/CompleteSuggestion').each do |e|
suggested_words << e.elements['suggestion'].attributes['data']
end
suggested_words
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment