Class: RestFtpDaemon::Helpers
- Inherits:
-
Object
- Object
- RestFtpDaemon::Helpers
- Defined in:
- lib/rest-ftp-daemon/helpers.rb
Class Method Summary collapse
- .dashboard_job_link(job) ⇒ Object
-
.datetime_full(datetime) ⇒ Object
Dates and times: date with time generator.
- .datetime_short(datetime) ⇒ Object
- .extract_dirname(path) ⇒ Object
- .extract_filename(path) ⇒ Object
- .extract_parent(path) ⇒ Object
- .format_bytes(number, unit = "", decimals = 0) ⇒ Object
- .formatted_duration(duration) ⇒ Object
- .get_censored_config ⇒ Object
- .hide_credentials_from_url(path) ⇒ Object
- .highlight_tokens(path) ⇒ Object
- .identifier(len) ⇒ Object
- .job_method_label(method) ⇒ Object
- .job_runs_style(runs) ⇒ Object
- .local_port_used?(port) ⇒ Boolean
- .text_or_empty(text) ⇒ Object
- .tokenize(item) ⇒ Object
Class Method Details
.dashboard_job_link(job) ⇒ Object
135 136 137 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 135 def self.dashboard_job_link job "/jobs/#{job.id}" if job.respond_to? :id end |
.datetime_full(datetime) ⇒ Object
Dates and times: date with time generator
101 102 103 104 105 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 101 def self.datetime_full datetime return "-" if datetime.nil? datetime.to_datetime.strftime("%d.%m.%Y %H:%M:%S") end |
.datetime_short(datetime) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 107 def self.datetime_short datetime # return param.class return "-" if datetime.nil? return "?" unless datetime.respond_to? :to_date return datetime.to_datetime.strftime("%H:%M:%S") if datetime.to_date == Time.now.to_date datetime.to_datetime.strftime("%d/%m %H:%M:%S") end |
.extract_dirname(path) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 50 def self.extract_dirname path return unless path.is_a? String # match all the beginning of the string up to the last slash m = path.match(/^(.*)\/[^\/]*$/) return "/#{m[1]}" unless m.nil? end |
.extract_filename(path) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 43 def self.extract_filename path return unless path.is_a? String # match everything that's after a slash at the end of the string m = path.match(/\/?([^\/]+)$/) return m[1] unless m.nil? end |
.extract_parent(path) ⇒ Object
57 58 59 60 61 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 57 def self.extract_parent path return unless path.is_a? String m = path.match(/^(.*)\/([^\/]+)\/?$/) return m[1], m[2] unless m.nil? end |
.format_bytes(number, unit = "", decimals = 0) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 11 def self.format_bytes number, unit="", decimals = 0 return "Ø" if number.nil? || number.to_f.zero? units = ["", "k", "M", "G", "T", "P" ] index = ( Math.log(number) / Math.log(2) ).to_i / 10 converted = number.to_f / (1024 ** index) truncated = converted.round(decimals) "#{truncated} #{units[index]}#{unit}" end |
.formatted_duration(duration) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 121 def self.formatted_duration duration out = [] hours = duration / (60 * 60) minutes = (duration / 60) % 60 seconds = duration % 60 out << "#{hours}h" if hours > 0 out << "#{minutes}mn" if (minutes > 0) || (hours > 0) out << "#{seconds}s" out.join(" ") end |
.get_censored_config ⇒ Object
4 5 6 7 8 9 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 4 def self.get_censored_config config = Settings.to_hash config[:users] = Settings.users.keys if Settings.users config[:endpoints] = Settings.endpoints.keys if Settings.endpoints config end |
.hide_credentials_from_url(path) ⇒ Object
116 117 118 119 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 116 def self.hide_credentials_from_url path return unless path.is_a? String path.sub(/([a-z]+:\/\/[^\/]+):[^\/]+\@/, '\1@') end |
.highlight_tokens(path) ⇒ Object
38 39 40 41 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 38 def self.highlight_tokens path return unless path.is_a? String path.gsub(/\[([^\[]+)\]/, '<span class="token">\1</span>') end |
.identifier(len) ⇒ Object
29 30 31 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 29 def self.identifier len rand(36**len).to_s(36) end |
.job_method_label(method) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 78 def self.job_method_label method return if method.nil? klass = case method when :file "label-primary" when :ftp "label-warning" when :ftps, :ftpes "label-success" else "label-default" end "<div class=\"transfer-method label #{klass}\">#{method.upcase}</div>" end |
.job_runs_style(runs) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 93 def self.job_runs_style runs return "label-outline" if runs <= 0 return "label-info" if runs == 1 return "label-warning" if runs == 2 return "label-danger" if runs > 2 end |
.local_port_used?(port) ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 63 def self.local_port_used? port Timeout.timeout(BIND_PORT_TIMEOUT) do begin TCPSocket.new(BIND_PORT_LOCALHOST, port).close true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH false rescue Errno::EADDRNOTAVAIL "Settings.local_port_used: Errno::EADDRNOTAVAIL" end end rescue Timeout::Error false end |
.text_or_empty(text) ⇒ Object
23 24 25 26 27 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 23 def self.text_or_empty text return "-" if text.nil? || text.empty? text end |
.tokenize(item) ⇒ Object
33 34 35 36 |
# File 'lib/rest-ftp-daemon/helpers.rb', line 33 def self.tokenize item return unless item.is_a? String "[#{item}]" end |