Class: Kanal::Plugins::Batteries::Attachments::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/kanal/plugins/batteries/attachments/attachment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Attachment

Returns a new instance of Attachment.



14
15
16
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 14

def initialize(url)
  @url = url
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Extension checks like jpg?, mp3?, mp4?, doc? etc. fall here



19
20
21
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 19

def method_missing(method)
  extension == method.to_s.delete("?")
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 12

def url
  @url
end

Instance Method Details

#audio?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 27

def audio?
  [mp3?, wav?, ogg?].any?
end

#document?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 35

def document?
  [doc?, docx?, odf?].any?
end

#extensionString?

Method that returns extension of url file if possible For example calling extension 123.txt?something=1 will return ‘txt’

Returns:

  • (String, nil)


45
46
47
48
49
50
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 45

def extension
  uri = URI.parse(@url)
  return nil if uri.path.nil?

  File.extname(uri.path).split(".").last if File.basename(uri.path).include? "."
end

#image?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 23

def image?
  [jpg?, jpeg?, png?, bmp?, gif?].any?
end

#quick_save(directory, create_dir = false, filename_length = 32) ⇒ String

Saves file. End user provides directory only. Filename gets generated, extension is read from url.

Parameters:

  • <Directory> (String)
  • <Should (Boolean)

    directories be created or not>

  • <Length (Integer)

    of filename to generate>

Returns:

  • (String)

    Full filepath to saved file



75
76
77
78
79
80
81
82
83
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 75

def quick_save(directory, create_dir = false, filename_length = 32)
  filename = generate_filename filename_length, extension

  return quick_save directory, create_dir, filename_length if File.exist? filename

  save directory + filename, create_dir

  directory + filename
end

#save(filepath, create_dirs = false) ⇒ void

This method returns an undefined value.

Saves file to specified path. End user provides full filepath.

Parameters:

  • <Full (String)

    filepath>

  • <Should (Boolean)

    directories be created or not>



60
61
62
63
64
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 60

def save(filepath, create_dirs = false)
  stream = URI.open(@url)

  save_stream_to_file stream, filepath, create_dirs
end

#video?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 31

def video?
  [mp4?, mov?, mkv?].any?
end