Skip to content
Browse files

Update to version 0.1.7

Add Resource.delete_from_url, Resource.delete_from_urn and Resource.delete_from_id
  • Loading branch information...
1 parent 27ad65b commit 60765c04a8981182d8273d14a75cd362d6a99805 @JonathanTron JonathanTron committed Aug 4, 2011
Showing with 122 additions and 1 deletion.
  1. +24 −0 lib/harpy/resource.rb
  2. +1 −1 lib/harpy/version.rb
  3. +97 −0 spec/harpy/resource_spec.rb
View
24 lib/harpy/resource.rb
@@ -80,6 +80,19 @@ def with_url(url)
result
end
+ def delete_from_url(url)
+ delete_from_url_handler Harpy.client.delete url
+ end
+
+ def delete_from_urn(urn)
+ url = Harpy.entry_point.urn urn
+ url ? delete_from_url(url) : false
+ end
+
+ def delete_from_id(id)
+ delete_from_urn urn id
+ end
+
private
def url
@@ -96,6 +109,17 @@ def from_url_handler(response)
Harpy.client.invalid_code response
end
end
+
+ def delete_from_url_handler(response)
+ case response.code
+ when 204
+ true
+ when 404
+ false
+ else
+ Harpy.client.invalid_code response
+ end
+ end
end
module InstanceMethods
View
2 lib/harpy/version.rb
@@ -1,3 +1,3 @@
module Harpy
- VERSION = "0.1.6"
+ VERSION = "0.1.7"
end
View
97 spec/harpy/resource_spec.rb
@@ -191,6 +191,103 @@ def callback_before_destroy
Harpy::Spec::Company.from_id(1).should be_nil
end
end
+ end
+ describe ".delete_from_url(url)" do
+ context "called with only one url" do
+ let(:url){ "http://localhost/company/1" }
+ it "is true on success" do
+ response = Typhoeus::Response.new :code => 204
+ Harpy.client.should_receive(:delete).with(url).and_return response
+ result = Harpy::Spec::Company.delete_from_url url
+ result.should be_true
+ end
+ it "is false when not found" do
+ response = Typhoeus::Response.new :code => 404
+ Harpy.client.should_receive(:delete).with(url).and_return response
+ Harpy::Spec::Company.delete_from_url(url).should be_false
+ end
+ it "delegates response code != 204 or 404 to client" do
+ response = Typhoeus::Response.new :code => 500
+ Harpy.client.should_receive(:delete).with(url).and_return response
+ Harpy.client.should_receive(:invalid_code).with response
+ Harpy::Spec::Company.delete_from_url url
+ end
+ end
+ end
+ describe ".delete_from_urn(urn)" do
+ context "when entry point is not set" do
+ it "raises Harpy::EntryPointRequired" do
+ lambda{
+ Harpy::Spec::Company.delete_from_urn("urn:harpy:company:1")
+ }.should raise_error Harpy::EntryPointRequired
+ end
+ end
+ context "when entry point is set" do
+ let(:urn) { "urn:harpy:company:1" }
+ let(:url) { "http://localhost/company/1" }
+ it "asks Harpy.entry_point to convert urn to url then call .from_url" do
+ # urn:harpy:company:1 -> http://localhost/company/1
+ Harpy.entry_point = mock
+ Harpy.entry_point.should_receive(:urn).with(urn).and_return url
+
+ # http://localhost/company/1 -> Harpy::Spec::Company instance
+ Harpy::Spec::Company.should_receive(:delete_from_url).with(url).and_return(expected = mock)
+ Harpy::Spec::Company.delete_from_urn(urn).should be expected
+ end
+ it "is nil if urn is not found" do
+ # urn:harpy:company:1 -> nil
+ Harpy.entry_point = mock
+ Harpy.entry_point.should_receive(:urn).with(urn)
+
+ Harpy::Spec::Company.delete_from_urn(urn).should be_false
+ end
+ end
+ end
+ describe ".delete_from_id(id)" do
+ context "when urn has not been overriden" do
+ it "raises NotImplementedError" do
+ lambda{
+ Harpy::Spec::Company.delete_from_id(1)
+ }.should raise_error NotImplementedError
+ end
+ end
+ context "when urn has been overriden but entry point is not set" do
+ let(:urn) { "urn:harpy:company:1" }
+ it "raises Harpy::EntryPointRequired" do
+ # 1 -> urn:harpy:company:1
+ Harpy::Spec::Company.should_receive(:urn).with(1).and_return urn
+
+ lambda{
+ Harpy::Spec::Company.delete_from_id(1)
+ }.should raise_error Harpy::EntryPointRequired
+ end
+ end
+ context "when entry point is set and urn has been overriden" do
+ let(:urn) { "urn:harpy:company:1" }
+ let(:url) { "http://localhost/company/1" }
+ it "asks Harpy.entry_point to convert urn to url then call .from_url" do
+ # 1 -> urn:harpy:company:1
+ Harpy::Spec::Company.should_receive(:urn).with(1).and_return urn
+
+ # urn:harpy:company:1 -> http://localhost/company/1
+ Harpy.entry_point = mock
+ Harpy.entry_point.should_receive(:urn).with(urn).and_return url
+
+ # http://localhost/company/1 -> Harpy::Spec::Company instance
+ Harpy::Spec::Company.should_receive(:delete_from_url).with(url).and_return(expected = mock)
+ Harpy::Spec::Company.delete_from_id(1).should be expected
+ end
+ it "is nil if urn is not found" do
+ # 1 -> urn:harpy:company:1
+ Harpy::Spec::Company.should_receive(:urn).with(1).and_return urn
+
+ # urn:harpy:company:1 -> nil
+ Harpy.entry_point = mock
+ Harpy.entry_point.should_receive(:urn).with(urn)
+
+ Harpy::Spec::Company.delete_from_id(1).should be_false
+ end
+ end
end
describe ".urn(id)" do
it "raises NotImplementedError" do

0 comments on commit 60765c0

Please sign in to comment.
Something went wrong with that request. Please try again.