Class: Utilities
- Inherits:
-
Object
- Object
- Utilities
- Defined in:
- lib/utilities.rb
Overview
Utilities
Constant Summary collapse
- LOG_SYMBOLS =
{ info: "→", success: "🎉", error: "😓", none: "" }.freeze
- ENCRYPTED_STORE_KEY =
"JIRA-LOGWORK"
- TEMP_FOLDER_NAME =
"tmp"
- COOKIE_FILE_NAME =
".cookie"
Class Method Summary collapse
-
.cookie_exists? ⇒ Boolean
Check if cookie file exist.
-
.log(message, options = {}) ⇒ Object
Logs a message to console.
-
.number?(str) ⇒ Boolean
Validates a String to see if it's an Integer.
-
.pluralize(count, singular, plural) ⇒ Int
Return the singluar or plural version of the string.
-
.remove_cookie ⇒ Object
Removes the stored cookie if exists.
-
.retrieve_cookie ⇒ String
Retrieves session cookie if available.
-
.rspec_running? ⇒ Bool
Checks if rspecs are running.
-
.store_cookie(cookie) ⇒ Object
Stores session cookie in tmp folder.
-
.time_diff_hours(start_time, end_time) ⇒ Int
Return the total hour duration between two time strings.
-
.valid_date?(str) ⇒ Boolean
Validates a Date string with a specific format.
-
.valid_json?(json) ⇒ Boolean
Validates a JSON String.
-
.valid_time?(time) ⇒ Boolean
Validates a time string, e.g.
-
.valid_url?(url) ⇒ Boolean
Validates a URL string.
Class Method Details
.cookie_exists? ⇒ Boolean
Check if cookie file exist
70 71 72 |
# File 'lib/utilities.rb', line 70 def self. File.file?() end |
.log(message, options = {}) ⇒ Object
Logs a message to console.
40 41 42 43 44 45 |
# File 'lib/utilities.rb', line 40 def self.log(, = {}) type = [:type] || :info newline = [:newline] || true print "#{LOG_SYMBOLS[type] ? " #{LOG_SYMBOLS[type]} " : ''}#{}" + (newline ? "\n" : "") end |
.number?(str) ⇒ Boolean
Validates a String to see if it's an Integer
110 111 112 113 114 |
# File 'lib/utilities.rb', line 110 def self.number?(str) !(!Integer(str)) rescue ArgumentError, TypeError false end |
.pluralize(count, singular, plural) ⇒ Int
Return the singluar or plural version of the string.
145 146 147 148 |
# File 'lib/utilities.rb', line 145 def self.pluralize(count, singular, plural) count.zero? && return count == 1 ? singular : plural end |
.remove_cookie ⇒ Object
Removes the stored cookie if exists.
56 57 58 |
# File 'lib/utilities.rb', line 56 def self. File.delete() if File.file?() end |
.retrieve_cookie ⇒ String
Retrieves session cookie if available.
63 64 65 |
# File 'lib/utilities.rb', line 63 def self. File.read() if end |
.rspec_running? ⇒ Bool
Checks if rspecs are running.
135 136 137 |
# File 'lib/utilities.rb', line 135 def self.rspec_running? $PROGRAM_NAME.split("/").last == "rspec" end |
.store_cookie(cookie) ⇒ Object
Stores session cookie in tmp folder.
50 51 52 53 |
# File 'lib/utilities.rb', line 50 def self.() FileUtils.mkdir_p(temp_folder_path) unless File.directory?(temp_folder_path) File.write(, ) end |
.time_diff_hours(start_time, end_time) ⇒ Int
Return the total hour duration between two time strings.
128 129 130 |
# File 'lib/utilities.rb', line 128 def self.time_diff_hours(start_time, end_time) (Time.parse(end_time) - Time.parse(start_time)).to_i / 3600 end |
.valid_date?(str) ⇒ Boolean
Validates a Date string with a specific format
119 120 121 122 123 |
# File 'lib/utilities.rb', line 119 def self.valid_date?(str) Date.strptime(str, "%m/%d/%Y") rescue ArgumentError, TypeError false end |
.valid_json?(json) ⇒ Boolean
Validates a JSON String
88 89 90 91 92 93 |
# File 'lib/utilities.rb', line 88 def self.valid_json?(json) JSON.parse(json) true rescue JSON::ParserError false end |
.valid_time?(time) ⇒ Boolean
Validates a time string, e.g. 10:00
98 99 100 101 102 103 104 105 |
# File 'lib/utilities.rb', line 98 def self.valid_time?(time) return false unless time =~ /[0-2][0-9]:[0-6][0-6]/ Time.parse(time) true rescue StandardError false end |
.valid_url?(url) ⇒ Boolean
Validates a URL string
78 79 80 81 82 83 |
# File 'lib/utilities.rb', line 78 def self.valid_url?(url) uri = URI.parse(url) !uri.nil? && !uri.host.nil? rescue URI::InvalidURIError false end |