module RHC::Helpers

Constants

PREFIX
ROLES
Role
SSLVersion

Private Class Methods

global_option(*args, &block) click to toggle source
# File lib/rhc/helpers.rb, line 15
def self.global_option(*args, &block)
  RHC::Commands.global_option *args, &block
end

Public Instance Methods

date(s) click to toggle source
# File lib/rhc/helpers.rb, line 51
def date(s)
  return nil unless s.present?
  now = Date.today
  d = datetime_rfc3339(s).to_time
  if now.year == d.year
    return d.strftime('%l:%M %p').strip if now.yday == d.yday
    d.strftime('%b %d %l:%M %p')
  else
    d.strftime('%b %d, %Y %l:%M %p')
  end
rescue ArgumentError
  "Unknown date"
end
datetime_rfc3339(s) click to toggle source
# File lib/rhc/helpers.rb, line 88
def datetime_rfc3339(s)
  DateTime.strptime(s, '%Y-%m-%dT%H:%M:%S%z')
  # Replace with d = DateTime.rfc3339(s)
end
decode_json(s) click to toggle source
# File lib/rhc/helpers.rb, line 29
def decode_json(s)
  RHC::Vendor::OkJson.decode(s)
end
distance_of_time_in_words(from_time, to_time = 0) click to toggle source
# File lib/rhc/helpers.rb, line 65
def distance_of_time_in_words(from_time, to_time = 0)
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  distance_in_minutes = (((to_time - from_time).abs)/60).round
  distance_in_seconds = ((to_time - from_time).abs).round

  case distance_in_minutes
    when 0..1
      return distance_in_minutes == 0 ?
             "less than 1 minute" :
             "#{distance_in_minutes} minute"

    when 2..44           then "#{distance_in_minutes} minutes"
    when 45..89          then "about 1 hour"
    when 90..1439        then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
    when 1440..2519      then "about 1 day"
    when 2520..43199     then "#{(distance_in_minutes.to_f / 1440.0).round} days"
    when 43200..86399    then "about 1 month"
    else
      "about #{(distance_in_minutes.to_f / 43200.0).round} months"
  end
end
human_size( s ) click to toggle source
# File lib/rhc/helpers.rb, line 40
def human_size( s )
  return "unknown" unless s
  s = s.to_f
  i = PREFIX.length - 1
  while s > 500 && i > 0
    i -= 1
    s /= 1000
  end
  ((s > 9 || s.modulo(1) < 0.1 ? '%d' : '%.1f') % s) + ' ' + PREFIX[i]
end
system_path(path) click to toggle source
# File lib/rhc/helpers.rb, line 33
def system_path(path)
  return path.gsub(File::SEPARATOR, File::ALT_SEPARATOR) if File.const_defined?('ALT_SEPARATOR') and File::ALT_SEPARATOR.present?
  path
end
user_agent() click to toggle source

Web related requests

# File lib/rhc/helpers.rb, line 97
def user_agent
  "rhc/#{RHC::VERSION::STRING} (ruby #{RUBY_VERSION}; #{RUBY_PLATFORM})#{" (API #{RHC::Rest::API_VERSION})" rescue ''}"
end