class Ldp::Resource

Attributes

client[R]
content[RW]
subject[R]

Public Class Methods

for(client, subject, response) click to toggle source
# 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
new(client, subject, response = nil, base_path = '') click to toggle source
# 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

Public Instance Methods

create() { |req| ... } click to toggle source

Create a new resource at the URI @return [RdfSource] the new representation @raise [Ldp::Conflict] if you attempt to call create on an existing resource

# 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) click to toggle source
# 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() click to toggle source

Delete the resource

# 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() click to toggle source

Get the resource

# File lib/ldp/resource.rb, line 53
def get
  @get ||= client.get(subject)
end
head() click to toggle source
# File lib/ldp/resource.rb, line 57
def head
  @head ||= begin
    @get || client.head(subject)
            rescue Ldp::NotFound
              None
  end
end
new?() click to toggle source

Is the resource new, or does it exist in the LDP server?

# File lib/ldp/resource.rb, line 41
def new?
  subject.nil? || head == None
end
reload() click to toggle source

Reload the LDP resource

# File lib/ldp/resource.rb, line 35
def reload
  self.class.new client, subject, @get
end
retrieved_content?() click to toggle source

Have we retrieved the content already?

# File lib/ldp/resource.rb, line 47
def retrieved_content?
  @get
end
save() click to toggle source
# File lib/ldp/resource.rb, line 73
def save
  new? ? create : update
end
subject_uri() click to toggle source

Get the graph subject as a URI

# File lib/ldp/resource.rb, line 29
def subject_uri
  @subject_uri ||= RDF::URI(subject)
end
update(new_content = nil) { |req| ... } click to toggle source

Update the stored graph

# 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) click to toggle source
# 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

Protected Instance Methods

interaction_model() click to toggle source
# File lib/ldp/resource.rb, line 130
def interaction_model
  nil
end