Permalink
Browse files

Add Harpy::Resource.from_urn

  • Loading branch information...
1 parent e916c5c commit 27ad65b1ac8dd2b2ebe1ff91eaa482bce6a2cb9f Joseph HALTER committed Aug 2, 2011
Showing with 44 additions and 8 deletions.
  1. +6 −2 lib/harpy/resource.rb
  2. +1 −1 lib/harpy/version.rb
  3. +37 −5 spec/harpy/resource_spec.rb
View
@@ -43,11 +43,15 @@ def from_url(url)
end
end
- def from_id(id)
- url = Harpy.entry_point.urn urn(id)
+ def from_urn(urn)
+ url = Harpy.entry_point.urn urn
from_url url if url
end
+ def from_id(id)
+ from_urn urn id
+ end
+
def urn(id)
raise NotImplementedError
end
View
@@ -1,3 +1,3 @@
module Harpy
- VERSION = "0.1.5"
+ VERSION = "0.1.6"
end
@@ -117,22 +117,54 @@ def callback_before_destroy
end
end
end
- describe ".from_id(id)" do
- context "when entry point is configured" do
+ describe ".from_urn(urn)" do
+ context "when entry point is not set" do
it "raises Harpy::EntryPointRequired" do
lambda{
- Harpy::Spec::Company.from_id(1)
+ Harpy::Spec::Company.from_urn("urn:harpy:company:1")
}.should raise_error Harpy::EntryPointRequired
end
end
- context "when entry point is set but urn has not been overriden" do
+ 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(:from_url).with(url).and_return(expected = mock)
+ Harpy::Spec::Company.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.from_urn(urn).should be_nil
+ end
+ end
+ end
+ describe ".from_id(id)" do
+ context "when urn has not been overriden" do
it "raises NotImplementedError" do
- Harpy.entry_point_url = "http://localhost"
lambda{
Harpy::Spec::Company.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.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" }

0 comments on commit 27ad65b

Please sign in to comment.