Skip to content

Commit

Permalink
Merge pull request #3 from chriswoodrich/config-exclude-domains
Browse files Browse the repository at this point in the history
Add excluded domains to config, reject any results with matching domains
  • Loading branch information
jimmycuadra committed May 20, 2015
2 parents fa762c2 + ee77f2c commit 16f1007
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) - 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
```

Expand Down
26 changes: 25 additions & 1 deletion lib/lita/handlers/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions spec/lita/handlers/google_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 16f1007

Please sign in to comment.