lita-google-images 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/LICENSE +1 -1
- data/README.md +8 -1
- data/lib/lita/handlers/google_images.rb +17 -10
- data/lita-google-images.gemspec +1 -1
- data/spec/lita/handlers/google_images_spec.rb +61 -52
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4bc36412c0c5156ab0ffed74fe5fbcdff1e23d5
|
4
|
+
data.tar.gz: 96e450e761edcdf46b67eac2bd56b8156718cf87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2fbcff89238256c8e7022f49542956aba93dbed8e23d4a54d453d5832551b3a73e6bc306de89fac4b898704e914fa55510cfe71ea56cb63705b2afef19d63617
|
7
|
+
data.tar.gz: 5839266460ac7db8b237edb1513ec48893f5a4d471d0e13ac6ab56b02586392c544d09ece53b6f42b6e3b22cc4a6acb77e02b38e53e8479a96dfc82d9204c219
|
data/.travis.yml
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
language: ruby
|
2
|
+
sudo: false
|
3
|
+
cache: bundler
|
2
4
|
rvm:
|
3
5
|
- 2.0.0
|
4
6
|
script: bundle exec rspec
|
5
7
|
before_install:
|
6
8
|
- gem update --system
|
9
|
+
- gem update bundler
|
7
10
|
services:
|
8
11
|
- redis-server
|
9
12
|
notifications:
|
10
13
|
email: false
|
11
14
|
webhooks:
|
12
15
|
urls:
|
13
|
-
-
|
16
|
+
- http://util.perceptes.com:8080/travis
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -16,9 +16,16 @@ gem "lita-google-images"
|
|
16
16
|
|
17
17
|
## Configuration
|
18
18
|
|
19
|
+
### Required attributes
|
20
|
+
|
21
|
+
```
|
22
|
+
* `google_cse_id` (String) - ID of the Google Custom Search account to use.
|
23
|
+
* `google_cse_key` (String) - API key for the Google Custom Search account.
|
24
|
+
```
|
25
|
+
|
19
26
|
### Optional attributes
|
20
27
|
|
21
|
-
* `safe_search` (String, Symbol) - The safe search setting to use when querying for images. Possible values are `:
|
28
|
+
* `safe_search` (String, Symbol) - The safe search setting to use when querying for images. Possible values are `:high`, `:medium`, and `:off`. Default: `:high`.
|
22
29
|
|
23
30
|
### Example
|
24
31
|
|
@@ -3,18 +3,21 @@ require "lita"
|
|
3
3
|
module Lita
|
4
4
|
module Handlers
|
5
5
|
class GoogleImages < Handler
|
6
|
-
URL = "https://
|
7
|
-
VALID_SAFE_VALUES = %w(
|
6
|
+
URL = "https://www.googleapis.com/customsearch/v1"
|
7
|
+
VALID_SAFE_VALUES = %w(high medium off)
|
8
8
|
|
9
|
-
config :safe_search, types: [String, Symbol], default: :
|
9
|
+
config :safe_search, types: [String, Symbol], default: :high do
|
10
10
|
validate do |value|
|
11
11
|
unless VALID_SAFE_VALUES.include?(value.to_s.strip)
|
12
|
-
"valid values are :
|
12
|
+
"valid values are :high, :medium, or :off"
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
|
17
|
+
config :google_cse_id, type: String, required: true
|
18
|
+
config :google_cse_key, type: String, required: true
|
19
|
+
|
20
|
+
route(/(?:image|img)(?:\s+me)? (.+)/i, :fetch, command: true, help: {
|
18
21
|
"image QUERY" => "Displays a random image from Google Images matching the query."
|
19
22
|
})
|
20
23
|
|
@@ -24,22 +27,26 @@ module Lita
|
|
24
27
|
http_response = http.get(
|
25
28
|
URL,
|
26
29
|
v: "1.0",
|
30
|
+
searchType: 'image',
|
27
31
|
q: query,
|
28
32
|
safe: config.safe_search,
|
29
|
-
|
33
|
+
fields: 'items(link)',
|
34
|
+
rsz: 8,
|
35
|
+
cx: config.google_cse_id,
|
36
|
+
key: config.google_cse_key
|
30
37
|
)
|
31
38
|
|
32
39
|
data = MultiJson.load(http_response.body)
|
33
40
|
|
34
|
-
if
|
35
|
-
choice = data["
|
41
|
+
if http_response.status == 200
|
42
|
+
choice = data["items"].sample if data["items"]
|
36
43
|
if choice
|
37
|
-
response.reply ensure_extension(choice["
|
44
|
+
response.reply ensure_extension(choice["link"])
|
38
45
|
else
|
39
46
|
response.reply %{No images found for "#{query}".}
|
40
47
|
end
|
41
48
|
else
|
42
|
-
reason = data["
|
49
|
+
reason = data["error"]["message"] || "unknown error"
|
43
50
|
Lita.logger.warn(
|
44
51
|
"Couldn't get image from Google: #{reason}"
|
45
52
|
)
|
data/lita-google-images.gemspec
CHANGED
@@ -5,76 +5,85 @@ describe Lita::Handlers::GoogleImages, lita_handler: true do
|
|
5
5
|
it { is_expected.to route_command("image foo").to(:fetch) }
|
6
6
|
it { is_expected.to route_command("img foo").to(:fetch) }
|
7
7
|
it { is_expected.to route_command("img me foo").to(:fetch) }
|
8
|
+
it { is_expected.to route_command("IMAGE FOO").to(:fetch) }
|
8
9
|
|
9
10
|
describe "#foo" do
|
10
|
-
let(:response) { double("Faraday::Response") }
|
11
|
+
let(:response) { double("Faraday::Response", status: 200,) }
|
12
|
+
let(:fail_response) { double("Faraday::Response", status: 500) }
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
context "Success" do
|
15
|
+
before do
|
16
|
+
allow_any_instance_of(
|
17
|
+
Faraday::Connection
|
18
|
+
).to receive(:get).and_return(response)
|
19
|
+
end
|
17
20
|
|
18
|
-
|
19
|
-
|
21
|
+
it "replies with a matching image URL on success" do
|
22
|
+
allow(response).to receive(:body).and_return(<<-JSON.chomp
|
20
23
|
{
|
21
|
-
"
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
}
|
27
|
-
]
|
28
|
-
}
|
24
|
+
"items": [
|
25
|
+
{
|
26
|
+
"link": "http://www.example.com/path/to/an/image"
|
27
|
+
}
|
28
|
+
]
|
29
29
|
}
|
30
30
|
JSON
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
)
|
32
|
+
send_command("image carl")
|
33
|
+
expect(replies.last).to eq(
|
34
|
+
"http://www.example.com/path/to/an/image#.png"
|
35
|
+
)
|
36
|
+
end
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
it "doesn't append a fake file extension if the image URL has a common image extension" do
|
39
|
+
allow(response).to receive(:body).and_return(<<-JSON.chomp
|
40
40
|
{
|
41
|
-
"
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
}
|
47
|
-
]
|
48
|
-
}
|
41
|
+
"items": [
|
42
|
+
{
|
43
|
+
"link": "http://www.example.com/path/to/an/image.jpg"
|
44
|
+
}
|
45
|
+
]
|
49
46
|
}
|
50
47
|
JSON
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
48
|
+
)
|
49
|
+
send_command("image carl")
|
50
|
+
expect(replies.last).to eq(
|
51
|
+
"http://www.example.com/path/to/an/image.jpg"
|
52
|
+
)
|
53
|
+
end
|
57
54
|
|
58
|
-
|
59
|
-
|
60
|
-
{"
|
55
|
+
it "replies that no images were found if the results are empty" do
|
56
|
+
allow(response).to receive(:body).and_return(<<-JSON.chomp
|
57
|
+
{"items": [] }
|
61
58
|
JSON
|
62
|
-
|
63
|
-
|
64
|
-
|
59
|
+
)
|
60
|
+
send_command("image carl")
|
61
|
+
expect(replies.last).to eq(%{No images found for "carl".})
|
62
|
+
end
|
65
63
|
end
|
66
64
|
|
67
|
-
|
68
|
-
|
65
|
+
context "Failure" do
|
66
|
+
before do
|
67
|
+
allow_any_instance_of(
|
68
|
+
Faraday::Connection
|
69
|
+
).to receive(:get).and_return(fail_response)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "logs a warning on failure" do
|
73
|
+
allow(fail_response).to receive(:body).and_return(<<-JSON.chomp
|
69
74
|
{
|
70
|
-
"
|
71
|
-
|
75
|
+
"error":
|
76
|
+
{
|
77
|
+
"code": 500,
|
78
|
+
"message": "google fail"
|
79
|
+
}
|
72
80
|
}
|
73
81
|
JSON
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
82
|
+
)
|
83
|
+
expect(Lita.logger).to receive(:warn).with(/google fail/)
|
84
|
+
send_command("image carl")
|
85
|
+
expect(replies).to be_empty
|
86
|
+
end
|
78
87
|
end
|
79
88
|
end
|
80
89
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lita-google-images
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jimmy Cuadra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lita
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.4.5.1
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: A Lita handler for fetching images from Google.
|