Module: Conify::Helpers
- Extended by:
- Helpers
- Included in:
- Api::AbstractApi, CLI, Command, Command::AbstractCommand, Helpers, Manifest, Test
- Defined in:
- lib/conify/helpers.rb
Instance Method Summary collapse
- #allow_user_response ⇒ Object
- #ask_for_conflux_creds ⇒ Object
- #ask_for_password ⇒ Object
- #ask_for_password_on_windows ⇒ Object
-
#camelize(str) ⇒ Object
convert string from underscores to camelcase.
- #display(msg = '') ⇒ Object
-
#echo_off ⇒ Object
Hide user input.
-
#echo_on ⇒ Object
Show user input.
- #error(msg = '') ⇒ Object
- #exclusive_deep_merge(merge_to, merge_from) ⇒ Object
-
#format_with_bang(message) ⇒ Object
Add a bang to an error message.
-
#host ⇒ Object
Strip the protocol + following slashes off of a url.
- #host_url ⇒ Object
- #kensa_manifest_name ⇒ Object
- #kensa_manifest_path ⇒ Object
- #manifest_content ⇒ Object
- #manifest_filename ⇒ Object
- #manifest_path ⇒ Object
-
#manually_added_methods(klass) ⇒ Object
Get an array (of symbols) of the user-defined methods for a klass.
- #open_url(url) ⇒ Object
- #running_on_a_mac? ⇒ Boolean
- #running_on_windows? ⇒ Boolean
- #site_url ⇒ Object
-
#to_table(data, headers) ⇒ Object
format some data into a table to then be displayed to the user.
- #with_tty(&block) ⇒ Object
Instance Method Details
#allow_user_response ⇒ Object
73 74 75 |
# File 'lib/conify/helpers.rb', line 73 def allow_user_response $stdin.gets.to_s.strip end |
#ask_for_conflux_creds ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/conify/helpers.rb', line 139 def ask_for_conflux_creds # Ask for Conflux Credentials puts 'Enter your Conflux credentials.' # Email: print 'Email: ' email = allow_user_response # Password print 'Password (typing will be hidden): ' password = running_on_windows? ? ask_for_password_on_windows : ask_for_password { email: email, password: password } end |
#ask_for_password ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/conify/helpers.rb', line 104 def ask_for_password begin echo_off # make the password input hidden password = allow_user_response puts ensure echo_on # flip input visibility back on end password end |
#ask_for_password_on_windows ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/conify/helpers.rb', line 85 def ask_for_password_on_windows require 'Win32API' char = nil password = '' while char = Win32API.new('msvcrt', '_getch', [ ], 'L').Call do break if char == 10 || char == 13 # received carriage return or newline if char == 127 || char == 8 # backspace and delete password.slice!(-1, 1) else # windows might throw a -1 at us so make sure to handle RangeError (password << char.chr) rescue RangeError end end puts password end |
#camelize(str) ⇒ Object
convert string from underscores to camelcase
23 24 25 |
# File 'lib/conify/helpers.rb', line 23 def camelize(str) str.split('_').collect(&:capitalize).join end |
#display(msg = '') ⇒ Object
17 18 19 20 |
# File 'lib/conify/helpers.rb', line 17 def display(msg = '') puts(msg) $stdout.flush end |
#echo_off ⇒ Object
Hide user input
117 118 119 120 121 |
# File 'lib/conify/helpers.rb', line 117 def echo_off with_tty do system 'stty -echo' end end |
#echo_on ⇒ Object
Show user input
124 125 126 127 128 |
# File 'lib/conify/helpers.rb', line 124 def echo_on with_tty do system 'stty echo' end end |
#error(msg = '') ⇒ Object
5 6 7 8 |
# File 'lib/conify/helpers.rb', line 5 def error(msg = '') $stderr.puts(format_with_bang(msg)) exit(1) end |
#exclusive_deep_merge(merge_to, merge_from) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/conify/helpers.rb', line 193 def exclusive_deep_merge(merge_to, merge_from) merged = merge_to.clone merge_from.each do |key, value| # Only override existing key if merged.keys.include?(key) # Deep merge for nested hash if value.is_a?(Hash) && merged[key].is_a?(Hash) merged[key] = exclusive_deep_merge(merged[key], value) else merged[key] = value end end end merged end |
#format_with_bang(message) ⇒ Object
Add a bang to an error message
11 12 13 14 15 |
# File 'lib/conify/helpers.rb', line 11 def format_with_bang() return if !.is_a?(String) return '' if .to_s.strip == '' " ! " + .encode('utf-8', 'binary', invalid: :replace, undef: :replace).split("\n").join("\n ! ") end |
#host ⇒ Object
Strip the protocol + following slashes off of a url
156 157 158 |
# File 'lib/conify/helpers.rb', line 156 def host host_url.gsub(/http:\/\/|https:\/\//, '') end |
#host_url ⇒ Object
160 161 162 |
# File 'lib/conify/helpers.rb', line 160 def host_url ENV['CONFLUX_HOST'] || 'https://api.goconflux.com' end |
#kensa_manifest_name ⇒ Object
189 190 191 |
# File 'lib/conify/helpers.rb', line 189 def kensa_manifest_name 'addon-manifest.json' end |
#kensa_manifest_path ⇒ Object
185 186 187 |
# File 'lib/conify/helpers.rb', line 185 def kensa_manifest_path File.join(Dir.pwd, kensa_manifest_name) end |
#manifest_content ⇒ Object
181 182 183 |
# File 'lib/conify/helpers.rb', line 181 def manifest_content JSON.parse(File.read(manifest_path)) rescue {} end |
#manifest_filename ⇒ Object
177 178 179 |
# File 'lib/conify/helpers.rb', line 177 def manifest_filename 'conflux-manifest.json' end |
#manifest_path ⇒ Object
173 174 175 |
# File 'lib/conify/helpers.rb', line 173 def manifest_path File.join(Dir.pwd, manifest_filename) end |
#manually_added_methods(klass) ⇒ Object
Get an array (of symbols) of the user-defined methods for a klass
169 170 171 |
# File 'lib/conify/helpers.rb', line 169 def manually_added_methods(klass) klass.instance_methods(false) end |
#open_url(url) ⇒ Object
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/conify/helpers.rb', line 211 def open_url(url) if running_on_a_mac? system "open #{url}" elsif running_on_windows? system "explorer #{url}" else # Probably some flavor of Linux system "xdg-open #{url}" end end |
#running_on_a_mac? ⇒ Boolean
81 82 83 |
# File 'lib/conify/helpers.rb', line 81 def running_on_a_mac? RUBY_PLATFORM =~ /-darwin\d/ end |
#running_on_windows? ⇒ Boolean
77 78 79 |
# File 'lib/conify/helpers.rb', line 77 def running_on_windows? RUBY_PLATFORM =~ /mswin32|mingw32/ end |
#site_url ⇒ Object
164 165 166 |
# File 'lib/conify/helpers.rb', line 164 def site_url ENV['CONFLUX_SITE_URL'] || 'https://goconflux.com' end |
#to_table(data, headers) ⇒ Object
format some data into a table to then be displayed to the user
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/conify/helpers.rb', line 28 def to_table(data, headers) column_lengths = [] gutter = 2 table = '' # Figure out column widths based on longest string in each column (including the header string) headers.each { |header| width = data.map { |_| _[header] }.max_by(&:length).length width = header.length if width < header.length column_lengths << width } # format the length of a table cell string to make it as wide as the column (by adding extra spaces) format_row_entry = lambda { |entry, i| entry + (' ' * (column_lengths[i] - entry.length + gutter)) } # Add headers headers.each_with_index { |header, i| table += format_row_entry.call(header, i) } table += "\n" # Add line breaks under headers column_lengths.each { |length| table += (('-' * length) + (' ' * gutter)) } table += "\n" # Add rows data.each { |row| headers.each_with_index { |header, i| table += format_row_entry.call(row[header], i) } table += "\n" } table end |
#with_tty(&block) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/conify/helpers.rb', line 130 def with_tty(&block) return unless $stdin.isatty begin yield rescue # fails on windows end end |