Class: Ldp::Resource
- Inherits:
-
Object
- Object
- Ldp::Resource
- Defined in:
- lib/ldp/resource.rb
Direct Known Subclasses
Defined Under Namespace
Classes: BinarySource, RdfSource
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#content ⇒ Object
Returns the value of attribute content.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Class Method Summary collapse
Instance Method Summary collapse
-
#create(&block) ⇒ RdfSource
Create a new resource at the URI.
- #current?(response = nil) ⇒ Boolean
-
#delete ⇒ Object
Delete the resource.
-
#get ⇒ Object
Get the resource.
- #head ⇒ Object
-
#initialize(client, subject, response = nil, base_path = '') ⇒ Resource
constructor
A new instance of Resource.
-
#new? ⇒ Boolean
Is the resource new, or does it exist in the LDP server?.
-
#reload ⇒ Object
Reload the LDP resource.
-
#retrieved_content? ⇒ Boolean
Have we retrieved the content already?.
- #save ⇒ Object
-
#subject_uri ⇒ Object
Get the graph subject as a URI.
-
#update(new_content = nil) ⇒ Object
Update the stored graph.
- #update_cached_get(response) ⇒ Object
Constructor Details
#initialize(client, subject, response = nil, base_path = '') ⇒ Resource
Returns a new instance of Resource.
20 21 22 23 24 25 |
# File 'lib/ldp/resource.rb', line 20 def initialize client, subject, response = nil, base_path = '' @client = client @subject = subject @get = response if response.is_a? Faraday::Response and current? response @base_path = base_path end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/ldp/resource.rb', line 6 def client @client end |
#content ⇒ Object
Returns the value of attribute content.
7 8 9 |
# File 'lib/ldp/resource.rb', line 7 def content @content end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
6 7 8 |
# File 'lib/ldp/resource.rb', line 6 def subject @subject end |
Class Method Details
.for(client, subject, response) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ldp/resource.rb', line 9 def self.for(client, subject, response) case when response.container? Ldp::Container.for client, subject, response when response.rdf_source? Resource::RdfSource.new client, subject, response else Resource::BinarySource.new client, subject, response end end |
Instance Method Details
#create(&block) ⇒ RdfSource
Create a new resource at the URI
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ldp/resource.rb', line 81 def create &block raise Ldp::Conflict, "Can't call create on an existing resource (#{subject})" unless new? verb = subject.nil? ? :post : :put resp = client.send(verb, (subject || @base_path), content) do |req| req.headers["Link"] = "<#{interaction_model}>;rel=\"type\"" if interaction_model yield req if block_given? end @subject = resp.headers['Location'] @subject_uri = nil reload end |
#current?(response = nil) ⇒ Boolean
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ldp/resource.rb', line 106 def current? response = nil response ||= @get return true if new? and subject.nil? new_response = client.head(subject) response.headers['ETag'] && response.headers['Last-Modified'] && new_response.headers['ETag'] == response.headers['ETag'] && new_response.headers['Last-Modified'] == response.headers['Last-Modified'] end |
#delete ⇒ Object
Delete the resource
67 68 69 70 71 |
# File 'lib/ldp/resource.rb', line 67 def delete client.delete subject do |req| req.headers['If-Unmodified-Since'] = get.last_modified if retrieved_content? end end |
#get ⇒ Object
Get the resource
53 54 55 |
# File 'lib/ldp/resource.rb', line 53 def get @get ||= client.get(subject) end |
#head ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/ldp/resource.rb', line 57 def head @head ||= begin @get || client.head(subject) rescue Ldp::NotFound None end end |
#new? ⇒ Boolean
Is the resource new, or does it exist in the LDP server?
41 42 43 |
# File 'lib/ldp/resource.rb', line 41 def new? subject.nil? || head == None end |
#reload ⇒ Object
Reload the LDP resource
35 36 37 |
# File 'lib/ldp/resource.rb', line 35 def reload self.class.new client, subject, @get end |
#retrieved_content? ⇒ Boolean
Have we retrieved the content already?
47 48 49 |
# File 'lib/ldp/resource.rb', line 47 def retrieved_content? @get end |
#save ⇒ Object
73 74 75 |
# File 'lib/ldp/resource.rb', line 73 def save new? ? create : update end |
#subject_uri ⇒ Object
Get the graph subject as a URI
29 30 31 |
# File 'lib/ldp/resource.rb', line 29 def subject_uri @subject_uri ||= RDF::URI(subject) end |
#update(new_content = nil) ⇒ Object
Update the stored graph
96 97 98 99 100 101 102 103 104 |
# File 'lib/ldp/resource.rb', line 96 def update new_content = nil new_content ||= content resp = client.put subject, new_content do |req| req.headers['If-Unmodified-Since'] = get.last_modified if retrieved_content? yield req if block_given? end update_cached_get(resp) if retrieved_content? resp end |
#update_cached_get(response) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/ldp/resource.rb', line 118 def update_cached_get(response) response = Response.new(response) if response.etag.nil? || response.last_modified.nil? response = client.head(subject) end @get.etag = response.etag @get.last_modified = response.last_modified end |