google_suggest 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/google_suggest.rb +77 -0
- data/spec/google_suggest-spec.rb +127 -0
- data/spec/sample_ja.xml +1 -0
- data/spec/sample_us.xml +12 -0
- data/spec/spec_helper.rb +8 -0
- metadata +70 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'net/http'
|
6
|
+
|
7
|
+
class GoogleSuggest
|
8
|
+
class Configure
|
9
|
+
attr_accessor :home_language
|
10
|
+
attr_accessor :proxy
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@home_language = 'en'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_accessor :home_language
|
18
|
+
attr_accessor :proxy
|
19
|
+
|
20
|
+
@@configure = Configure.new
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def configure
|
24
|
+
yield @@configure if block_given?
|
25
|
+
@@configure
|
26
|
+
end
|
27
|
+
|
28
|
+
def suggest_for(keyword)
|
29
|
+
self.new.suggest_for(keyword)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize
|
34
|
+
@home_language = @@configure.home_language
|
35
|
+
@proxy = @@configure.proxy
|
36
|
+
end
|
37
|
+
|
38
|
+
def suggest_for(keyword)
|
39
|
+
query = {:output => 'toolbar',
|
40
|
+
:hl => self.home_language,
|
41
|
+
:q => URI.encode(keyword)}
|
42
|
+
res = http_get('/complete/search', query)
|
43
|
+
xml = Nokogiri::XML(res.body.to_s)
|
44
|
+
suggestions = []
|
45
|
+
xml.css('toplevel CompleteSuggestion').each do |node|
|
46
|
+
suggest = {}
|
47
|
+
node.children.each do |child|
|
48
|
+
suggest[child.name] = (child['data'] || child['int'].to_i)
|
49
|
+
end
|
50
|
+
if suggest['suggestion'] and not suggest['suggestion'].valid_encoding?
|
51
|
+
suggest['suggestion'].force_encoding('Shift_JIS').encode!('UTF-8')
|
52
|
+
end
|
53
|
+
suggestions << suggest
|
54
|
+
end
|
55
|
+
return suggestions
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
GOOGLE_HOST = 'www.google.com'
|
60
|
+
|
61
|
+
def http
|
62
|
+
if @proxy
|
63
|
+
proxy_url = URI.parse(@proxy)
|
64
|
+
http_class = Net::HTTP.Proxy(proxy_url.host, proxy_url.port)
|
65
|
+
else
|
66
|
+
http_class = Net::HTTP
|
67
|
+
end
|
68
|
+
http_class.new(GOOGLE_HOST)
|
69
|
+
end
|
70
|
+
|
71
|
+
def http_get(path, query)
|
72
|
+
path = path + '?' + query.map{|k,v| "#{k}=#{v}"}.join('&')
|
73
|
+
req = Net::HTTP::Get.new(path)
|
74
|
+
http.request(req)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
3
|
+
|
4
|
+
describe GoogleSuggest do
|
5
|
+
context "default settiong" do
|
6
|
+
subject { GoogleSuggest.new }
|
7
|
+
|
8
|
+
its(:home_language) { should be_eql 'en' }
|
9
|
+
its(:proxy) { should be_nil }
|
10
|
+
end
|
11
|
+
|
12
|
+
context "Class Methods" do
|
13
|
+
before do
|
14
|
+
google_suggest = GoogleSuggest.new
|
15
|
+
res = Object.new
|
16
|
+
res.stub!(:body) do
|
17
|
+
doc = nil
|
18
|
+
File.open(File.join(File.dirname(__FILE__), 'sample_us.xml')) do |f|
|
19
|
+
doc = f.read
|
20
|
+
end
|
21
|
+
doc
|
22
|
+
end
|
23
|
+
google_suggest.stub!(:http_get) { res }
|
24
|
+
suggestions = google_suggest.suggest_for('google')
|
25
|
+
GoogleSuggest.stub!(:new).and_return { mock('google_suggest', :suggest_for => suggestions) }
|
26
|
+
|
27
|
+
@suggestions = GoogleSuggest.suggest_for('google')
|
28
|
+
end
|
29
|
+
|
30
|
+
it do
|
31
|
+
@suggestions.size.should be 10
|
32
|
+
end
|
33
|
+
it do
|
34
|
+
@suggestions.should be_all do
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe GoogleSuggest::Configure do
|
40
|
+
context "#configure called without block" do
|
41
|
+
before do
|
42
|
+
@configure = GoogleSuggest.configure
|
43
|
+
@configure.home_language = 'ja'
|
44
|
+
@configure.proxy = 'http://proxy.example.com'
|
45
|
+
end
|
46
|
+
subject { GoogleSuggest.new }
|
47
|
+
its(:home_language) { should be_eql 'ja' }
|
48
|
+
its(:proxy) { should be_eql 'http://proxy.example.com' }
|
49
|
+
end
|
50
|
+
context "#configure called and given block" do
|
51
|
+
before do
|
52
|
+
GoogleSuggest.configure do |c|
|
53
|
+
c.home_language = 'us'
|
54
|
+
c.proxy = 'http://proxy.example.com'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
subject { GoogleSuggest.new }
|
58
|
+
its(:home_language) { should be_eql 'us' }
|
59
|
+
its(:proxy) { should be_eql 'http://proxy.example.com' }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "#suggest_from method" do
|
64
|
+
before :all do
|
65
|
+
GoogleSuggest.configure do |c|
|
66
|
+
c.home_language = 'us'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
before do
|
71
|
+
@google_suggest = GoogleSuggest.new
|
72
|
+
res = Object.new
|
73
|
+
res.stub!(:body) do
|
74
|
+
doc = nil
|
75
|
+
File.open(File.join(File.dirname(__FILE__), 'sample_us.xml')) do |f|
|
76
|
+
doc = f.read
|
77
|
+
end
|
78
|
+
doc
|
79
|
+
end
|
80
|
+
@google_suggest.stub!(:http_get) { res }
|
81
|
+
end
|
82
|
+
|
83
|
+
it do
|
84
|
+
suggestions = @google_suggest.suggest_for 'google'
|
85
|
+
suggestions.size.should == 10
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'all suggestions should have the value of the key \'suggest\' 'do
|
89
|
+
suggestions = @google_suggest.suggest_for 'google'
|
90
|
+
suggestions.should be_all do |suggest|
|
91
|
+
not suggest['suggest'].nil?
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
it 'all suggestions should have the value of the key \'num_queries\' 'do
|
97
|
+
suggestions = @google_suggest.suggest_for 'google'
|
98
|
+
suggestions.should be_all do |suggest|
|
99
|
+
not suggest['num_queries'].nil?
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
describe "#suggest_for with home language 'ja'" do
|
104
|
+
before :all do
|
105
|
+
GoogleSuggest.configure do |c|
|
106
|
+
c.home_language = 'ja'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
before do
|
111
|
+
@google_suggest = GoogleSuggest.new
|
112
|
+
res = Object.new
|
113
|
+
res.stub!(:body) do
|
114
|
+
doc = nil
|
115
|
+
File.open(File.join(File.dirname(__FILE__), 'sample_ja.xml')) do |f|
|
116
|
+
doc = f.read
|
117
|
+
end
|
118
|
+
doc
|
119
|
+
end
|
120
|
+
@google_suggest.stub!(:http_get) { res }
|
121
|
+
end
|
122
|
+
subject {@google_suggest.suggest_for('グーグル').shift['suggestion']}
|
123
|
+
|
124
|
+
its(:encoding) { should be Encoding.find("UTF-8") }
|
125
|
+
it { should be_valid_encoding }
|
126
|
+
end
|
127
|
+
end
|
data/spec/sample_ja.xml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
<?xml version="1.0"?><toplevel><CompleteSuggestion><suggestion data="グーグル"/><num_queries int="13000000"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグルマップ"/><num_queries int="293000000"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグルアース"/><num_queries int="183000000"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグルクローム"/><num_queries int="782000"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグルカレンダー"/><num_queries int="9830000"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグル翻訳"/><num_queries int="5140000"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグルショッピング"/><num_queries int="21900000"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグルスカラー"/><num_queries int="91600"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグルメール"/><num_queries int="62700000"/></CompleteSuggestion><CompleteSuggestion><suggestion data="グーグルドキュメント"/><num_queries int="2260000"/></CompleteSuggestion></toplevel>
|
data/spec/sample_us.xml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
<?xml version="1.0"?><toplevel>
|
2
|
+
<CompleteSuggestion><suggestion data="google ebook store"/></CompleteSuggestion>
|
3
|
+
<CompleteSuggestion><suggestion data="google chrome os"/></CompleteSuggestion>
|
4
|
+
<CompleteSuggestion><suggestion data="google earth"/><num_queries int="113000000"/></CompleteSuggestion>
|
5
|
+
<CompleteSuggestion><suggestion data="google 翻訳"/><num_queries int="5290000"/></CompleteSuggestion>
|
6
|
+
<CompleteSuggestion><suggestion data="google chrome"/><num_queries int="69000000"/></CompleteSuggestion>
|
7
|
+
<CompleteSuggestion><suggestion data="google apps"/><num_queries int="102000000"/></CompleteSuggestion>
|
8
|
+
<CompleteSuggestion><suggestion data="google カレンダー"/><num_queries int="155000000"/></CompleteSuggestion>
|
9
|
+
<CompleteSuggestion><suggestion data="google scholar"/><num_queries int="20600000"/></CompleteSuggestion>
|
10
|
+
<CompleteSuggestion><suggestion data="google usa"/><num_queries int="215000000"/></CompleteSuggestion>
|
11
|
+
<CompleteSuggestion><suggestion data="googlemap"/><num_queries int="84800000"/></CompleteSuggestion>
|
12
|
+
</toplevel>%
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_suggest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tatsuya Sato
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-05-17 00:00:00 +09:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: satoryu.1981@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/google_suggest.rb
|
31
|
+
- spec/sample_ja.xml
|
32
|
+
- spec/google_suggest-spec.rb
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
- spec/sample_us.xml
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/satoryu/google_suggest/
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
segments:
|
50
|
+
- 1
|
51
|
+
- 8
|
52
|
+
- 7
|
53
|
+
version: 1.8.7
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.7
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: A gem which allows us to retrieve suggest words from Google in your Ruby Code.
|
69
|
+
test_files: []
|
70
|
+
|