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.
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) ⇒ 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.
Constructor Details
#initialize(client, subject, response = nil) ⇒ Resource
Returns a new instance of Resource.
9 10 11 12 13 |
# File 'lib/ldp/resource.rb', line 9 def initialize client, subject, response = nil @client = client @subject = subject @get = response if response.is_a? Faraday::Response and current? response 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 |
Instance Method Details
#create(&block) ⇒ RdfSource
Create a new resource at the URI
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ldp/resource.rb', line 66 def create &block raise "Can't call create on an existing resource" unless new? resp = client.post((subject || ""), content) do |req| yield req if block_given? end @subject = resp.headers['Location'] @subject_uri = nil reload end |
#current?(response = nil) ⇒ Boolean
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ldp/resource.rb', line 87 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
53 54 55 56 57 |
# File 'lib/ldp/resource.rb', line 53 def delete client.delete subject do |req| req.headers['If-Match'] = get.etag if retrieved_content? end end |
#get ⇒ Object
Get the resource
43 44 45 |
# File 'lib/ldp/resource.rb', line 43 def get @get ||= client.get(subject) end |
#head ⇒ Object
47 48 49 |
# File 'lib/ldp/resource.rb', line 47 def head @head ||= @get || client.head(subject) end |
#new? ⇒ Boolean
Is the resource new, or does it exist in the LDP server?
29 30 31 32 33 |
# File 'lib/ldp/resource.rb', line 29 def new? subject.nil? || !head rescue Ldp::NotFound true end |
#reload ⇒ Object
Reload the LDP resource
23 24 25 |
# File 'lib/ldp/resource.rb', line 23 def reload self.class.new client, subject, @get end |
#retrieved_content? ⇒ Boolean
Have we retrieved the content already?
37 38 39 |
# File 'lib/ldp/resource.rb', line 37 def retrieved_content? @get end |
#save ⇒ Object
59 60 61 |
# File 'lib/ldp/resource.rb', line 59 def save new? ? create : update end |
#subject_uri ⇒ Object
Get the graph subject as a URI
17 18 19 |
# File 'lib/ldp/resource.rb', line 17 def subject_uri @subject_uri ||= RDF::URI.new subject end |
#update(new_content = nil) ⇒ Object
Update the stored graph
80 81 82 83 84 85 |
# File 'lib/ldp/resource.rb', line 80 def update new_content = nil new_content ||= content client.put subject, new_content do |req| req.headers['If-Match'] = get.etag if retrieved_content? end end |