Skip to content

Commit

Permalink
Use Lita 4 configuration system.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Oct 23, 2014
1 parent c46515c commit fa762c2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
24 changes: 10 additions & 14 deletions lib/lita/handlers/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ module Lita
module Handlers
class Google < Handler
URL = "https://ajax.googleapis.com/ajax/services/search/web"
VALID_SAFE_VALUES = %w(active moderate off)

config :safe_search, types: [String, Symbol], default: :active do
validate do |value|
unless VALID_SAFE_VALUES.include?(value.to_s.strip)
"valid values are :active, :moderate, or :off"
end
end
end

route(/^(?:google|g)\s+(.+)/i, :search, command: true, help: {
"google QUERY" => "Return the first Google search result for QUERY."
})

def self.default_config(handler_config)
handler_config.safe_search = :active
end

def search(response)
query = response.matches[0][0]

http_response = http.get(
URL,
safe: safe_value,
safe: config.safe_search,
q: query,
v: "1.0"
)
Expand All @@ -43,15 +48,6 @@ def search(response)
)
end
end

private

def safe_value
safe = Lita.config.handlers.google.safe_search || "active"
safe = safe.to_s.downcase
safe = "active" unless ["active", "moderate", "off"].include?(safe)
safe
end
end

Lita.register_handler(Google)
Expand Down
6 changes: 3 additions & 3 deletions lita-google.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "lita-google"
spec.version = "0.1.0"
spec.version = "1.0.0"
spec.authors = ["Jimmy Cuadra"]
spec.email = ["jimmy@jimmycuadra.com"]
spec.description = %q{A Lita handler for returning Google search results.}
Expand All @@ -14,11 +14,11 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "lita", ">= 2.6"
spec.add_runtime_dependency "lita", ">= 4.0"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", ">= 3.0.0.beta2"
spec.add_development_dependency "rspec", ">= 3.0.0"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "coveralls"
end
6 changes: 3 additions & 3 deletions spec/lita/handlers/google_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "spec_helper"

describe Lita::Handlers::Google, lita_handler: true do
it { routes_command("google ruby").to(:search) }
it { routes_command("google me ruby").to(:search) }
it { routes_command("g ruby").to(:search) }
it { is_expected.to route_command("google ruby").to(:search) }
it { is_expected.to route_command("google me ruby").to(:search) }
it { is_expected.to route_command("g ruby").to(:search) }

describe "#search" do
let(:response) do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

require "lita-google"
require "lita/rspec"

Lita.version_3_compatibility_mode = false

0 comments on commit fa762c2

Please sign in to comment.