Class: Kanal::Plugins::Batteries::Attachments::Attachment
- Inherits:
-
Object
- Object
- Kanal::Plugins::Batteries::Attachments::Attachment
- Defined in:
- lib/kanal/plugins/batteries/attachments/attachment.rb
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #audio? ⇒ Boolean
- #document? ⇒ Boolean
-
#extension ⇒ String?
Method that returns extension of url file if possible For example calling extension 123.txt?something=1 will return ‘txt’.
- #image? ⇒ Boolean
-
#initialize(url) ⇒ Attachment
constructor
A new instance of Attachment.
-
#method_missing(method) ⇒ Object
Extension checks like jpg?, mp3?, mp4?, doc? etc.
-
#quick_save(directory, create_dir = false, filename_length = 32) ⇒ String
Saves file.
-
#save(filepath, create_dirs = false) ⇒ void
Saves file to specified path.
- #video? ⇒ Boolean
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
#url ⇒ Object (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
27 28 29 |
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 27 def audio? [mp3?, wav?, ogg?].any? end |
#document? ⇒ Boolean
35 36 37 |
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 35 def document? [doc?, docx?, odf?].any? end |
#extension ⇒ String?
Method that returns extension of url file if possible For example calling extension 123.txt?something=1 will return ‘txt’
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
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.
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.
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
31 32 33 |
# File 'lib/kanal/plugins/batteries/attachments/attachment.rb', line 31 def video? [mp4?, mov?, mkv?].any? end |