google_suggest 0.1.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +28 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +38 -17
- data/README.md +10 -7
- data/google_suggest.gemspec +5 -6
- data/lib/google_suggest.rb +23 -23
- data/lib/google_suggest/region.rb +207 -202
- data/lib/google_suggest/version.rb +1 -1
- data/spec/google_suggest/region_spec.rb +16 -2
- data/spec/google_suggest_spec.rb +73 -63
- data/spec/sample_ja.xml +1 -1
- data/spec/spec_helper.rb +13 -3
- metadata +30 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ffd260b1addbb7ef31c712b20bd25083b5adf349b993b24a437c40c2f6586218
|
4
|
+
data.tar.gz: 704a9b4961814af668998d153eeb40dba4416f9f6b74e28ad1c4971ff911e952
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b895594e5e8591804e2f08cc7d47d2f3ce34a6321940079fd05fbb7042a6c210f1c452a07c43908203dc03c842e23a99d128d124b3b890999a41a0eeea29bfec
|
7
|
+
data.tar.gz: 75fea7fa4200ad474d2efdf3519d1b8b2fce11bf6e3916f70e0d2366aeca075c4f63f0276124fbc26c55bcfa6539a7057d312e6c910ba76b745b5bbeac795f70
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,31 @@
|
|
1
|
+
# ChangeLog
|
2
|
+
|
3
|
+
## GoogleSuggest 1.1.0
|
4
|
+
|
5
|
+
* Migrate CI from Travis CI to GitHub Actions. [#32](https://github.com/satoryu/google_suggest/pull/32)
|
6
|
+
* And from this version, starts to support ruby v3.0
|
7
|
+
|
8
|
+
## GoogleSuggest 1.0.3
|
9
|
+
|
10
|
+
* BugFix: Failed to parse Suggest API response since encoding issue. [#29](https://github.com/satoryu/google_suggest/pull/29)
|
11
|
+
|
12
|
+
## GoogleSuggest 1.0.2
|
13
|
+
|
14
|
+
* Drop ruby 2.3 from supported ruby versions. [#25](https://github.com/satoryu/google_suggest/pull/25)
|
15
|
+
* Made `Region` module since it has never been instantiated. [#26](https://github.com/satoryu/google_suggest/pull/26)
|
16
|
+
|
17
|
+
## GoogleSuggest 1.0.1
|
18
|
+
|
19
|
+
* Drop ruby 2.1 and 2.2 from and add ruby 2.5 supported ruby versions. [#19](https://github.com/satoryu/google_suggest/pull/19)
|
20
|
+
[#22](https://github.com/satoryu/google_suggest/pull/22)
|
21
|
+
* Starting use Webmock and RSpec 3.[#15](https://github.com/satoryu/google_suggest/pull/15)[#21](https://github.com/satoryu/google_suggest/pull/21)
|
22
|
+
|
23
|
+
## GoogleSuggest 1.0.0
|
24
|
+
|
25
|
+
* Removing the dependency on `nokogiri`, use REXML. [#10](https://github.com/satoryu/google_suggest/pull/10)
|
26
|
+
* Fix `NameError: uninitialized constant GoogleSuggest::Region::DEFAULT_GOOGLE_HOST`. [#11](https://github.com/satoryu/google_suggest/pull/11)
|
27
|
+
* Allows to get all supported region codes with `GoogleSuggest::Region.codes`. [#14](https://github.com/satoryu/google_suggest/pull/14)
|
28
|
+
|
1
29
|
## GoogleSuggest 0.1.0
|
2
30
|
|
3
31
|
* `GoogleSuggest.suggest_for` supports `region` [#2](https://github.com/satoryu/google_suggest/issues/2)
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,25 +1,44 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
google_suggest (
|
5
|
-
nokogiri (~> 1.6.0)
|
4
|
+
google_suggest (1.1.0)
|
6
5
|
|
7
6
|
GEM
|
8
7
|
remote: https://rubygems.org/
|
9
8
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
rspec
|
21
|
-
|
22
|
-
|
9
|
+
addressable (2.7.0)
|
10
|
+
public_suffix (>= 2.0.2, < 5.0)
|
11
|
+
crack (0.4.5)
|
12
|
+
rexml
|
13
|
+
diff-lcs (1.4.4)
|
14
|
+
docile (1.3.5)
|
15
|
+
hashdiff (1.0.1)
|
16
|
+
public_suffix (4.0.6)
|
17
|
+
rake (13.0.3)
|
18
|
+
rexml (3.2.4)
|
19
|
+
rspec (3.10.0)
|
20
|
+
rspec-core (~> 3.10.0)
|
21
|
+
rspec-expectations (~> 3.10.0)
|
22
|
+
rspec-mocks (~> 3.10.0)
|
23
|
+
rspec-core (3.10.1)
|
24
|
+
rspec-support (~> 3.10.0)
|
25
|
+
rspec-expectations (3.10.1)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.10.0)
|
28
|
+
rspec-mocks (3.10.1)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.10.0)
|
31
|
+
rspec-support (3.10.1)
|
32
|
+
simplecov (0.21.2)
|
33
|
+
docile (~> 1.1)
|
34
|
+
simplecov-html (~> 0.11)
|
35
|
+
simplecov_json_formatter (~> 0.1)
|
36
|
+
simplecov-html (0.12.3)
|
37
|
+
simplecov_json_formatter (0.1.2)
|
38
|
+
webmock (3.11.1)
|
39
|
+
addressable (>= 2.3.6)
|
40
|
+
crack (>= 0.3.2)
|
41
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
23
42
|
|
24
43
|
PLATFORMS
|
25
44
|
ruby
|
@@ -27,7 +46,9 @@ PLATFORMS
|
|
27
46
|
DEPENDENCIES
|
28
47
|
google_suggest!
|
29
48
|
rake
|
30
|
-
rspec (~>
|
49
|
+
rspec (~> 3.10)
|
50
|
+
simplecov
|
51
|
+
webmock (~> 3.11)
|
31
52
|
|
32
53
|
BUNDLED WITH
|
33
|
-
|
54
|
+
2.2.6
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# GoogleSuggest
|
2
2
|
|
3
|
-
[![
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/google_suggest.svg)](https://badge.fury.io/rb/google_suggest)
|
4
|
+
[![CI](https://github.com/satoryu/google_suggest/workflows/CI/badge.svg)](https://github.com/satoryu/google_suggest/actions?query=workflow%3ACI)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/satoryu/google_suggest/badges/coverage.svg)](https://codeclimate.com/github/satoryu/google_suggest/coverage)
|
4
6
|
|
5
|
-
This gem allows you to access google suggest API in your ruby codes.
|
7
|
+
This gem allows you to access google suggest API in your ruby codes.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -22,21 +24,22 @@ Or install it yourself as:
|
|
22
24
|
|
23
25
|
## Usage
|
24
26
|
|
25
|
-
`GoogleSuggest.suggest_for` gives suggestions for a given keyword.
|
27
|
+
`GoogleSuggest.suggest_for` gives suggestions for a given keyword.
|
26
28
|
|
27
29
|
```ruby
|
28
30
|
GoogleSuggest.suggest_for 'google'
|
29
|
-
=> [
|
30
|
-
|
31
|
+
=> ["google", "google maps", "google translate", "google classroom", "google docs", "google drive", "google earth", "google play", "google scholar", "google slides"]
|
31
32
|
```
|
32
33
|
|
33
34
|
and allows developers to switch the endpoint by specifying `region` option:
|
34
35
|
|
35
36
|
```ruby
|
36
|
-
GoogleSuggest.suggest_for 'google'
|
37
|
-
=> [
|
37
|
+
GoogleSuggest.suggest_for 'google', region: 'jp'
|
38
|
+
=> ["google", "google maps", "google drive", "google translate", "google scholar", "google docs", "google flights", "google news", "google play", "google earth"]
|
38
39
|
```
|
39
40
|
|
41
|
+
You can get all available region codes by `GoogleSuggest::Region.codes`.
|
42
|
+
|
40
43
|
## Development
|
41
44
|
|
42
45
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/google_suggest.gemspec
CHANGED
@@ -5,22 +5,21 @@ require 'google_suggest/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'google_suggest'
|
8
|
-
spec.required_ruby_version = '>= 2.
|
8
|
+
spec.required_ruby_version = '>= 2.5.0'
|
9
9
|
spec.summary = 'A gem which allows us to retrieve suggest words from Google in your Ruby Code.'
|
10
10
|
spec.version = GoogleSuggest::VERSION
|
11
11
|
spec.author = 'Tatsuya Sato'
|
12
12
|
spec.email = 'satoryu.1981@gmail.com'
|
13
13
|
spec.homepage = 'http://github.com/satoryu/google_suggest/'
|
14
|
-
spec.license
|
14
|
+
spec.license = 'MIT'
|
15
15
|
spec.files = Dir[
|
16
16
|
'lib/**/*.rb',
|
17
17
|
'[A-Z]*',
|
18
18
|
'spec/**/*'
|
19
19
|
]
|
20
20
|
|
21
|
-
spec.add_runtime_dependency "nokogiri", '~> 1.6.0'
|
22
|
-
|
23
21
|
spec.add_development_dependency 'rake'
|
24
|
-
spec.add_development_dependency 'rspec', '~>
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
23
|
+
spec.add_development_dependency 'simplecov'
|
24
|
+
spec.add_development_dependency 'webmock', '~> 3.11'
|
25
25
|
end
|
26
|
-
|
data/lib/google_suggest.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require 'uri'
|
4
|
-
require 'nokogiri'
|
5
2
|
require 'net/http'
|
3
|
+
require 'rexml/document'
|
6
4
|
require 'google_suggest/configuration'
|
7
5
|
require 'google_suggest/region'
|
8
6
|
|
@@ -21,34 +19,26 @@ class GoogleSuggest
|
|
21
19
|
@configuration ||= Configuration.new
|
22
20
|
end
|
23
21
|
|
24
|
-
def self.suggest_for(keyword, options={})
|
25
|
-
|
22
|
+
def self.suggest_for(keyword, options = {})
|
23
|
+
new(options).suggest_for(keyword)
|
26
24
|
end
|
27
25
|
|
28
|
-
def initialize(options={})
|
26
|
+
def initialize(options = {})
|
29
27
|
@home_language = self.class.configuration.home_language
|
30
28
|
@proxy = self.class.configuration.proxy
|
31
29
|
@region = options[:region] || self.class.configure.region
|
32
30
|
end
|
33
31
|
|
34
32
|
def suggest_for(keyword)
|
35
|
-
query = {
|
36
|
-
|
37
|
-
|
33
|
+
query = {
|
34
|
+
output: 'toolbar',
|
35
|
+
hl: home_language,
|
36
|
+
q: keyword
|
37
|
+
}
|
38
|
+
|
38
39
|
res = http_get('/complete/search', query)
|
39
|
-
|
40
|
-
|
41
|
-
xml.css('toplevel CompleteSuggestion').each do |node|
|
42
|
-
suggest = {}
|
43
|
-
node.children.each do |child|
|
44
|
-
suggest[child.name] = (child['data'] || child['int'].to_i)
|
45
|
-
end
|
46
|
-
if suggest['suggestion'] and not suggest['suggestion'].valid_encoding?
|
47
|
-
suggest['suggestion'].force_encoding('Shift_JIS').encode!('UTF-8')
|
48
|
-
end
|
49
|
-
suggestions << suggest
|
50
|
-
end
|
51
|
-
return suggestions
|
40
|
+
|
41
|
+
parse(res.body.to_s)
|
52
42
|
end
|
53
43
|
|
54
44
|
private
|
@@ -64,7 +54,7 @@ class GoogleSuggest
|
|
64
54
|
end
|
65
55
|
|
66
56
|
def http_get(path, query)
|
67
|
-
path = path + '?' +
|
57
|
+
path = path + '?' + URI.encode_www_form(query)
|
68
58
|
req = Net::HTTP::Get.new(path)
|
69
59
|
http.request(req)
|
70
60
|
end
|
@@ -72,4 +62,14 @@ class GoogleSuggest
|
|
72
62
|
def google_host
|
73
63
|
Region.host_for(region)
|
74
64
|
end
|
65
|
+
|
66
|
+
def parse(doc)
|
67
|
+
doc = doc.encode('UTF-8', 'Shift_JIS') unless doc.valid_encoding?
|
68
|
+
xml = REXML::Document.new(doc)
|
69
|
+
suggestions = REXML::XPath.match(xml, '/toplevel/CompleteSuggestion/suggestion').map do |suggest|
|
70
|
+
suggest.attribute('data').value
|
71
|
+
end
|
72
|
+
|
73
|
+
suggestions
|
74
|
+
end
|
75
75
|
end
|
@@ -1,208 +1,213 @@
|
|
1
1
|
class GoogleSuggest
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
2
|
+
module Region
|
3
|
+
GOOGLE_HOSTS = { # :nodoc:
|
4
|
+
ac: 'www.google.ac',
|
5
|
+
ad: 'www.google.ad',
|
6
|
+
ae: 'www.google.ae',
|
7
|
+
af: 'www.google.com.af',
|
8
|
+
ag: 'www.google.com.ag',
|
9
|
+
ai: 'www.google.com.ai',
|
10
|
+
al: 'www.google.al',
|
11
|
+
am: 'www.google.am',
|
12
|
+
ao: 'www.google.am',
|
13
|
+
ar: 'www.google.com.ar',
|
14
|
+
as: 'www.google.as',
|
15
|
+
at: 'www.google.at',
|
16
|
+
au: 'www.google.com.au',
|
17
|
+
az: 'www.google.az',
|
18
|
+
ba: 'www.google.ga',
|
19
|
+
bd: 'www.google.com.bd',
|
20
|
+
be: 'www.google.be',
|
21
|
+
bf: 'www.google.bf',
|
22
|
+
bg: 'www.google.bg',
|
23
|
+
bh: 'www.google.com.bh',
|
24
|
+
bi: 'www.google.bi',
|
25
|
+
bj: 'www.google.bj',
|
26
|
+
bn: 'www.google.com.bn',
|
27
|
+
bo: 'www.google.com.bo',
|
28
|
+
br: 'www.google.com.br',
|
29
|
+
bs: 'www.google.bs',
|
30
|
+
bt: 'www.google.bt',
|
31
|
+
bw: 'www.google.co.bw',
|
32
|
+
by: 'www.google.by',
|
33
|
+
bz: 'www.google.com.bz',
|
34
|
+
ca: 'www.google.ca',
|
35
|
+
kh: 'www.google.com.kh',
|
36
|
+
cc: 'www.google.cc',
|
37
|
+
cd: 'www.google.cd',
|
38
|
+
cf: 'www.google.cf',
|
39
|
+
cat: 'www.google.cat',
|
40
|
+
cg: 'www.google.cg',
|
41
|
+
ch: 'www.google.ch',
|
42
|
+
ci: 'www.google.ci',
|
43
|
+
ck: 'www.google.co.ck',
|
44
|
+
cl: 'www.google.cl',
|
45
|
+
cm: 'www.google.cm',
|
46
|
+
cn: 'www.google.cn',
|
47
|
+
co: 'www.google.com.co',
|
48
|
+
cr: 'www.google.co.cr',
|
49
|
+
cu: 'www.google.com.cu',
|
50
|
+
cv: 'www.google.cv',
|
51
|
+
cy: 'www.google.com.cy',
|
52
|
+
cz: 'www.google.cz',
|
53
|
+
de: 'www.google.de',
|
54
|
+
dj: 'www.google.dj',
|
55
|
+
dk: 'www.google.dk',
|
56
|
+
dm: 'www.google.dm',
|
57
|
+
do: 'www.google.com.do',
|
58
|
+
dz: 'www.google.dz',
|
59
|
+
ec: 'www.google.com.ec',
|
60
|
+
ee: 'www.google.ee',
|
61
|
+
eg: 'www.google.com.eg',
|
62
|
+
es: 'www.google.es',
|
63
|
+
et: 'www.google.com.et',
|
64
|
+
fi: 'www.google.fi',
|
65
|
+
fj: 'www.google.com.fj',
|
66
|
+
fm: 'www.google.fm',
|
67
|
+
fr: 'www.google.fr',
|
68
|
+
ga: 'www.google.ga',
|
69
|
+
ge: 'www.google.ge',
|
70
|
+
gf: 'www.google.gf',
|
71
|
+
gg: 'www.google.gg',
|
72
|
+
gh: 'www.google.com.gh',
|
73
|
+
gi: 'www.google.com.gi',
|
74
|
+
gl: 'www.google.gl',
|
75
|
+
gm: 'www.google.gm',
|
76
|
+
gp: 'www.google.gp',
|
77
|
+
gr: 'www.google.gr',
|
78
|
+
gt: 'www.google.com.gt',
|
79
|
+
gy: 'www.google.gy',
|
80
|
+
hk: 'www.google.com.hk',
|
81
|
+
hn: 'www.google.hn',
|
82
|
+
hr: 'www.google.hr',
|
83
|
+
ht: 'www.google.ht',
|
84
|
+
hu: 'www.google.hu',
|
85
|
+
id: 'www.google.co.id',
|
86
|
+
iq: 'www.google.iq',
|
87
|
+
ie: 'www.google.ie',
|
88
|
+
il: 'www.google.co.il',
|
89
|
+
im: 'www.google.im',
|
90
|
+
in: 'www.google.co.in',
|
91
|
+
io: 'www.google.io',
|
92
|
+
is: 'www.google.is',
|
93
|
+
it: 'www.google.it',
|
94
|
+
je: 'www.google.je',
|
95
|
+
jm: 'www.google.com.jm',
|
96
|
+
jo: 'www.google.jo',
|
97
|
+
jp: 'www.google.co.jp',
|
98
|
+
ke: 'www.google.co.ke',
|
99
|
+
ki: 'www.google.ki',
|
100
|
+
kg: 'www.google.kg',
|
101
|
+
kr: 'www.google.co.kr',
|
102
|
+
kw: 'www.google.com.kw',
|
103
|
+
kz: 'www.google.kz',
|
104
|
+
la: 'www.google.la',
|
105
|
+
lb: 'www.google.com.lb',
|
106
|
+
lc: 'www.google.com.lc',
|
107
|
+
li: 'www.google.li',
|
108
|
+
lk: 'www.google.lk',
|
109
|
+
ls: 'www.google.co.ls',
|
110
|
+
lt: 'www.google.lt',
|
111
|
+
lu: 'www.google.lu',
|
112
|
+
lv: 'www.google.lv',
|
113
|
+
ly: 'www.google.com.ly',
|
114
|
+
ma: 'www.google.co.ma',
|
115
|
+
md: 'www.google.md',
|
116
|
+
me: 'www.google.me',
|
117
|
+
mg: 'www.google.mg',
|
118
|
+
mk: 'www.google.mk',
|
119
|
+
ml: 'www.google.ml',
|
120
|
+
mm: 'www.google.com.mm',
|
121
|
+
mn: 'www.google.mn',
|
122
|
+
ms: 'www.google.ms',
|
123
|
+
mt: 'www.google.com.mt',
|
124
|
+
mu: 'www.google.mu',
|
125
|
+
mv: 'www.google.mv',
|
126
|
+
mz: 'www.google.co.mz',
|
127
|
+
na: 'www.google.com.na',
|
128
|
+
ne: 'www.google.ne',
|
129
|
+
nf: 'www.google.com.nf',
|
130
|
+
ng: 'www.google.com.ng',
|
131
|
+
ni: 'www.google.com.ni',
|
132
|
+
nl: 'www.google.nl',
|
133
|
+
no: 'www.google.no',
|
134
|
+
np: 'www.google.com.np',
|
135
|
+
nr: 'www.google.nr',
|
136
|
+
nu: 'www.google.nu',
|
137
|
+
nz: 'www.google.co.nz',
|
138
|
+
om: 'www.google.com.om',
|
139
|
+
pk: 'www.google.com.pk',
|
140
|
+
pa: 'www.google.com.pa',
|
141
|
+
pe: 'www.google.com.pe',
|
142
|
+
ph: 'www.google.com.ph',
|
143
|
+
pl: 'www.google.pl',
|
144
|
+
pg: 'www.google.com.pg',
|
145
|
+
pn: 'www.google.pn',
|
146
|
+
pr: 'www.google.com.pr',
|
147
|
+
ps: 'www.google.ps',
|
148
|
+
pt: 'www.google.pt',
|
149
|
+
py: 'www.google.com.py',
|
150
|
+
qa: 'www.google.com.qa',
|
151
|
+
ro: 'www.google.ro',
|
152
|
+
rs: 'www.google.rs',
|
153
|
+
ru: 'www.google.ru',
|
154
|
+
rw: 'www.google.rw',
|
155
|
+
sa: 'www.google.com.sa',
|
156
|
+
sb: 'www.google.com.sb',
|
157
|
+
sc: 'www.google.sc',
|
158
|
+
se: 'www.google.se',
|
159
|
+
sg: 'www.google.com.sg',
|
160
|
+
sh: 'www.google.sh',
|
161
|
+
si: 'www.google.si',
|
162
|
+
sk: 'www.google.sk',
|
163
|
+
sl: 'www.google.sl',
|
164
|
+
sn: 'www.google.sn',
|
165
|
+
sm: 'www.google.sm',
|
166
|
+
so: 'www.google.so',
|
167
|
+
st: 'www.google.st',
|
168
|
+
sr: 'www.google.sr',
|
169
|
+
sv: 'www.google.com.sv',
|
170
|
+
td: 'www.google.td',
|
171
|
+
tg: 'www.google.tg',
|
172
|
+
th: 'www.google.co.th',
|
173
|
+
tj: 'www.google.com.tj',
|
174
|
+
tk: 'www.google.tk',
|
175
|
+
tl: 'www.google.tl',
|
176
|
+
tm: 'www.google.tm',
|
177
|
+
to: 'www.google.to',
|
178
|
+
tn: 'www.google.tn',
|
179
|
+
tr: 'www.google.com.tr',
|
180
|
+
tt: 'www.google.tt',
|
181
|
+
tw: 'www.google.com.tw',
|
182
|
+
tz: 'www.google.co.tz',
|
183
|
+
ua: 'www.google.com.ua',
|
184
|
+
ug: 'www.google.co.ug',
|
185
|
+
uk: 'www.google.co.uk',
|
186
|
+
us: 'www.google.com',
|
187
|
+
com: 'www.google.com',
|
188
|
+
uy: 'www.google.com.uy',
|
189
|
+
uz: 'www.google.co.uz',
|
190
|
+
vc: 'www.google.com.vc',
|
191
|
+
ve: 'www.google.co.ve',
|
192
|
+
vg: 'www.google.vg',
|
193
|
+
vi: 'www.google.co.vi',
|
194
|
+
vn: 'www.google.com.vn',
|
195
|
+
vu: 'www.google.vu',
|
196
|
+
ws: 'www.google.ws',
|
197
|
+
za: 'www.google.co.za',
|
198
|
+
zm: 'www.google.co.zm',
|
199
|
+
zw: 'www.google.co.zw'
|
200
|
+
}.freeze
|
200
201
|
|
201
|
-
|
202
|
-
def self.host_for(region_code=nil)
|
203
|
-
region_code = :com if region_code.nil?
|
202
|
+
def host_for(region_code = nil)
|
204
203
|
region_code = region_code.to_sym if region_code.is_a?(String)
|
205
|
-
GOOGLE_HOSTS[region_code] ||
|
204
|
+
GOOGLE_HOSTS[region_code] || GOOGLE_HOSTS[:com]
|
206
205
|
end
|
206
|
+
|
207
|
+
def codes
|
208
|
+
GOOGLE_HOSTS.keys
|
209
|
+
end
|
210
|
+
|
211
|
+
module_function :host_for, :codes
|
207
212
|
end
|
208
213
|
end
|
@@ -8,15 +8,29 @@ describe GoogleSuggest::Region do
|
|
8
8
|
let(:region_code) { nil }
|
9
9
|
|
10
10
|
it 'should be default host "www.google.com"' do
|
11
|
-
|
11
|
+
is_expected.to be_eql('www.google.com')
|
12
12
|
end
|
13
13
|
end
|
14
14
|
context 'when giving a valid region code like "ac"' do
|
15
15
|
let(:region_code) { 'ac' }
|
16
16
|
|
17
17
|
it 'shoud be the google host for the given region' do
|
18
|
-
|
18
|
+
is_expected.to be_eql('www.google.ac')
|
19
19
|
end
|
20
20
|
end
|
21
|
+
context 'when giving an undefined region code like "zz"' do
|
22
|
+
let(:region_code) { 'zz' }
|
23
|
+
|
24
|
+
it 'should be the default host' do
|
25
|
+
is_expected.to be_eql('www.google.com')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '.codes' do
|
31
|
+
subject { GoogleSuggest::Region.codes }
|
32
|
+
|
33
|
+
it { is_expected.to be_a(Array) }
|
34
|
+
it { is_expected.to be_all { |k| k.is_a?(Symbol) } }
|
21
35
|
end
|
22
36
|
end
|
data/spec/google_suggest_spec.rb
CHANGED
@@ -1,41 +1,43 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
require 'spec_helper'
|
3
2
|
|
4
3
|
describe GoogleSuggest do
|
5
|
-
describe
|
4
|
+
describe '.new' do
|
6
5
|
let(:args) { {} }
|
7
6
|
let(:google_suggest) { GoogleSuggest.new(args) }
|
8
7
|
|
9
8
|
subject { google_suggest }
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
describe '#home_language' do
|
11
|
+
subject { super().home_language }
|
12
|
+
it { is_expected.to be_eql 'en' }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#proxy' do
|
16
|
+
subject { super().proxy }
|
17
|
+
it { is_expected.to be_nil }
|
18
|
+
end
|
14
19
|
|
15
20
|
context 'when giving region option' do
|
16
21
|
let(:args) { { region: 'ac' } }
|
17
22
|
|
18
|
-
|
19
|
-
|
23
|
+
describe '#home_language' do
|
24
|
+
subject { super().home_language }
|
25
|
+
it { is_expected.to be_eql 'en' }
|
26
|
+
end
|
20
27
|
end
|
21
28
|
end
|
22
29
|
|
23
|
-
describe
|
24
|
-
|
25
|
-
google_suggest = GoogleSuggest.new
|
26
|
-
res = Object.new
|
27
|
-
allow(res).to receive(:body) do
|
28
|
-
File.read(File.join(File.dirname(__FILE__), 'sample_us.xml'))
|
29
|
-
end
|
30
|
-
allow(google_suggest).to receive(:http_get) { res }
|
31
|
-
suggestions = google_suggest.suggest_for('google')
|
32
|
-
allow(GoogleSuggest).to receive(:new).and_return { double('google_suggest', :suggest_for => suggestions) }
|
30
|
+
describe '.suggest_for' do
|
31
|
+
let(:suggestions) { GoogleSuggest.suggest_for('google') }
|
33
32
|
|
34
|
-
|
33
|
+
before do
|
34
|
+
stub_request(:get, 'http://www.google.com/complete/search')
|
35
|
+
.with(query: { hl: 'en', output: 'toolbar', q: 'google' })
|
36
|
+
.to_return(body: File.read(File.join(File.dirname(__FILE__), 'sample_us.xml')))
|
35
37
|
end
|
36
38
|
|
37
39
|
it do
|
38
|
-
|
40
|
+
expect(suggestions.size).to be 10
|
39
41
|
end
|
40
42
|
|
41
43
|
context 'When passing Hash as options' do
|
@@ -50,7 +52,7 @@ describe GoogleSuggest do
|
|
50
52
|
end
|
51
53
|
|
52
54
|
describe '.configure' do
|
53
|
-
context
|
55
|
+
context 'When called without block' do
|
54
56
|
before do
|
55
57
|
@configure = GoogleSuggest.configure
|
56
58
|
@configure.home_language = 'ja'
|
@@ -60,11 +62,22 @@ describe GoogleSuggest do
|
|
60
62
|
|
61
63
|
subject { GoogleSuggest.new }
|
62
64
|
|
63
|
-
|
64
|
-
|
65
|
-
|
65
|
+
describe '#home_language' do
|
66
|
+
subject { super().home_language }
|
67
|
+
it { is_expected.to be_eql 'ja' }
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#region' do
|
71
|
+
subject { super().region }
|
72
|
+
it { is_expected.to be_eql 'ac' }
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#proxy' do
|
76
|
+
subject { super().proxy }
|
77
|
+
it { is_expected.to be_eql 'http://proxy.example.com' }
|
78
|
+
end
|
66
79
|
end
|
67
|
-
context
|
80
|
+
context 'When called with given block' do
|
68
81
|
before do
|
69
82
|
GoogleSuggest.configure do |c|
|
70
83
|
c.home_language = 'us'
|
@@ -74,72 +87,69 @@ describe GoogleSuggest do
|
|
74
87
|
|
75
88
|
subject { GoogleSuggest.new }
|
76
89
|
|
77
|
-
|
78
|
-
|
90
|
+
describe '#home_language' do
|
91
|
+
subject { super().home_language }
|
92
|
+
it { is_expected.to be_eql 'us' }
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#proxy' do
|
96
|
+
subject { super().proxy }
|
97
|
+
it { is_expected.to be_eql 'http://proxy.example.com' }
|
98
|
+
end
|
79
99
|
end
|
80
100
|
end
|
81
101
|
|
82
|
-
describe
|
102
|
+
describe '#suggest_for' do
|
83
103
|
before :all do
|
84
104
|
GoogleSuggest.configure do |c|
|
85
105
|
c.home_language = 'us'
|
106
|
+
c.region = :com
|
86
107
|
end
|
87
108
|
end
|
88
109
|
|
110
|
+
let(:google_suggest) { GoogleSuggest.new }
|
111
|
+
let(:suggestions) { google_suggest.suggest_for('google') }
|
112
|
+
|
89
113
|
before do
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
doc = nil
|
94
|
-
File.open(File.join(File.dirname(__FILE__), 'sample_us.xml')) do |f|
|
95
|
-
doc = f.read
|
96
|
-
end
|
97
|
-
doc
|
98
|
-
end
|
99
|
-
allow(@google_suggest).to receive(:http_get) { res }
|
114
|
+
stub_request(:get, 'http://www.google.com/complete/search')
|
115
|
+
.with(query: { hl: 'us', output: 'toolbar', q: 'google'})
|
116
|
+
.and_return(body: File.read(File.join(__dir__, 'sample_us.xml')))
|
100
117
|
end
|
101
118
|
|
102
119
|
it do
|
103
|
-
suggestions
|
104
|
-
suggestions.size.should == 10
|
120
|
+
expect(suggestions.size).to eq(10)
|
105
121
|
end
|
106
122
|
|
107
123
|
it 'all suggestions should have the value of the key \'suggest\' 'do
|
108
|
-
suggestions
|
109
|
-
|
110
|
-
not suggest['suggest'].nil?
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
it 'all suggestions should have the value of the key \'num_queries\' 'do
|
115
|
-
suggestions = @google_suggest.suggest_for 'google'
|
116
|
-
suggestions.should be_all do |suggest|
|
117
|
-
not suggest['num_queries'].nil?
|
124
|
+
expect(suggestions).to be_all do |suggest|
|
125
|
+
not suggest.nil?
|
118
126
|
end
|
119
127
|
end
|
120
128
|
end
|
121
|
-
describe
|
129
|
+
describe '#suggest_for with home language \'ja\'' do
|
122
130
|
before :all do
|
123
131
|
GoogleSuggest.configure do |c|
|
124
132
|
c.home_language = 'ja'
|
133
|
+
c.region = :com
|
125
134
|
end
|
126
135
|
end
|
127
136
|
|
137
|
+
let(:google_suggest) { GoogleSuggest.new }
|
138
|
+
|
128
139
|
before do
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
140
|
+
stub_request(:get, 'http://www.google.com/complete/search')
|
141
|
+
.with(query: { hl: 'ja', output: 'toolbar', q: 'グーグル' })
|
142
|
+
.to_return(body: File.read(File.join(File.dirname(__FILE__), 'sample_ja.xml')))
|
143
|
+
end
|
144
|
+
|
145
|
+
subject { google_suggest.suggest_for('グーグル').shift }
|
146
|
+
|
147
|
+
describe '#encoding' do
|
148
|
+
subject { super().encoding }
|
149
|
+
|
150
|
+
it { is_expected.to be Encoding.find('UTF-8') }
|
139
151
|
end
|
140
|
-
subject {@google_suggest.suggest_for('グーグル').shift['suggestion']}
|
141
152
|
|
142
|
-
|
143
|
-
it { should be_valid_encoding }
|
153
|
+
it { is_expected.to be_valid_encoding }
|
144
154
|
end
|
145
155
|
end
|
data/spec/sample_ja.xml
CHANGED
@@ -1 +1 @@
|
|
1
|
-
<?xml version="1.0"?><toplevel><CompleteSuggestion><suggestion data="
|
1
|
+
<?xml version="1.0"?><toplevel><CompleteSuggestion><suggestion data="�O�[�O���}�b�v"/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O��"/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O���|��"/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O���A�[�X"/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O���h���C�u"/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O���t�H�g"/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O���J�����_�["/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O���A�J�E���g"/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O���A�i���e�B�N�X"/></CompleteSuggestion><CompleteSuggestion><suggestion data="�O�[�O���t�H�[��"/></CompleteSuggestion></toplevel>
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,18 @@
|
|
1
|
-
|
1
|
+
if ENV['CI']
|
2
|
+
require 'simplecov'
|
2
3
|
|
3
|
-
|
4
|
-
|
4
|
+
SimpleCov.start do
|
5
|
+
add_filter '/spec/'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
require File.expand_path(File.join(__dir__, '..', 'lib', 'google_suggest'))
|
10
|
+
require 'webmock/rspec'
|
5
11
|
|
6
12
|
RSpec.configure do |c|
|
7
13
|
c.mock_with :rspec
|
14
|
+
|
15
|
+
c.before :suite do
|
16
|
+
WebMock.disable_net_connect!
|
17
|
+
end
|
8
18
|
end
|
metadata
CHANGED
@@ -1,31 +1,45 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_suggest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatsuya Sato
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
type: :
|
33
|
+
version: '3.10'
|
34
|
+
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
40
|
+
version: '3.10'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
42
|
+
name: simplecov
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - ">="
|
@@ -39,20 +53,20 @@ dependencies:
|
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
56
|
+
name: webmock
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
61
|
+
version: '3.11'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
55
|
-
description:
|
68
|
+
version: '3.11'
|
69
|
+
description:
|
56
70
|
email: satoryu.1981@gmail.com
|
57
71
|
executables: []
|
58
72
|
extensions: []
|
@@ -79,7 +93,7 @@ homepage: http://github.com/satoryu/google_suggest/
|
|
79
93
|
licenses:
|
80
94
|
- MIT
|
81
95
|
metadata: {}
|
82
|
-
post_install_message:
|
96
|
+
post_install_message:
|
83
97
|
rdoc_options: []
|
84
98
|
require_paths:
|
85
99
|
- lib
|
@@ -87,16 +101,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
101
|
requirements:
|
88
102
|
- - ">="
|
89
103
|
- !ruby/object:Gem::Version
|
90
|
-
version: 2.
|
104
|
+
version: 2.5.0
|
91
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
106
|
requirements:
|
93
107
|
- - ">="
|
94
108
|
- !ruby/object:Gem::Version
|
95
109
|
version: '0'
|
96
110
|
requirements: []
|
97
|
-
|
98
|
-
|
99
|
-
signing_key:
|
111
|
+
rubygems_version: 3.0.3
|
112
|
+
signing_key:
|
100
113
|
specification_version: 4
|
101
114
|
summary: A gem which allows us to retrieve suggest words from Google in your Ruby
|
102
115
|
Code.
|