lita-google 1.0.0 → 1.1.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: b4079fc2e7915b16fec653ee9d7d7a4cc66a5ede
4
- data.tar.gz: 0119e80fa94736073a9739433cddbe58419d1cb3
3
+ metadata.gz: 71adaf250d1a9a8e8e61d384bcf354bb27dacf0b
4
+ data.tar.gz: ef3dab0c4ba35d7d8893726b26b86e1428d29f22
5
5
  SHA512:
6
- metadata.gz: 64ac5a07542ac85e256a5630fa8490e3bfad184e95d47a1d3cda5073ba73da97c8649cebf654a74040848da945a1a35252b3032960dc27933c6db277e743ba2b
7
- data.tar.gz: de3541305ed634f5ddea55afcbd13156f19d4fa46233ee367a5b5e5d3028662eab0475d57d9d33f7d4fe623d2754a3c709e77978c00c303e2b3c183f58851419
6
+ metadata.gz: a1484e9c7199cb39f54b4e73cddeb6cf5234cf1bfef80032d0b0ce9050452852745c354fb79bbc964561ca04ddf8eb852d6ba9e3e5bdc4a1bb6656448364e853
7
+ data.tar.gz: 09324813ab5b0f012851fa68dd8faf33f56a9cab74a96ec5a543dedb5abca722d959044da5d12aa736e22286416d055242ad5825a4b56b8482cb7856d3b188ea
data/README.md CHANGED
@@ -20,11 +20,14 @@ gem "lita-google"
20
20
 
21
21
  * `safe_search` (String, Symbol) - The safe search setting to use. Possible values are `:active`, `:moderate`, and `:off`. Default: `:active`.
22
22
 
23
+ * `excluded_domains` (Array<String>) - Domains from which you never want to see results.
24
+
23
25
  ### Example
24
26
 
25
27
  ```
26
28
  Lita.configure do |config|
27
29
  config.handlers.google.safe_search = :off
30
+ config.excluded_domains = ["gawker.com", "funnyjunk.com", "dailymail.co.uk"]
28
31
  end
29
32
  ```
30
33
 
@@ -17,12 +17,15 @@ module Lita
17
17
  end
18
18
  end
19
19
 
20
+ config :excluded_domains, type: Array
21
+
20
22
  route(/^(?:google|g)\s+(.+)/i, :search, command: true, help: {
21
23
  "google QUERY" => "Return the first Google search result for QUERY."
22
24
  })
23
25
 
24
26
  def search(response)
25
27
  query = response.matches[0][0]
28
+ result = nil
26
29
 
27
30
  http_response = http.get(
28
31
  URL,
@@ -33,7 +36,12 @@ module Lita
33
36
 
34
37
  if http_response.status == 200
35
38
  data = MultiJson.load(http_response.body)
36
- result = data["responseData"]["results"].first
39
+
40
+ if config.excluded_domains
41
+ result = check_for_excluded_domains(data)
42
+ else
43
+ result = data["responseData"]["results"].first
44
+ end
37
45
 
38
46
  if result
39
47
  response.reply(
@@ -48,6 +56,22 @@ module Lita
48
56
  )
49
57
  end
50
58
  end
59
+ private
60
+
61
+ def check_for_excluded_domains(data)
62
+ data["responseData"]["results"].find do |response|
63
+ begin
64
+ uri = URI.parse(response["unescapedUrl"])
65
+ if uri && uri.host
66
+ response if config.excluded_domains.none? { |domain| uri.host.include?(domain) }
67
+ else
68
+ response
69
+ end
70
+ rescue URI::InvalidURIError
71
+ nil
72
+ end
73
+ end
74
+ end
51
75
  end
52
76
 
53
77
  Lita.register_handler(Google)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-google"
3
- spec.version = "1.0.0"
3
+ spec.version = "1.1.0"
4
4
  spec.authors = ["Jimmy Cuadra"]
5
5
  spec.email = ["jimmy@jimmycuadra.com"]
6
6
  spec.description = %q{A Lita handler for returning Google search results.}
@@ -62,5 +62,59 @@ JSON
62
62
 
63
63
  send_command("google ruby")
64
64
  end
65
+
66
+ it "skips over domains that are blacklisted config" do
67
+ registry.config.handlers.google.excluded_domains = ['funnyjunk.com' ,'gawker.com']
68
+
69
+ allow(response).to receive(:body).and_return(
70
+ <<-JSON.chomp
71
+ {
72
+ "responseData": {
73
+ "results": [
74
+ {
75
+ "unescapedUrl": "http://www.funnyjunk.com",
76
+ "titleNoFormatting": "Funny pictures blah blah"
77
+ },
78
+ {
79
+ "unescapedUrl": "http://theoatmeal.com/blog/funnyjunk2",
80
+ "titleNoFormatting": "An update on the FunnyJunk situation"
81
+ }
82
+ ]
83
+ }
84
+ }
85
+ JSON
86
+ )
87
+
88
+ send_command("google funnyjunk")
89
+
90
+ expect(replies.last).to eq(
91
+ "An update on the FunnyJunk situation - http://theoatmeal.com/blog/funnyjunk2"
92
+ )
93
+ end
94
+ it "fails gracefully if URI.parse raises an error" do
95
+ registry.config.handlers.google.excluded_domains = ['dailmail.co.uk']
96
+
97
+ allow(response).to receive(:body).and_return(
98
+ <<-JSON.chomp
99
+ {
100
+ "responseData": {
101
+ "results": [
102
+ {
103
+ "unescapedUrl": "555-867-5309",
104
+ "titleNoFormatting": "Funny pictures blah blah"
105
+ }
106
+ ]
107
+ }
108
+ }
109
+ JSON
110
+ )
111
+
112
+ send_command("google funnyjunk")
113
+
114
+ expect(replies.last).to eq(
115
+ "Funny pictures blah blah - 555-867-5309"
116
+ )
117
+ end
118
+
65
119
  end
66
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-google
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.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-05-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
137
137
  signing_key:
138
138
  specification_version: 4
139
139
  summary: A Lita handler for returning Google search results.