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..95e4be3 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,22 @@ 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 + response if config.excluded_domains.none? { |domain| uri.host.include?(domain) } + 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