module Conflux::Helpers

Public Instance Methods

add_headers(request, headers) click to toggle source
# File lib/conflux/helpers.rb, line 175
def add_headers(request, headers)
  headers.each { |key, val| request.add_field(key, val) }
end
allow_user_response() click to toggle source
# File lib/conflux/helpers.rb, line 80
def allow_user_response
  $stdin.gets.to_s.strip
end
ask_for_basic_creds() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/conflux/helpers.rb, line 187
def conflux_folder_path
  "#{Dir.pwd}/.conflux/"
end
conflux_manifest_path() click to toggle source
# File lib/conflux/helpers.rb, line 191
def conflux_manifest_path
  File.join(conflux_folder_path, 'manifest.json')
end
echo_off() click to toggle source

Hide user input

# File lib/conflux/helpers.rb, line 58
def echo_off
  with_tty do
    system 'stty -echo'
  end
end
echo_on() click to toggle source

Show user input

# File lib/conflux/helpers.rb, line 65
def echo_on
  with_tty do
    system 'stty echo'
  end
end
error(msg = '') click to toggle source
# 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) click to toggle source
# File lib/conflux/helpers.rb, line 153
def form_request(net_obj, route, data, headers, error_message)
  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, error_message)
end
handle_json_response(response, error_message) click to toggle source
# File lib/conflux/helpers.rb, line 179
def handle_json_response(response, error_message)
  if response.code.to_i == 200
    JSON.parse(response.body) rescue {}
  else
    error(error_message)
  end
end
host_url() click to toggle source
# File lib/conflux/helpers.rb, line 134
def host_url
  ENV['CONFLUX_HOST'] || 'https://api.goconflux.com'
end
http() click to toggle source
# File lib/conflux/helpers.rb, line 145
def http
  uri = URI.parse(host_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  ssl_chek_win(http)
  http
end
json_request(net_obj, route, data, headers, error_message) click to toggle source
# File lib/conflux/helpers.rb, line 164
def json_request(net_obj, route, data, headers, error_message)
  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, error_message)
end
prompt_user_to_select_app(apps_map) click to toggle source
# File lib/conflux/helpers.rb, line 88
def prompt_user_to_select_app(apps_map)
  answer = nil
  question = "\nWhich Conflux bundle do you wish to use for this project?\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?() click to toggle source
# File lib/conflux/helpers.rb, line 84
def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end
ssl_chek_win(net_http) click to toggle source
# File lib/conflux/helpers.rb, line 138
def ssl_chek_win(net_http)
  case RUBY_PLATFORM
    when /win/i, /ming/i
      net_http.verify_mode = OpenSSL::SSL::VERIFY_NONE if net_http.use_ssl?
  end
end
with_tty() { || ... } click to toggle source
# File lib/conflux/helpers.rb, line 71
def with_tty(&block)
  return unless $stdin.isatty
  begin
    yield
  rescue
    # fails on windows
  end
end