diff --git a/lib/ldp/resource.rb b/lib/ldp/resource.rb index 2699752..6a36778 100644 --- a/lib/ldp/resource.rb +++ b/lib/ldp/resource.rb @@ -65,7 +65,8 @@ def save # @return [RdfSource] the new representation def create &block raise "Can't call create on an existing resource" unless new? - resp = client.post((subject || ""), content) do |req| + verb = subject.nil? ? :post : :put + resp = client.send(verb, (subject || ""), content) do |req| yield req if block_given? end @@ -79,9 +80,11 @@ def create &block # Update the stored graph def update new_content = nil new_content ||= content - client.put subject, new_content do |req| + resp = client.put subject, new_content do |req| req.headers['If-Match'] = get.etag if retrieved_content? end + update_cached_get(resp) if retrieved_content? + resp end def current? response = nil @@ -95,5 +98,15 @@ def current? response = nil new_response.headers['ETag'] == response.headers['ETag'] && new_response.headers['Last-Modified'] == response.headers['Last-Modified'] end + + def update_cached_get response + Response.wrap(client, response) + if response.etag.nil? || response.last_modified.nil? + response = Response.wrap(client, client.head(subject)) + end + @get.etag = response.etag + @get.last_modified = response.last_modified + end + end end diff --git a/lib/ldp/response.rb b/lib/ldp/response.rb index 23d3ce7..ad9076d 100644 --- a/lib/ldp/response.rb +++ b/lib/ldp/response.rb @@ -113,13 +113,21 @@ def graph ## # Extract the ETag for the resource def etag - headers['ETag'] + @etag ||= headers['ETag'] + end + + def etag=(val) + @etag = val end ## # Extract the last modified header for the resource def last_modified - headers['Last-Modified'] + @last_modified ||= headers['Last-Modified'] + end + + def last_modified=(val) + @last_modified = val end ## diff --git a/spec/lib/ldp/resource/rdf_source_spec.rb b/spec/lib/ldp/resource/rdf_source_spec.rb index 7f8599e..6e2300c 100644 --- a/spec/lib/ldp/resource/rdf_source_spec.rb +++ b/spec/lib/ldp/resource/rdf_source_spec.rb @@ -9,7 +9,7 @@ Faraday::Adapter::Test::Stubs.new do |stub| # stub.get('/a_resource') {[ 200, {"Link" => ";rel=\"type\""}, simple_graph ]} stub.post("/") { [201]} - stub.post("/abs_url_object") { [201]} + stub.put("/abs_url_object") { [201]} end end diff --git a/spec/lib/ldp/resource_spec.rb b/spec/lib/ldp/resource_spec.rb index d51676a..bb82cfb 100644 --- a/spec/lib/ldp/resource_spec.rb +++ b/spec/lib/ldp/resource_spec.rb @@ -61,7 +61,7 @@ context "with initial content" do let(:path) { '/a_new_resource' } it "should post an RDF graph" do - mock_client.should_receive(:post).with(path, "xyz").and_return(double(headers: {})) + mock_client.should_receive(:put).with(path, "xyz").and_return(double(headers: {})) subject.content = "xyz" subject.save end