Module: Flydata::Helpers

Included in:
ApiClient, Cli, Command::Sync, Command::Version, Credentials
Defined in:
lib/flydata/helpers.rb

Constant Summary collapse

UNIT_PREFIX =
%W(TB GB MB KB B).freeze
@@development_mode =
nil

Class Method Summary collapse

Class Method Details

.as_size(s) ⇒ Object


90
91
92
93
94
95
96
97
98
# File 'lib/flydata/helpers.rb', line 90

def as_size( s )
  s = s.to_f
  i = UNIT_PREFIX.length - 1
  while s > 512 && i > 0
    s /= 1024
    i -= 1
  end
  ((s > 9 || s.modulo(1) < 0.1 ? '%d' : '%.1f') % s) + ' ' + UNIT_PREFIX[i]
end

.development?Boolean

Returns:

  • (Boolean)

35
36
37
38
# File 'lib/flydata/helpers.rb', line 35

def development?
  return @@development_mode unless @@development_mode.nil?
  @@development_mode = !!(File.open(flydata_conf_file).first.strip.end_with?('dev'))
end

.env_modeObject


40
41
42
# File 'lib/flydata/helpers.rb', line 40

def env_mode
  development? ? 'dev' : ''
end

.env_suffixObject


44
45
46
# File 'lib/flydata/helpers.rb', line 44

def env_suffix
  development? ? '_dev' : ''
end

.flydata_api_host_fileObject


64
65
66
# File 'lib/flydata/helpers.rb', line 64

def flydata_api_host_file
  File.join(FLYDATA_HOME, 'flydata_api_host')
end

.flydata_conf_fileObject


68
69
70
# File 'lib/flydata/helpers.rb', line 68

def flydata_conf_file
  File.join(FLYDATA_HOME, 'flydata.conf')
end

.flydata_versionObject


27
28
29
# File 'lib/flydata/helpers.rb', line 27

def flydata_version
  IO.read(VERSION_PATH).strip
end

.format_menu_list(menu_list) ⇒ Object

format text


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/flydata/helpers.rb', line 49

def format_menu_list(menu_list)
  max_length_list = menu_list.inject(Array.new(menu_list.first.size, 0)) do |ml, menu|
    0.upto(menu.size - 1) do |i|
      ml[i] = menu[i].length if ml[i] < menu[i].length
    end
    ml
  end

  menu_list = menu_list.collect do |menu|
    0.upto(menu.size - 1).inject("") do |str, i|
      str = "#{str}#{menu[i].ljust(max_length_list[i] + 1, ' ')}"
    end
  end
end

.retry_on(exception = StandardError, try_count = 3, interval = 1.0) ⇒ Object

Retry the given block if exception happens


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flydata/helpers.rb', line 73

def retry_on(exception = StandardError, try_count = 3, interval = 1.0)
  count = 0
  begin
    count += 1
    yield
  rescue exception
    if count < try_count
      sleep interval
      interval *= 2
      retry
    else
      raise
    end
  end
end

.to_command_class(class_name) ⇒ Object


31
32
33
# File 'lib/flydata/helpers.rb', line 31

def to_command_class(class_name)
  eval("Flydata::Command::#{class_name.camelcase}")
end

.usage_textObject


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flydata/helpers.rb', line 5

def usage_text
  text = ""

  text += '-' * 64
  text += "\n"
  text += <<-EOM
Usage: flydata COMMAND
  start                    # start flydata process
  stop                     # stop flydata process
  restart                  # restart flydata process
  status                   # check flydata process
  version                  # show flydata version
  sync:generate_table_ddl  # Generate CREATE TABLE script for Redshift
  sync:reset [tables]      # reset sync and stop flydata process
  sync:flush               # flush your current buffer and stop flydata process
  EOM
  text += "\n"
  text += '-' * 64
  text += "\n"
  text
end