Class: ZohoHub::Cli::CallbackServer

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho_hub/cli/callback_server.rb

Instance Method Summary collapse

Constructor Details

#initializeCallbackServer

Returns a new instance of CallbackServer.



12
13
14
# File 'lib/zoho_hub/cli/callback_server.rb', line 12

def initialize
  @options = {}
end

Instance Method Details

#configuration_incomplete?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/zoho_hub/cli/callback_server.rb', line 69

def configuration_incomplete?
  !ZohoHub.configuration.client_id || !ZohoHub.configuration.secret
end

#default_portObject



34
35
36
# File 'lib/zoho_hub/cli/callback_server.rb', line 34

def default_port
  ZohoHub::OauthCallbackServer.settings.port
end

#error_output(error) ⇒ Object



86
87
88
89
90
91
# 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) ⇒ Object



73
74
75
76
77
# 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) ⇒ Object



79
80
81
82
83
84
# 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

#parserObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



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
# 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