Top Level Namespace
Defined Under Namespace
Modules: Morpheus, RestClient
Classes: Hash, NilClass
Constant Summary
collapse
- DEFAULT_DATE_FORMAT =
"%x"
- DEFAULT_TIME_FORMAT =
"%x %I:%M %p"
- ALTERNATE_TIME_FORMAT =
"yyyy-MM-dd'T'HH:mm:ss'Z'"
Instance Method Summary
collapse
-
#anded_list(items, limit = nil) ⇒ Object
-
#currency_sym(currency) ⇒ Object
-
#display_appliance(name, url) ⇒ Object
-
#filter_data(data, include_fields = nil, exclude_fields = nil) ⇒ Object
filter_data filters Hash-like data to only the specified fields To specify fields of child objects, use a “.” Usage: filter_data(instance, [“id”, “name”, “plan.name”]).
-
#format_bytes(bytes, units = "B", round = nil) ⇒ Object
-
#format_bytes_short(bytes) ⇒ Object
returns bytes in an abbreviated format eg.
-
#format_currency(amount, currency = 'USD', opts = {}) ⇒ Object
(also: #format_money)
returns currency amount formatted like “$4,5123.00”.
-
#format_date(dt, options = {}) ⇒ Object
-
#format_dt(dt, options = {}) ⇒ Object
-
#format_dt_as_param(dt) ⇒ Object
-
#format_duration(start_time, end_time = nil, format = "human") ⇒ Object
-
#format_duration_ago(start_time, end_time = nil) ⇒ Object
-
#format_duration_milliseconds(milliseconds, format = "human", ms_threshold = 1000) ⇒ Object
-
#format_duration_seconds(seconds, format = "human") ⇒ Object
-
#format_human_duration(seconds, show_relative = false) ⇒ Object
returns a human readable time duration.
-
#format_list(items, conjunction = "", limit = nil) ⇒ Object
def format_money(amount, currency=‘usd’, opts={}) format_currency(amount, currency, opts) end.
-
#format_local_date(dt, options = {}) ⇒ Object
-
#format_local_dt(dt, options = {}) ⇒ Object
-
#format_name_values(obj) ⇒ Object
-
#format_number(n, opts = {}) ⇒ Object
-
#format_sig_dig(n, sigdig = 3, min_sigdig = nil, pad_zeros = false) ⇒ Object
-
#get_object_value(data, key) ⇒ Object
get_object_value returns a value within a Hash like object Usage: get_object_value(host, “plan.name”).
-
#iso8601(dt) ⇒ Object
-
#no_colors(str) ⇒ Object
-
#ored_list(items, limit = nil) ⇒ Object
-
#parse_time(dt, format = nil) ⇒ Object
returns an instance of Time.
Instance Method Details
#anded_list(items, limit = nil) ⇒ Object
450
451
452
|
# File 'lib/morpheus/formatters.rb', line 450
def anded_list(items, limit=nil)
format_list(items, "and", limit)
end
|
#currency_sym(currency) ⇒ Object
391
392
393
|
# File 'lib/morpheus/formatters.rb', line 391
def currency_sym(currency)
Money::Currency.new((currency || 'USD').to_sym).symbol
end
|
#display_appliance(name, url) ⇒ Object
185
186
187
188
189
190
191
192
193
|
# File 'lib/morpheus/formatters.rb', line 185
def display_appliance(name, url)
if name.to_s == "" || name == 'remote-url'
"#{url}"
else
"[#{name}] #{url}"
end
end
|
#filter_data(data, include_fields = nil, exclude_fields = nil) ⇒ Object
filter_data filters Hash-like data to only the specified fields To specify fields of child objects, use a “.” Usage: filter_data(instance, [“id”, “name”, “plan.name”])
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/morpheus/formatters.rb', line 239
def filter_data(data, include_fields=nil, exclude_fields=nil)
if !data
return data
elsif data.is_a?(Array)
new_data = data.collect { |it| filter_data(it, include_fields, exclude_fields) }
return new_data
elsif data.is_a?(Hash)
if include_fields
my_data = {}
include_fields.each do |field|
if field.nil?
next
end
field = field.to_s
if field.empty?
next
end
field_key = field.strip
field_label = field_key
if field.index(/\s+as\s+/)
field_key, field_label = field.split(/\s+as\s+/)
if !field_label
field_label = field_key
end
end
if field.include?(".")
if field_label != field_key
my_data[field_label] = get_object_value(data, field_key)
else
namespaces = field.split(".")
cur_data = data
cur_filtered_data = my_data
namespaces.each_with_index do |ns, index|
if index != namespaces.length - 1
if cur_data && cur_data.respond_to?("key?")
cur_data = cur_data.key?(ns) ? cur_data[ns] : cur_data[ns.to_sym]
else
cur_data = nil
end
cur_filtered_data[ns] ||= {}
cur_filtered_data = cur_filtered_data[ns]
else
if cur_data && cur_data.respond_to?("key?")
cur_filtered_data[ns] = cur_data.key?(ns) ? cur_data[ns] : cur_data[ns.to_sym]
else
cur_filtered_data[ns] = nil
end
end
end
end
else
my_data[field_label] = data.key?(field_key) ? data[field_key] : data[field_key.to_sym]
end
end
return my_data
elsif exclude_fields
new_data = data.reject {|k, v| exclude_fields.include?(k.to_s) || exclude_fields.include?(k.to_sym) }
return new_data
end
else
return data end
end
|
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/morpheus/formatters.rb', line 313
def format_bytes(bytes, units="B", round=nil)
out = ""
if bytes
if bytes < 1024 && units == "B"
out = "#{bytes.to_i} B"
else
out = Filesize.from("#{bytes}#{units == 'auto' ? '' : " #{units}"}").pretty.strip
out = out.split(' ')[0].to_f.round(round).to_s + ' ' + out.split(' ')[1] if round
end
end
out
end
|
returns bytes in an abbreviated format eg. 3.1K instead of 3.10 KiB
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/morpheus/formatters.rb', line 328
def format_bytes_short(bytes)
out = format_bytes(bytes)
if out.include?(" ")
val, units = out.split(" ")
val = val.to_f
if val % 1 == 0
val = val.round(0).to_s
else
val = val.round(1).to_s
end
units = units[0].chr
out = "#{val}#{units}"
end
return out
end
|
returns currency amount formatted like “$4,5123.00”. 0.00 is formatted as “$0” this is not ideal
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
# File 'lib/morpheus/formatters.rb', line 397
def format_currency(amount, currency='USD', opts={})
if currency.to_s.empty?
currency = 'USD'
end
currency = currency.to_s.upcase
amount = amount.to_f
if amount == 0
return currency_sym(currency).to_s + "0"
else
sigdig = opts[:sigdig] ? opts[:sigdig].to_i : 2 min_sigdig = opts[:min_sigdig] ? opts[:min_sigdig].to_i : (sigdig || 2) display_value = format_sig_dig(amount, sigdig, min_sigdig, opts[:pad_zeros])
display_value = format_number(display_value) rtn = currency_sym(currency).to_s + display_value
if amount.to_i < 0
rtn = "(#{rtn})"
if opts[:minus_color]
rtn = "#{opts[:minus_color]}#{rtn}#{opts[:return_color] || cyan}"
end
end
rtn
end
end
|
67
68
69
|
# File 'lib/morpheus/formatters.rb', line 67
def format_date(dt, options={})
format_dt(dt, {format: DEFAULT_DATE_FORMAT}.merge(options))
end
|
53
54
55
56
57
58
59
60
61
|
# File 'lib/morpheus/formatters.rb', line 53
def format_dt(dt, options={})
dt = parse_time(dt)
return "" if dt.nil?
if options[:local]
dt = dt.getlocal
end
format = options[:format] || DEFAULT_TIME_FORMAT
return dt.strftime(format)
end
|
75
76
77
78
|
# File 'lib/morpheus/formatters.rb', line 75
def format_dt_as_param(dt)
dt = dt.getutc
format_dt(dt, {format: "%Y-%m-%d %X"})
end
|
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/morpheus/formatters.rb', line 80
def format_duration(start_time, end_time=nil, format="human")
if !start_time
return ""
end
start_time = parse_time(start_time)
if end_time
end_time = parse_time(end_time)
else
end_time = Time.now
end
seconds = (end_time - start_time).abs
format_duration_seconds(seconds, format)
end
|
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/morpheus/formatters.rb', line 94
def format_duration_ago(start_time, end_time=nil)
if !start_time
return ""
end
start_time = parse_time(start_time)
if end_time
end_time = parse_time(end_time)
else
end_time = Time.now
end
seconds = (end_time - start_time).abs
format_human_duration(seconds, true)
end
|
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/morpheus/formatters.rb', line 124
def format_duration_milliseconds(milliseconds, format="human", ms_threshold=1000)
out = ""
milliseconds = milliseconds.abs.to_i
if ms_threshold && ms_threshold > milliseconds
out = "#{milliseconds}ms"
else
out = format_duration_seconds((milliseconds.to_f / 1000).floor, format)
end
out
end
|
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/morpheus/formatters.rb', line 108
def format_duration_seconds(seconds, format="human")
seconds = seconds.abs
out = ""
if format == "human"
out = format_human_duration(seconds)
elsif format
interval_time = Time.mktime(0) + seconds
out = interval_time.strftime(format)
else
interval_time = Time.mktime(0) + seconds
out = interval_time.strftime("%H:%M:%S")
end
out
end
|
returns a human readable time duration
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/morpheus/formatters.rb', line 137
def format_human_duration(seconds, show_relative=false)
out = ""
days, hours, minutes = (seconds / (60*60*24)).floor, (seconds / (60*60)).floor, (seconds / (60)).floor
if days > 365
out << "#{days.floor} days"
elsif days > 61
out << "#{days.floor} days"
elsif days > 31
out << "#{days.floor} days"
elsif days > 0
if days.floor == 1
out << "1 day"
else
out << "#{days.floor} days"
end
elsif hours > 1
if hours == 1
out << "1 hour"
else
out << "#{hours.floor} hours"
end
elsif minutes > 1
if minutes == 1
out << "1 minute"
else
out << "#{minutes.floor} minutes"
end
elsif seconds > 0 && seconds < 1
ms = (seconds.to_f * 1000).to_i
out << "#{ms}ms"
else
if seconds.floor == 1
out << "1 second"
else
out << "#{seconds.floor} seconds"
end
end
if show_relative
if seconds < 1
out = "just now"
else
out << " ago"
end
end
out
end
|
def format_money(amount, currency=‘usd’, opts={})
format_currency(amount, currency, opts)
end
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/morpheus/formatters.rb', line 432
def format_list(items, conjunction="", limit=nil)
items = items ? items.clone : []
num_items = items.size
if limit
items = items.first(limit)
end
last_item = items.pop
if items.empty?
return "#{last_item}"
else
if limit && limit < num_items
items << last_item
last_item = "(#{num_items - items.size} more)"
end
return items.join(", ") + (conjunction.to_s.empty? ? ", " : " #{conjunction} ") + "#{last_item}"
end
end
|
71
72
73
|
# File 'lib/morpheus/formatters.rb', line 71
def format_local_date(dt, options={})
format_dt(dt, {local: true, format: DEFAULT_DATE_FORMAT}.merge(options))
end
|
63
64
65
|
# File 'lib/morpheus/formatters.rb', line 63
def format_local_dt(dt, options={})
format_dt(dt, {local: true}.merge(options))
end
|
458
459
460
461
462
463
464
|
# File 'lib/morpheus/formatters.rb', line 458
def format_name_values(obj)
if obj.is_a?(Hash)
obj.collect {|k,v| "#{k}: #{v}"}.join(", ")
else
""
end
end
|
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
# File 'lib/morpheus/formatters.rb', line 351
def format_number(n, opts={})
delim = opts[:delimiter] || ','
out = ""
parts = n.to_s.split(".")
whole_number = parts[0].to_s
decimal = parts[1] ? parts[1..-1].join('.') : nil
i = 0
whole_number.reverse.each_char do |c|
out = (i > 0 && i % 3 == 0) ? "#{c}#{delim}#{out}" : "#{c}#{out}"
i+= 1
end
if decimal
out << "." + decimal
end
return out
end
|
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# File 'lib/morpheus/formatters.rb', line 368
def format_sig_dig(n, sigdig=3, min_sigdig=nil, pad_zeros=false)
v = ""
if sigdig && sigdig > 0
v = sprintf("%.#{sigdig}f", n)
else
v = n.to_i.round().to_s
end
if min_sigdig
v_parts = v.split(".")
decimal_str = v_parts[1]
if decimal_str == nil
v = v + "." + ('0' * min_sigdig)
elsif decimal_str.size < min_sigdig
v = v + ('0' * (min_sigdig - decimal_str.size))
end
end
v
end
|
#get_object_value(data, key) ⇒ Object
get_object_value returns a value within a Hash like object Usage: get_object_value(host, “plan.name”)
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/morpheus/formatters.rb', line 201
def get_object_value(data, key)
value = nil
if key.is_a?(Proc)
return key.call(data)
end
key = key.to_s
if key.include?(".")
namespaces = key.split(".")
value = data
namespaces.each do |ns|
if value.respond_to?("key?")
if value.key?(ns.to_s)
value = value[ns]
elsif value.key?(ns.to_sym)
value = value[ns.to_sym]
else
value = nil
end
else
value = nil
end
end
else
if data.respond_to?("key?")
if data.key?(key.to_s)
value = data[key.to_s]
elsif data.key?(key.to_sym)
value = data[key.to_sym]
end
end
end
return value
end
|
#iso8601(dt) ⇒ Object
195
196
197
|
# File 'lib/morpheus/formatters.rb', line 195
def iso8601(dt)
dt.instance_of(Time) ? dt.iso8601 : "#{dt}"
end
|
#no_colors(str) ⇒ Object
346
347
348
|
# File 'lib/morpheus/formatters.rb', line 346
def no_colors(str)
str.to_s.gsub /\e\[\d+m/, ""
end
|
#ored_list(items, limit = nil) ⇒ Object
454
455
456
|
# File 'lib/morpheus/formatters.rb', line 454
def ored_list(items, limit=nil)
format_list(items, "or", limit)
end
|
#parse_time(dt, format = nil) ⇒ Object
returns an instance of Time
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/morpheus/formatters.rb', line 10
def parse_time(dt, format=nil)
if dt.nil? || dt == '' || dt.to_i == 0
return nil
elsif dt.is_a?(Time)
return dt
elsif dt.is_a?(String)
result = nil
err = nil
begin
result = Time.parse(dt)
rescue => e
err = e
end
if !result
format ||= DEFAULT_TIME_FORMAT
if format
begin
result = Time.strptime(dt, format)
rescue => e
err = e
end
end
if !result
begin
result = Time.strptime(dt, ALTERNATE_TIME_FORMAT)
rescue => e
err = e
end
end
end
if result
return result
else
raise "unable to parse time '#{dt}'. #{err}"
end
elsif dt.is_a?(Numeric)
return Time.at(dt)
else
raise "bad argument type for parse_time() #{dt.class} #{dt.inspect}"
end
end
|