From ce2948a317ca9da0f699237ebf948bb3a8e11e65 Mon Sep 17 00:00:00 2001 From: Chris Woodrich Date: Fri, 1 May 2015 00:29:10 -0700 Subject: [PATCH 1/2] Add excluded domains to config, reject any results with matching domains Simpler URI checking with URI.parse, sytlistic changes Error handling for URI.parse Error handling Move domain checking logic into private method, use enumerable #find instead of #each Refactor domain checking, rescue from URI errors Refactor domain checking, rescue from URI errors --- README.md | 3 ++ lib/lita/handlers/google.rb | 28 +++++++++++++++- spec/lita/handlers/google_spec.rb | 54 +++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 279135e..a4f2bbd 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,14 @@ gem "lita-google" * `safe_search` (String, Symbol) - The safe search setting to use. Possible values are `:active`, `:moderate`, and `:off`. Default: `:active`. +* `excluded_domains` (Array) - Domains from which you never want to see results. + ### Example ``` Lita.configure do |config| config.handlers.google.safe_search = :off + config.excluded_domains = ["gawker.com", "funnyjunk.com", "dailymail.co.uk"] end ``` diff --git a/lib/lita/handlers/google.rb b/lib/lita/handlers/google.rb index 8050efd..4d39cf5 100644 --- a/lib/lita/handlers/google.rb +++ b/lib/lita/handlers/google.rb @@ -17,12 +17,15 @@ class Google < Handler end end + config :excluded_domains, type: Array + route(/^(?:google|g)\s+(.+)/i, :search, command: true, help: { "google QUERY" => "Return the first Google search result for QUERY." }) def search(response) query = response.matches[0][0] + result = nil http_response = http.get( URL, @@ -33,7 +36,12 @@ def search(response) if http_response.status == 200 data = MultiJson.load(http_response.body) - result = data["responseData"]["results"].first + + if config.excluded_domains + result = check_for_excluded_domains(data) + else + result = data["responseData"]["results"].first + end if result response.reply( @@ -48,6 +56,24 @@ def search(response) ) end end + private + + def check_for_excluded_domains(data) + data["responseData"]["results"].find do |response| + begin + uri = URI.parse(response["unescapedUrl"]) + if uri && uri.host + unless config.excluded_domains.any? { |domain| uri.host.include?(domain) } + response + end + else + response + end + rescue URI::InvalidURIError + nil + end + end + end end Lita.register_handler(Google) diff --git a/spec/lita/handlers/google_spec.rb b/spec/lita/handlers/google_spec.rb index 6dc6d5e..580ef97 100644 --- a/spec/lita/handlers/google_spec.rb +++ b/spec/lita/handlers/google_spec.rb @@ -62,5 +62,59 @@ send_command("google ruby") end + + it "skips over domains that are blacklisted config" do + registry.config.handlers.google.excluded_domains = ['funnyjunk.com' ,'gawker.com'] + + allow(response).to receive(:body).and_return( +<<-JSON.chomp +{ + "responseData": { + "results": [ + { + "unescapedUrl": "http://www.funnyjunk.com", + "titleNoFormatting": "Funny pictures blah blah" + }, + { + "unescapedUrl": "http://theoatmeal.com/blog/funnyjunk2", + "titleNoFormatting": "An update on the FunnyJunk situation" + } + ] + } +} +JSON + ) + + send_command("google funnyjunk") + + expect(replies.last).to eq( + "An update on the FunnyJunk situation - http://theoatmeal.com/blog/funnyjunk2" + ) + end + it "fails gracefully if URI.parse raises an error" do + registry.config.handlers.google.excluded_domains = ['dailmail.co.uk'] + + allow(response).to receive(:body).and_return( +<<-JSON.chomp +{ + "responseData": { + "results": [ + { + "unescapedUrl": "555-867-5309", + "titleNoFormatting": "Funny pictures blah blah" + } + ] + } +} +JSON + ) + + send_command("google funnyjunk") + + expect(replies.last).to eq( + "Funny pictures blah blah - 555-867-5309" + ) + end + end end From ee77f2cbb86fc28ac0b25739751697c7edd22167 Mon Sep 17 00:00:00 2001 From: Chris Woodrich Date: Fri, 8 May 2015 16:09:56 -0700 Subject: [PATCH 2/2] Refactor domain checking, rescue from URI errors --- lib/lita/handlers/google.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/lita/handlers/google.rb b/lib/lita/handlers/google.rb index 4d39cf5..95e4be3 100644 --- a/lib/lita/handlers/google.rb +++ b/lib/lita/handlers/google.rb @@ -63,9 +63,7 @@ def check_for_excluded_domains(data) begin uri = URI.parse(response["unescapedUrl"]) if uri && uri.host - unless config.excluded_domains.any? { |domain| uri.host.include?(domain) } - response - end + response if config.excluded_domains.none? { |domain| uri.host.include?(domain) } else response end