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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3d9a71b531642fd0dbf1a9e84488fd8474c8e1a
4
- data.tar.gz: 8a479aa429d9217b6c32786dfcfa48d0a80899a5
3
+ metadata.gz: c4bc36412c0c5156ab0ffed74fe5fbcdff1e23d5
4
+ data.tar.gz: 96e450e761edcdf46b67eac2bd56b8156718cf87
5
5
  SHA512:
6
- metadata.gz: 31884376917a510d9682a390861a65ed6e950cf640197d6240dc11018708df8e7c844b90577d40efa7dafaf050d355db11d828042d134fa06db594a6bb9467ca
7
- data.tar.gz: 34ef2fde97127a128d0f661180b51ad1eb13ae500e398049db911831abc117dabc5bd3748eaa0a4082f4e5b2e5822e5d5ffb9c67c96c754da1cba17626b76b0b
6
+ metadata.gz: 2fbcff89238256c8e7022f49542956aba93dbed8e23d4a54d453d5832551b3a73e6bc306de89fac4b898704e914fa55510cfe71ea56cb63705b2afef19d63617
7
+ data.tar.gz: 5839266460ac7db8b237edb1513ec48893f5a4d471d0e13ac6ab56b02586392c544d09ece53b6f42b6e3b22cc4a6acb77e02b38e53e8479a96dfc82d9204c219
@@ -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
- - https://lita-freenode.herokuapp.com/travis
16
+ - http://util.perceptes.com:8080/travis
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Jimmy Cuadra
1
+ Copyright (c) 2013-2015 Jimmy Cuadra
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
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 `:active`, `:moderate`, and `:off`. Default: `:active`.
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://ajax.googleapis.com/ajax/services/search/images"
7
- VALID_SAFE_VALUES = %w(active moderate off)
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: :active do
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 :active, :moderate, or :off"
12
+ "valid values are :high, :medium, or :off"
13
13
  end
14
14
  end
15
15
  end
16
16
 
17
- route(/(?:image|img)(?:\s+me)? (.+)/, :fetch, command: true, help: {
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
- rsz: 8
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 data["responseStatus"] == 200
35
- choice = data["responseData"]["results"].sample
41
+ if http_response.status == 200
42
+ choice = data["items"].sample if data["items"]
36
43
  if choice
37
- response.reply ensure_extension(choice["unescapedUrl"])
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["responseDetails"] || "unknown error"
49
+ reason = data["error"]["message"] || "unknown error"
43
50
  Lita.logger.warn(
44
51
  "Couldn't get image from Google: #{reason}"
45
52
  )
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-google-images"
3
- spec.version = "2.0.0"
3
+ spec.version = "3.0.0"
4
4
  spec.authors = ["Jimmy Cuadra"]
5
5
  spec.email = ["jimmy@jimmycuadra.com"]
6
6
  spec.description = %q{A Lita handler for fetching images from Google.}
@@ -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
- before do
13
- allow_any_instance_of(
14
- Faraday::Connection
15
- ).to receive(:get).and_return(response)
16
- end
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
- it "replies with a matching image URL on success" do
19
- allow(response).to receive(:body).and_return(<<-JSON.chomp
21
+ it "replies with a matching image URL on success" do
22
+ allow(response).to receive(:body).and_return(<<-JSON.chomp
20
23
  {
21
- "responseStatus": 200,
22
- "responseData": {
23
- "results": [
24
- {
25
- "unescapedUrl": "http://www.example.com/path/to/an/image"
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
- send_command("image carl")
33
- expect(replies.last).to eq(
34
- "http://www.example.com/path/to/an/image#.png"
35
- )
36
- end
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
- 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
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
- "responseStatus": 200,
42
- "responseData": {
43
- "results": [
44
- {
45
- "unescapedUrl": "http://www.example.com/path/to/an/image.jpg"
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
- send_command("image carl")
53
- expect(replies.last).to eq(
54
- "http://www.example.com/path/to/an/image.jpg"
55
- )
56
- end
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
- it "replies that no images were found if the results are empty" do
59
- allow(response).to receive(:body).and_return(<<-JSON.chomp
60
- {"responseStatus": 200, "responseData": { "results": [] } }
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
- send_command("image carl")
64
- expect(replies.last).to eq(%{No images found for "carl".})
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
- it "logs a warning on failure" do
68
- allow(response).to receive(:body).and_return(<<-JSON.chomp
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
- "responseStatus": 500,
71
- "responseDetails": "google fail"
75
+ "error":
76
+ {
77
+ "code": 500,
78
+ "message": "google fail"
79
+ }
72
80
  }
73
81
  JSON
74
- )
75
- expect(Lita.logger).to receive(:warn).with(/google fail/)
76
- send_command("image carl")
77
- expect(replies).to be_empty
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: 2.0.0
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: 2014-10-23 00:00:00.000000000 Z
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.2.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.