Module: Ubalo::Util

Included in:
ContainerProcess, Pod
Defined in:
lib/ubalo/util.rb

Defined Under Namespace

Classes: UbaloJSON

Class Method Summary collapse

Class Method Details

.append_config(filename, key, key_config) ⇒ Object


101
102
103
104
105
# File 'lib/ubalo/util.rb', line 101

def append_config(filename, key, key_config)
  config = read_config(filename)
  config[key] = key_config
  write_config(filename, config)
end

.columnsObject


117
118
119
120
121
# File 'lib/ubalo/util.rb', line 117

def columns
  @columns ||= Integer(`tput cols`.strip)
rescue
  80
end

.debug_mode?Boolean

Returns:

  • (Boolean)

113
114
115
# File 'lib/ubalo/util.rb', line 113

def debug_mode?
  ENV['UBALO_DEBUG'] == 'true'
end

.decode_content(encoded) ⇒ Object


83
84
85
86
87
88
89
90
91
# File 'lib/ubalo/util.rb', line 83

def decode_content encoded
  data = encoded.fetch('data')
  content_type = encoded.fetch('content_type')
  if content_type == UbaloJSON.content_type
    s = UbaloJSON.load(data)
  else
    raise Ubalo::Error, "cannot understand content of this type"
  end
end

.get_targz(url, archive_name) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ubalo/util.rb', line 34

def get_targz(url, archive_name)
  RestClient.get(url) do |response,*_|
    if response.code == 200
      File.open(archive_name, "wb") do |f|
        f.write response
      end
    else
      raise Ubalo::Error, "failed to download pod files: error #{response.code.inspect}"
    end
  end
end

.indent(str, amount = 2) ⇒ Object


73
74
75
76
77
78
79
80
81
# File 'lib/ubalo/util.rb', line 73

def indent str, amount=2
  if str
    str.chomp.each_line.map do |l|
      " "*amount + l
    end.join + "\n"
  else
    ""
  end
end

.login_suggestionObject


46
47
48
# File 'lib/ubalo/util.rb', line 46

def 
  "Please run ubalo login."
end

.normalize_pod_name(default_username, pod_name) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/ubalo/util.rb', line 22

def normalize_pod_name(default_username, pod_name)
  if pod_name.include?("/")
    username, pod_name = (pod_name or "").split '/', 2
  else
    username, pod_name = default_username, pod_name
  end
end

.pluralize(count, word) ⇒ Object


50
51
52
53
54
55
56
# File 'lib/ubalo/util.rb', line 50

def pluralize count, word
  if count == 1
    "#{count} #{word}"
  else
    "#{count} #{word}s"
  end
end

.put_targz(url, archive_name) ⇒ Object


30
31
32
# File 'lib/ubalo/util.rb', line 30

def put_targz(url, archive_name)
  RestClient.put(url, File.open(archive_name, 'rb'), :content_type => 'application/x-tar', :content_encoding => 'x-gzip')
end

.read_config(filename) ⇒ Object


93
94
95
96
97
98
99
# File 'lib/ubalo/util.rb', line 93

def read_config(filename)
  if File.exists?(filename)
    YAML.load_file(filename)
  else
    {}
  end
end

.time_ago_in_words(time) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ubalo/util.rb', line 58

def time_ago_in_words time
  return "-" unless time
  seconds = Time.now - Time.parse(time)
  case
  when seconds < 60
    "#{pluralize(seconds.floor, "second")} ago"
  when seconds/60 < 60
    "#{pluralize((seconds/60).floor, "minute")} ago"
  when seconds/60/60 < 24
    "#{pluralize((seconds/60/60).floor, "hour")} ago"
  else
    "#{pluralize((seconds/60/60/24).floor, "day")} ago"
  end
end

.write_config(filename, config) ⇒ Object


107
108
109
110
111
# File 'lib/ubalo/util.rb', line 107

def write_config(filename, config)
  File.open(filename, 'w') do |f|
    YAML.dump(config, f)
  end
end