class RDF::LDP::NonRDFSource

A NonRDFSource describes a `Resource` whose response body is a format other than an RDF serialization. The persistent state of the resource, as represented by the body, is persisted to an IO stream provided by a `RDF::LDP::NonRDFSource::StorageAdapter` given by `#storage`.

In addition to the properties stored by the `RDF::LDP::Resource#metagraph`, `NonRDFSource`s also store a content type (format).

When a `NonRDFSource` is created, it also creates an `RDFSource` which describes it. This resource is created at the URI in `#description_uri`, the resource itself is returned by `#description`.

@see RDF::LDP::Resource @see www.w3.org/TR/ldp/#dfn-linked-data-platform-non-rdf-source for

a definition of NonRDFSource in LDP

Constants

DEFAULT_ADAPTER

Use the default filesystem-based storage adapter

FORMAT_TERM

Use DC elements format

Attributes

storage[R]

Public Class Methods

new(subject_uri, data = RDF::Repository.new, storage_adapter = DEFAULT_ADAPTER) click to toggle source

@param [storage_adapter] a class implementing the StorageAdapter interface

@see RDF::LDP::Resource#initialize

Calls superclass method
# File lib/rdf/ldp/non_rdf_source.rb, line 33
def initialize(subject_uri,
               data            = RDF::Repository.new,
               storage_adapter = DEFAULT_ADAPTER)
  data ||= RDF::Repository.new # allows explict `nil` pass
  @storage = storage_adapter.new(self)
  super(subject_uri, data)
end
to_uri() click to toggle source

@return [RDF::URI] uri with lexical representation

'http://www.w3.org/ns/ldp#NonRDFSource'

@see www.w3.org/TR/ldp/#dfn-linked-data-platform-non-rdf-source

# File lib/rdf/ldp/non_rdf_source.rb, line 46
def self.to_uri
  RDF::Vocab::LDP.NonRDFSource
end

Public Instance Methods

content_type() click to toggle source

@return [StorageAdapter] this resource's content type

# File lib/rdf/ldp/non_rdf_source.rb, line 126
def content_type
  format_triple = metagraph.first([subject_uri, FORMAT_TERM, :format])
  format_triple.nil? ? nil : format_triple.object.object
end
content_type=(content_type) click to toggle source

Sets the MIME type for the resource in `metagraph`.

@param [String] a string representing the content type for this LDP-NR.

This SHOULD be a regisered MIME type.

@return [StorageAdapter] the content type

# File lib/rdf/ldp/non_rdf_source.rb, line 118
def content_type=(content_type)
  metagraph.delete([subject_uri, FORMAT_TERM])
  metagraph <<
    RDF::Statement(subject_uri, FORMAT_TERM, content_type)
end
create(input, c_type) click to toggle source

@param [IO, File] input input (usually from a Rack env's

`rack.input` key) that will be read into the NonRDFSource

@param [#to_s] c_type a MIME content_type used as a content type

for the created NonRDFSource

@raise [RDF::LDP::RequestError] when saving the NonRDFSource

@return [RDF::LDP::NonRDFSource] self

@see RDF::LDP::Resource#create

Calls superclass method
# File lib/rdf/ldp/non_rdf_source.rb, line 67
def create(input, c_type)
  storage.io { |io| IO.copy_stream(input, io) }
  super
  self.content_type = c_type

  RDFSource.new(description_uri, @data)
           .create(StringIO.new, 'application/n-triples')

  self
end
description() click to toggle source

@raise [RDF::LDP::NotFound] if the describedby resource doesn't exist

@return [RDF::LDP::RDFSource] resource describing this resource

# File lib/rdf/ldp/non_rdf_source.rb, line 101
def description
  RDF::LDP::Resource.find(description_uri, @data)
end
description_uri() click to toggle source

@return [RDF::URI] uri for this resource's associated RDFSource

# File lib/rdf/ldp/non_rdf_source.rb, line 107
def description_uri
  subject_uri / '.well-known' / 'desc'
end
destroy() click to toggle source

Deletes the LDP-NR contents from the storage medium and marks the resource as destroyed.

@see RDF::LDP::Resource#destroy

Calls superclass method
# File lib/rdf/ldp/non_rdf_source.rb, line 92
def destroy
  super
  storage.delete
end
non_rdf_source?() click to toggle source

@return [Boolean] whether this is an ldp:NonRDFSource

# File lib/rdf/ldp/non_rdf_source.rb, line 52
def non_rdf_source?
  true
end
to_response() click to toggle source

@return [#each] the response body. This is normally the StorageAdapter's

IO object in read and binary mode.

@raise [RDF::LDP::RequestError] when the request fails

# File lib/rdf/ldp/non_rdf_source.rb, line 136
def to_response
  exists? && !destroyed? ? storage.io : []
end
update(input, c_type) click to toggle source

@see RDF::LDP::Resource#update

Calls superclass method
# File lib/rdf/ldp/non_rdf_source.rb, line 80
def update(input, c_type)
  storage.io { |io| IO.copy_stream(input, io) }
  super
  self.content_type = c_type
  self
end

Private Instance Methods

put(_status, headers, env) click to toggle source

Process & generate response for PUT requsets.

# File lib/rdf/ldp/non_rdf_source.rb, line 144
def put(_status, headers, env)
  raise(PreconditionFailed, 'Etag invalid') if
    env.key?('HTTP_IF_MATCH') && !match?(env['HTTP_IF_MATCH'])

  if exists?
    update(env['rack.input'], env['CONTENT_TYPE'])
    headers = update_headers(headers)
    [200, headers, self]
  else
    create(env['rack.input'], env['CONTENT_TYPE'])
    [201, update_headers(headers), self]
  end
end
update_headers(headers) click to toggle source

@see RDF::LDP::Resource#update_headers

Calls superclass method
# File lib/rdf/ldp/non_rdf_source.rb, line 160
def update_headers(headers)
  headers['Content-Type'] = content_type
  super
end