Class: Utilities

Inherits:
Object
  • Object
show all
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"

Class Method Summary collapse

Class Method Details

Check if cookie file exist

Returns:

  • (Boolean)

    True if cookie file exists, false otherwise.



70
71
72
# File 'lib/utilities.rb', line 70

def self.cookie_exists?
  File.file?(cookie_file_path)
end

.log(message, options = {}) ⇒ Object

Logs a message to console.

Parameters:

  • message (String)

    The message to log.

  • options (Hash) (defaults to: {})

    Log type: `:info`, `:success`, `:error` or `:none`.



40
41
42
43
44
45
# File 'lib/utilities.rb', line 40

def self.log(message, options = {})
  type = options[:type] || :info
  newline = options[:newline] || true

  print "#{LOG_SYMBOLS[type] ? " #{LOG_SYMBOLS[type]} " : ''}#{message}" + (newline ? "\n" : "")
end

.number?(str) ⇒ Boolean

Validates a String to see if it's an Integer

Returns:

  • (Boolean)

    True if String isan Integer, false otherwise.



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.

Parameters:

  • count (Int)

    The count of context.

  • singular (String)

    The word in singular form.

  • plural (String)

    The word in plural form.

Returns:

  • (Int)

    Duration in hours.



145
146
147
148
# File 'lib/utilities.rb', line 145

def self.pluralize(count, singular, plural)
  count.zero? && return
  count == 1 ? singular : plural
end

Removes the stored cookie if exists.



56
57
58
# File 'lib/utilities.rb', line 56

def self.remove_cookie
  File.delete(cookie_file_path) if File.file?(cookie_file_path)
end

Retrieves session cookie if available.

Returns:

  • (String)

    Cookie value or nil



63
64
65
# File 'lib/utilities.rb', line 63

def self.retrieve_cookie
  File.read(cookie_file_path) if cookie_exists?
end

.rspec_running?Bool

Checks if rspecs are running.

Returns:

  • (Bool)

    True if rspecs are running.



135
136
137
# File 'lib/utilities.rb', line 135

def self.rspec_running?
  $PROGRAM_NAME.split("/").last == "rspec"
end

Stores session cookie in tmp folder.

Parameters:

  • cookie (String)

    The cookie string value.



50
51
52
53
# File 'lib/utilities.rb', line 50

def self.store_cookie(cookie)
  FileUtils.mkdir_p(temp_folder_path) unless File.directory?(temp_folder_path)
  File.write(cookie_file_path, cookie)
end

.time_diff_hours(start_time, end_time) ⇒ Int

Return the total hour duration between two time strings.

Returns:

  • (Int)

    Duration in hours.



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

Returns:

  • (Boolean)

    True if String isan Integer, false otherwise.



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

Parameters:

  • json (String)

    A JSON String.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)

    True if time is valid, false otherwise.



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

Returns:

  • (Boolean)

    True if URL is valid, false otherwise.



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