Module: Conflux::Helpers
Instance Method Summary collapse
- #add_headers(request, headers) ⇒ Object
- #allow_user_response ⇒ Object
- #ask_for_basic_creds ⇒ Object
- #ask_for_password ⇒ Object
- #ask_for_password_on_windows ⇒ Object
- #conflux_folder_path ⇒ Object
- #conflux_manifest_path ⇒ Object
-
#echo_off ⇒ Object
Hide user input.
-
#echo_on ⇒ Object
Show user input.
- #error(msg = '') ⇒ Object
- #form_request(net_obj, route, data, headers, error_message) ⇒ Object
- #handle_json_response(response, error_message) ⇒ Object
- #host_url ⇒ Object
- #http ⇒ Object
- #json_request(net_obj, route, data, headers, error_message) ⇒ Object
- #prompt_user_to_select_app(apps_map) ⇒ Object
- #running_on_windows? ⇒ Boolean
- #with_tty(&block) ⇒ Object
Instance Method Details
#add_headers(request, headers) ⇒ Object
165 166 167 |
# File 'lib/conflux/helpers.rb', line 165 def add_headers(request, headers) headers.each { |key, val| request.add_field(key, val) } end |
#allow_user_response ⇒ Object
80 81 82 |
# File 'lib/conflux/helpers.rb', line 80 def allow_user_response $stdin.gets.to_s.strip end |
#ask_for_basic_creds ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/conflux/helpers.rb', line 10 def ask_for_basic_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
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/conflux/helpers.rb', line 45 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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/conflux/helpers.rb', line 26 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 |
#conflux_folder_path ⇒ Object
177 178 179 |
# File 'lib/conflux/helpers.rb', line 177 def conflux_folder_path "#{Dir.pwd}/.conflux/" end |
#conflux_manifest_path ⇒ Object
181 182 183 |
# File 'lib/conflux/helpers.rb', line 181 def conflux_manifest_path File.join(conflux_folder_path, 'manifest.json') end |
#echo_off ⇒ Object
Hide user input
58 59 60 61 62 |
# File 'lib/conflux/helpers.rb', line 58 def echo_off with_tty do system 'stty -echo' end end |
#echo_on ⇒ Object
Show user input
65 66 67 68 69 |
# File 'lib/conflux/helpers.rb', line 65 def echo_on with_tty do system 'stty echo' end end |
#error(msg = '') ⇒ Object
129 130 131 132 |
# File 'lib/conflux/helpers.rb', line 129 def error(msg = '') $stderr.puts(msg) exit(1) end |
#form_request(net_obj, route, data, headers, error_message) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/conflux/helpers.rb', line 143 def form_request(net_obj, route, data, headers, ) data ||= {} headers ||= {} route = data.empty? ? route : "#{route}?#{URI.encode_www_form(data)}" request = net_obj.new("/api#{route}") request.add_field('Content-Type', 'application/x-www-form-urlencoded') add_headers(request, headers) response = http.request(request) handle_json_response(response, ) end |
#handle_json_response(response, error_message) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/conflux/helpers.rb', line 169 def handle_json_response(response, ) if response.code.to_i == 200 JSON.parse(response.body) rescue {} else error() end end |
#host_url ⇒ Object
134 135 136 |
# File 'lib/conflux/helpers.rb', line 134 def host_url ENV['CONFLUX_HOST'] || 'https://api.goconflux.com' end |
#http ⇒ Object
138 139 140 141 |
# File 'lib/conflux/helpers.rb', line 138 def http uri = URI.parse(host_url) Net::HTTP.new(uri.host, uri.port) end |
#json_request(net_obj, route, data, headers, error_message) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/conflux/helpers.rb', line 154 def json_request(net_obj, route, data, headers, ) data ||= {} headers ||= {} request = net_obj.new("/api#{route}") request.add_field('Content-Type', 'application/json') add_headers(request, headers) request.body = data.to_json response = http.request(request) handle_json_response(response, ) end |
#prompt_user_to_select_app(apps_map) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/conflux/helpers.rb', line 88 def prompt_user_to_select_app(apps_map) answer = nil question = "\nWhich Conflux app does this project belong to?\n" # Keep asking until the user responds with one of the possible answers until !answer.nil? count = 0 app_slugs = [] puts question apps_map.each { |team, apps| puts "\n#{team}:\n\n" # separate apps out by team for easier selection apps.each { |slug| count += 1 puts "(#{count}) #{slug}" app_slugs << slug } } puts "\n" response = allow_user_response # it's fine if the user responds with an exact app slug if app_slugs.include?(response) answer = response # otherwise, they can just respond with the number next to the app they wish to choose else response_int = response.to_i rescue 0 answer = app_slugs[response_int - 1 ]if response_int > 0 end question = "\nSorry I didn't catch that. Can you respond with the number that appears next to your answer?" end answer end |
#running_on_windows? ⇒ Boolean
84 85 86 |
# File 'lib/conflux/helpers.rb', line 84 def running_on_windows? RUBY_PLATFORM =~ /mswin32|mingw32/ end |
#with_tty(&block) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/conflux/helpers.rb', line 71 def with_tty(&block) return unless $stdin.isatty begin yield rescue # fails on windows end end |