Class: RDF::LDP::NonRDFSource

Inherits:
Resource
  • Object
show all
Defined in:
lib/rdf/ldp/non_rdf_source.rb,
lib/rdf/ldp/storage_adapters/file_storage_adapter.rb

Overview

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`.

Defined Under Namespace

Classes: FileStorageAdapter

Constant Summary collapse

DEFAULT_ADAPTER =

Use the default filesystem-based storage adapter

RDF::LDP::NonRDFSource::FileStorageAdapter
FORMAT_TERM =

Use DC elements format

RDF::Vocab::DC11.format.freeze

Constants inherited from Resource

Resource::CONTAINS_URI, Resource::INVALIDATED_AT_URI, Resource::MODIFIED_URI

Instance Attribute Summary collapse

Attributes inherited from Resource

#metagraph, #subject_uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#allowed_methods, #container?, #containers, #destroyed?, #etag, #exists?, find, gen_id, interaction_model, #last_modified, #ldp_resource?, #match?, metagraph_name, #rdf_source?, #request, #to_uri

Constructor Details

#initialize(subject_uri, data = RDF::Repository.new, storage_adapter = DEFAULT_ADAPTER) ⇒ NonRDFSource

Returns a new instance of NonRDFSource.

Parameters:

  • subject_uri (RDF::URI)
  • data (RDF::Queryable) (defaults to: RDF::Repository.new)
  • storage_adapter (StorageAdapter) (defaults to: DEFAULT_ADAPTER)

    a class implementing the StorageAdapter interface

See Also:



35
36
37
38
39
40
41
# File 'lib/rdf/ldp/non_rdf_source.rb', line 35

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

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



21
22
23
# File 'lib/rdf/ldp/non_rdf_source.rb', line 21

def storage
  @storage
end

Class Method Details

.to_uriRDF::URI

Returns uri with lexical representation ‘www.w3.org/ns/ldp#NonRDFSource’.

Returns:

See Also:



48
49
50
# File 'lib/rdf/ldp/non_rdf_source.rb', line 48

def self.to_uri
  RDF::Vocab::LDP.NonRDFSource
end

Instance Method Details

#content_typeStorageAdapter

Returns this resource’s content type.

Returns:

  • (StorageAdapter)

    this resource’s content type



128
129
130
131
# File 'lib/rdf/ldp/non_rdf_source.rb', line 128

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) ⇒ StorageAdapter

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

Parameters:

  • content_type (String)

    a string representing the content type for this LDP-NR. This SHOULD be a regisered MIME type.

Returns:

  • (StorageAdapter)

    the content type



120
121
122
123
124
# File 'lib/rdf/ldp/non_rdf_source.rb', line 120

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) ⇒ RDF::LDP::NonRDFSource

Returns self.

Parameters:

  • input (IO, File)

    input (usually from a Rack env’s ‘rack.input` key) that will be read into the NonRDFSource

  • c_type (#to_s)

    a MIME content_type used as a content type for the created NonRDFSource

Returns:

Raises:

See Also:



69
70
71
72
73
74
75
76
77
78
# File 'lib/rdf/ldp/non_rdf_source.rb', line 69

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

#descriptionRDF::LDP::RDFSource

Returns resource describing this resource.

Returns:

Raises:



103
104
105
# File 'lib/rdf/ldp/non_rdf_source.rb', line 103

def description
  RDF::LDP::Resource.find(description_uri, @data)
end

#description_uriRDF::URI

Returns uri for this resource’s associated RDFSource.

Returns:

  • (RDF::URI)

    uri for this resource’s associated RDFSource



109
110
111
# File 'lib/rdf/ldp/non_rdf_source.rb', line 109

def description_uri
  subject_uri / '.well-known' / 'desc'
end

#destroyObject

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

See Also:



94
95
96
97
# File 'lib/rdf/ldp/non_rdf_source.rb', line 94

def destroy
  super
  storage.delete
end

#non_rdf_source?Boolean

Returns whether this is an ldp:NonRDFSource.

Returns:

  • (Boolean)

    whether this is an ldp:NonRDFSource



54
55
56
# File 'lib/rdf/ldp/non_rdf_source.rb', line 54

def non_rdf_source?
  true
end

#to_response#each

Returns the response body. This is normally the StorageAdapter’s IO object in read and binary mode.

Returns:

  • (#each)

    the response body. This is normally the StorageAdapter’s IO object in read and binary mode.

Raises:



138
139
140
# File 'lib/rdf/ldp/non_rdf_source.rb', line 138

def to_response
  exists? && !destroyed? ? storage.io : []
end

#update(input, c_type) ⇒ Object

See Also:



82
83
84
85
86
87
# File 'lib/rdf/ldp/non_rdf_source.rb', line 82

def update(input, c_type)
  storage.io { |io| IO.copy_stream(input, io) }
  super
  self.content_type = c_type
  self
end