Class: Dayvan::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/dayvan/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Attachment

Returns a new instance of Attachment.



26
27
28
# File 'lib/dayvan/attachment.rb', line 26

def initialize(path)
  self.path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



24
25
26
# File 'lib/dayvan/attachment.rb', line 24

def path
  @path
end

Class Method Details

.all(root) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/dayvan/attachment.rb', line 7

def self.all(root)
  Dir[root.join('**/**')].
    reject {|f| File.directory?(f)}. # Allows symlinks
    inject({}) {|all, f|
      attachment = new(f)
      all[attachment.path.relative_path_from(root)] = attachment
      all
    }
end

.signaturesObject



17
18
19
20
21
22
# File 'lib/dayvan/attachment.rb', line 17

def self.signatures
  all.inject({}) do |signatures, (path, attachment)|
    signatures[path] = attachment.signature
    signatures
  end
end

Instance Method Details

#content_typeObject



30
31
32
# File 'lib/dayvan/attachment.rb', line 30

def content_type
  (MIME::Types.of(path.to_s).first || 'text/plain').to_s
end

#dataObject



34
35
36
# File 'lib/dayvan/attachment.rb', line 34

def data
  Base64.encode64(path.read).delete("\s\n")
end

#nameObject



38
39
40
# File 'lib/dayvan/attachment.rb', line 38

def name
  path.basename('*.*')
end

#signatureObject



46
47
48
# File 'lib/dayvan/attachment.rb', line 46

def signature
  Digest::MD5.hexdigest(data)
end

#to_jsonObject



50
51
52
# File 'lib/dayvan/attachment.rb', line 50

def to_json
  {:content_type => content_type, :data => data}.to_json
end