module RHC::Helpers

Public Instance Methods

color(s, color) click to toggle source
# File lib/rhc/helpers.rb, line 111
def color(s, color)
  $terminal.color(s, color)
end
config() click to toggle source
# File lib/rhc/helpers.rb, line 79
def config
  raise "Operations requiring configuration must define a config accessor"
end
date(s) click to toggle source
# File lib/rhc/helpers.rb, line 40
def date(s)
  now = Time.now
  d = datetime_rfc3339(s)
  if now.year == d.year
    return d.strftime('%l:%M %p').strip if now.yday == d.yday
  end
  d.strftime('%b %d %l:%M %p')
rescue ArgumentError
  "Unknown date"
end
datetime_rfc3339(s) click to toggle source
# File lib/rhc/helpers.rb, line 51
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 36
def decode_json(s)
  RHC::Vendor::OkJson.decode(s)
end
disable_deprecated?() click to toggle source
# File lib/rhc/helpers.rb, line 24
def disable_deprecated?
  # 1) default for now is false
  # 2) when releasing a 1.0 beta flip this to true
  # 3) all deprecated aliases should be removed right before 1.0
  disable = false

  env_disable = ENV['DISABLE_DEPRECATED']
  disable = true if env_disable == '1'

  disable
end
error(msg, *args) click to toggle source
# File lib/rhc/helpers.rb, line 107
def error(msg, *args)
  say color(msg, :red)
end
get(uri, opts=nil, *args) click to toggle source
# File lib/rhc/helpers.rb, line 64
def get(uri, opts=nil, *args)
  opts = {'User-Agent' => user_agent}.merge(opts || {})
  RestClient.get(uri, opts, *args)
end
header(s) click to toggle source
# File lib/rhc/helpers.rb, line 135
def header(s)
  say s
  say "=" * s.length
end
jruby?() click to toggle source

Platform helpers

# File lib/rhc/helpers.rb, line 224
def jruby? ; RUBY_PLATFORM =~ %rjava/ end
openshift_rest_node() click to toggle source
# File lib/rhc/helpers.rb, line 89
def openshift_rest_node
  "#{openshift_url}/broker/rest/api"
end
openshift_server() click to toggle source
# File lib/rhc/helpers.rb, line 83
def openshift_server
  config.get_value('libra_server')
end
openshift_url() click to toggle source
# File lib/rhc/helpers.rb, line 86
def openshift_url
  "https://#{openshift_server}"
end
paragraph(&block) click to toggle source

paragraph

highline helper which creates a section with margins of 1, 1

# File lib/rhc/helpers.rb, line 206
def paragraph(&block)
  section(:top => 1, :bottom => 1, &block)
end
pluralize(count, s) click to toggle source
# File lib/rhc/helpers.rb, line 115
def pluralize(count, s)
  count == 1 ? "#{count} #{s}" : "#{count} #{s}s"
end
results() { || ... } click to toggle source

results

highline helper which creates a paragraph with a header to distinguish the final results of a command from other output

# File lib/rhc/helpers.rb, line 216
def results(&block)
  paragraph do
    say "RESULT:"
    yield
  end
end
say(msg) click to toggle source

Output helpers

# File lib/rhc/helpers.rb, line 97
def say(msg)
  super
  msg
end
section(params={}, &block) click to toggle source
# File lib/rhc/helpers.rb, line 169
def section(params={}, &block)
  top = params[:top]
  top = 0 if top.nil?
  bottom = params[:bottom]
  bottom = 0 if bottom.nil?

  # add more newlines if top is greater than the last section's bottom margin
  top_margin = @@section_bottom_last

  # negitive previous bottoms indicate that an untracked newline was
  # printed and so we do our best to negate it since we can't remove it
  if top_margin < 0
    top += top_margin
    top_margin = 0
  end

  until top_margin >= top
    say "\n"
    top_margin += 1
  end

  block.call

  bottom_margin = 0
  until bottom_margin >= bottom
    say "\n"
    bottom_margin += 1
  end

  @@section_bottom_last = bottom
end
success(msg, *args) click to toggle source
# File lib/rhc/helpers.rb, line 101
def success(msg, *args)
  say color(msg, :green)
end
table(items, opts={}, &block) click to toggle source
# File lib/rhc/helpers.rb, line 119
def table(items, opts={}, &block)
  items = items.map &block if block_given?
  columns = []
  max = items.each do |item|
    item.each_with_index do |s, i|
      item[i] = s.to_s
      columns[i] = [columns[i] || 0, s.length].max if s.respond_to?(:length)
    end
  end
  align = opts[:align] || []
  join = opts[:join] || ' '
  items.map do |item|
    item.each_with_index.map{ |s,i| s.send((align[i] == :right ? :rjust : :ljust), columns[i], ' ') }.join(join).strip
  end
end
unix?() click to toggle source
# File lib/rhc/helpers.rb, line 226
def unix? ; !jruby? && !windows? end
user_agent() click to toggle source

Web related requests

# File lib/rhc/helpers.rb, line 60
def user_agent
  "rhc/#{RHC::VERSION::STRING} (ruby #{RUBY_VERSION}; #{RUBY_PLATFORM})#{" (API #{RHC::Rest::API_VERSION})" rescue ''}"
end
warn(msg, *args) click to toggle source
# File lib/rhc/helpers.rb, line 104
def warn(msg, *args)
  say color(msg, :yellow)
end
windows?() click to toggle source
# File lib/rhc/helpers.rb, line 225
def windows? ; RUBY_PLATFORM =~ %rwin(32|dows|ce)|djgpp|(ms|cyg|bcc)win|mingw32/ end