Class: Tilia::Dav::SimpleFile

Inherits:
File show all
Defined in:
lib/tilia/dav/simple_file.rb

Overview

SimpleFile

The ‘SimpleFile’ class is used to easily add read-only immutable files to the directory structure. One usecase would be to add a ‘readme.txt’ to a root of a webserver with some standard content.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from File

#put

Methods included from IFile

#put

Methods included from INode

#delete, #last_modified

Methods inherited from Node

#delete, #last_modified

Constructor Details

#initialize(name, contents, mime_type = nil) ⇒ SimpleFile

Creates this node

The name of the node must be passed, as well as the contents of the file.

Parameters:

  • string

    name

  • string

    contents

  • string|nil

    mime_type



33
34
35
36
37
# File 'lib/tilia/dav/simple_file.rb', line 33

def initialize(name, contents, mime_type = nil)
  @name = name
  @contents = contents
  @mime_type = mime_type
end

Instance Attribute Details

#contentsObject

File contents



13
14
15
# File 'lib/tilia/dav/simple_file.rb', line 13

def contents
  @contents
end

#mime_typeObject

A mimetype, such as ‘text/plain’ or ‘text/html’



23
24
25
# File 'lib/tilia/dav/simple_file.rb', line 23

def mime_type
  @mime_type
end

#nameObject

Returns the node name for this file.

This name is used to construct the url.

Returns:

  • string



18
19
20
# File 'lib/tilia/dav/simple_file.rb', line 18

def name
  @name
end

Instance Method Details

#content_typeObject

Returns the mime-type for a file

If null is returned, we’ll assume application/octet-stream

Returns:

  • string



77
78
79
# File 'lib/tilia/dav/simple_file.rb', line 77

def content_type
  @mime_type
end

#etagObject

Returns the ETag for a file

An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change. The ETag is an arbitrary string, but MUST be surrounded by double-quotes.

Return null if the ETag can not effectively be determined

Returns:

  • string



69
70
71
# File 'lib/tilia/dav/simple_file.rb', line 69

def etag
  '"' + Digest::SHA1.hexdigest(@contents) + '"'
end

#getObject

Returns the data

This method may either return a string or a readable stream resource

Returns:

  • mixed



51
52
53
# File 'lib/tilia/dav/simple_file.rb', line 51

def get
  @contents
end

#sizeObject

Returns the size of the file, in bytes.

Returns:

  • int



58
59
60
# File 'lib/tilia/dav/simple_file.rb', line 58

def size
  @contents.bytes.size
end