class ZohoHub::Cli::CallbackServer

Public Class Methods

new() click to toggle source
# File lib/zoho_hub/cli/callback_server.rb, line 12
def initialize
  @options = {}
end

Public Instance Methods

configuration_incomplete?() click to toggle source
# File lib/zoho_hub/cli/callback_server.rb, line 69
def configuration_incomplete?
  !ZohoHub.configuration.client_id || !ZohoHub.configuration.secret
end
default_port() click to toggle source
# File lib/zoho_hub/cli/callback_server.rb, line 34
def default_port
  ZohoHub::OauthCallbackServer.settings.port
end
error_output(error) click to toggle source
# File lib/zoho_hub/cli/callback_server.rb, line 86
def error_output(error)
  warn "Error: #{error}"
  warn "Try `#{parser.program_name} server --help' for more information"

  false
end
good_run(argv, env) click to toggle source
# File lib/zoho_hub/cli/callback_server.rb, line 73
def good_run(argv, env)
  return false unless parse(argv, env)

  true
end
parse(argv, _env) click to toggle source
# File lib/zoho_hub/cli/callback_server.rb, line 79
def parse(argv, _env)
  parser.parse!(argv)
  true
rescue OptionParser::ParseError => e
  error_output(e)
end
parser() click to toggle source
# File lib/zoho_hub/cli/callback_server.rb, line 16
def parser
  @parser ||= OptionParser.new do |op|
    op.banner = "Usage: #{op.program_name} server -c CLIENT_ID -s SECRET [options]"

    op.on('-c', '--client-id=client_id', 'The Zoho client ID') do |client|
      @options[:client_id] = client
    end

    op.on('-s', '--secret=secret', 'The Zoho secret') do |secret|
      @options[:secret] = secret
    end

    op.on('-p', '--port=port', "The port for your callback (#{default_port})") do |port|
      @options[:port] = port
    end
  end
end
run(argv = ARGV, env = ENV) click to toggle source
# File lib/zoho_hub/cli/callback_server.rb, line 38
def run(argv = ARGV, env = ENV)
  exit 1 unless good_run(argv, env)

  ZohoHub::OauthCallbackServer.set(:port, @options[:port]) if @options[:port]

  callback_path = ZohoHub::OauthCallbackServer::CALLBACK_PATH
  bind_port = ZohoHub::OauthCallbackServer.settings.port
  bind_address = ZohoHub::OauthCallbackServer.settings.bind

  callback_url = "http://#{bind_address}:#{bind_port}/#{callback_path}"

  ZohoHub.configure do |config|
    config.client_id    = @options[:client_id] || ENV['ZOHO_CLIENT_ID']
    config.secret       = @options[:secret] || ENV['ZOHO_SECRET']
    config.redirect_uri = callback_url
  end

  if configuration_incomplete?
    parser.parse %w[--help]
    exit 1
  end

  puts "Callback URL: #{callback_url}"

  url = ZohoHub::Auth.auth_url
  Launchy.open(url)

  puts 'Running callback server....'
  ZohoHub::OauthCallbackServer.run!
end