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:

  • a (storage_adapter)

    class implementing the StorageAdapter interface

See Also:



33
34
35
36
37
38
39
# 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

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:



46
47
48
# File 'lib/rdf/ldp/non_rdf_source.rb', line 46

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



126
127
128
129
# 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) ⇒ StorageAdapter

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

Parameters:

  • a (String)

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

Returns:

  • (StorageAdapter)

    the content type



118
119
120
121
122
# 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) ⇒ 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:



67
68
69
70
71
72
73
74
75
76
# 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

#descriptionRDF::LDP::RDFSource

Returns resource describing this resource.

Returns:

Raises:



101
102
103
# File 'lib/rdf/ldp/non_rdf_source.rb', line 101

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



107
108
109
# File 'lib/rdf/ldp/non_rdf_source.rb', line 107

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:



92
93
94
95
# File 'lib/rdf/ldp/non_rdf_source.rb', line 92

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



52
53
54
# File 'lib/rdf/ldp/non_rdf_source.rb', line 52

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:



136
137
138
# File 'lib/rdf/ldp/non_rdf_source.rb', line 136

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

#update(input, c_type) ⇒ Object

See Also:



80
81
82
83
84
85
# 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