Class: Medusa::File

Inherits:
Node
  • Object
show all
Includes:
Resource
Defined in:
lib/medusa/file.rb

Overview

Represents a Medusa file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

#exists?, #id, #uuid

Methods included from Uuidable

#uuid_url

Methods inherited from Node

#name, #relative_key

Constructor Details

#initializeFile

Returns a new instance of File.



51
52
53
54
# File 'lib/medusa/file.rb', line 51

def initialize
  super
  @loading = @loaded = false
end

Instance Attribute Details

#jsonObject

Returns the value of attribute json.



10
11
12
# File 'lib/medusa/file.rb', line 10

def json
  @json
end

Class Method Details

.from_json(json) ⇒ Medusa::File

Initializes a new instance from a JSON fragment.

Parameters:

  • json (Hash)

Returns:



18
19
20
21
22
23
# File 'lib/medusa/file.rb', line 18

def self.from_json(json)
  file = Medusa::File.new
  file.json = json
  file.load
  file
end

.with_id(id) ⇒ Medusa::File

Returns a new instance with the given ID. Existence is not checked, so an instance is returned regardless of whether the ID is valid.

Parameters:

  • id (Integer)

Returns:



32
33
34
35
36
# File 'lib/medusa/file.rb', line 32

def self.with_id(id)
  file = Medusa::File.new
  file.instance_variable_set('@id', id)
  file
end

.with_uuid(uuid) ⇒ Medusa::File

Returns a new instance with the given UUID. Existence is not checked, so an instance is returned regardless of whether the UUID is valid.

Parameters:

  • uuid (String)

Returns:



45
46
47
48
49
# File 'lib/medusa/file.rb', line 45

def self.with_uuid(uuid)
  file = Medusa::File.new
  file.instance_variable_set('@uuid', uuid)
  file
end

Instance Method Details

#directoryMedusa::Directory

Returns:



59
60
61
62
# File 'lib/medusa/file.rb', line 59

def directory
  load
  @directory ||= ::Medusa::Directory.with_id(@directory_id)
end

#loadObject

Updates the instance with current properties from Medusa.

It should not typically be necessary to use this method publicly.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/medusa/file.rb', line 101

def load
  return if @loading || @loaded
  @loading      = true
  struct        = json ? json : fetch_body
  @id           = struct['id']
  @md5_sum      = struct['md5_sum']
  @media_type   = struct['content_type']
  @mtime        = Time.parse(struct['mtime']) if struct['mtime']
  @relative_key = struct['relative_pathname']
  @size         = struct['size']
  @uuid         = struct['uuid']  # this will only be present in the /cfs_files/:id representation, not the
  # one nested in /cfs_directories

  if struct['directory'] and struct['directory']['uuid']
    @directory_id = struct['directory']['id']
  end
  @loaded = true
ensure
  @loading = false
end

#md5_sumString

Returns:

  • (String)


67
68
69
70
# File 'lib/medusa/file.rb', line 67

def md5_sum
  load
  @md5_sum
end

#media_typeString

Returns:

  • (String)


75
76
77
78
# File 'lib/medusa/file.rb', line 75

def media_type
  load
  @media_type
end

#mtimeTime

Returns:

  • (Time)


83
84
85
86
# File 'lib/medusa/file.rb', line 83

def mtime
  load
  @mtime
end

#sizeInteger

Returns:

  • (Integer)


91
92
93
94
# File 'lib/medusa/file.rb', line 91

def size
  load
  @size
end

#urlString

Returns Absolute URI of the corresponding Medusa resource.

Returns:

  • (String)

    Absolute URI of the corresponding Medusa resource.



125
126
127
128
129
# File 'lib/medusa/file.rb', line 125

def url
  [::Medusa::Client.configuration[:medusa_base_url].chomp('/'),
   'cfs_files',
   self.id].join('/')
end