Permalink
Browse files

Adds error handling for incorrect search parameters

  • Loading branch information...
1 parent 9d40ea3 commit 090f080f75f90b5b9cf03f9cbb30ab1e00377c55 @vesan committed Jun 17, 2012
Showing with 33 additions and 8 deletions.
  1. +11 −0 lib/kippt/clips.rb
  2. +22 −8 spec/kippt/clips_spec.rb
View
@@ -5,6 +5,7 @@
class Kippt::Clips
include Kippt::CollectionResource
+ VALID_SEARCH_PARAMETERS = [:q, :list, :is_starred]
def initialize(client)
@client = client
@@ -29,9 +30,19 @@ def search(parameters)
@client.get("search/clips", {:q => parameters}).body,
self)
else
+ validate_search_parameters(parameters)
+
Kippt::ClipCollection.new(
@client.get("search/clips", parameters).body,
self)
end
end
+
+ private
+
+ def validate_search_parameters(parameters)
+ parameters.each do |key, value|
+ raise ArgumentError.new("'#{key}' is not a valid search parameter") unless VALID_SEARCH_PARAMETERS.include?(key)
+ end
+ end
end
@@ -14,16 +14,30 @@
subject { Kippt::Client.new(valid_user_credentials).clips }
describe "parameters" do
- it "accepts String" do
- stub_get("/search/clips?q=bunny").
- to_return(:status => 200, :body => fixture("clips.json"))
- subject.search("bunny")
+ context "when parameter is a String" do
+ it "does the search with the string being q" do
+ stub_get("/search/clips?q=bunny").
+ to_return(:status => 200, :body => fixture("clips.json"))
+ subject.search("bunny")
+ end
end
- it "accepts Hash" do
- stub_get("/search/clips?q=bunny&list=4&is_starred=true").
- to_return(:status => 200, :body => fixture("clips.json"))
- subject.search(:q => "bunny", :list => 4, :is_starred => true)
+ context "when parameter is a Hash" do
+ context "with accepted keys" do
+ it "does the search" do
+ stub_get("/search/clips?q=bunny&list=4&is_starred=true").
+ to_return(:status => 200, :body => fixture("clips.json"))
+ subject.search(:q => "bunny", :list => 4, :is_starred => true)
+ end
+ end
+
+ context "with invalid keys" do
+ it "raises ArgumentError" do
+ lambda {
+ subject.search(:q => "bunny", :stuff => true)
+ }.should raise_error(ArgumentError, "'stuff' is not a valid search parameter")
+ end
+ end
end
end

0 comments on commit 090f080

Please sign in to comment.