Method: UltraHook::Client#start

Defined in:
lib/ultrahook/client.rb

#start(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/ultrahook/client.rb', line 13

def start(*args)
  @options = {}

  optparse = OptionParser.new do |o|
    o.banner = "Usage: ultrahook [options] <subdomain> <destination>"

    o.on("-k", "--key <api key>", String, "API Key") do |key|
      @options["key"] = key
    end

    o.on_tail("-h", "--help", "Display this help") do
      puts o
      exit
    end

    o.on("-V", "--version", "Display client version") do
      puts "UltraHook client version: #{UltraHook::VERSION}"
      exit
    end
  end
  optparse.parse!(args)

  if @options["key"].nil? || @options["key"] == ""
    @options["key"] = ENV["ULTRAHOOK_API_KEY"]
  end

  if @options["key"].nil? || @options["key"] == ""
    path = File.expand_path("~/.ultrahook")
    if File.readable?(path)
      if matchdata = /api_key:\s*([^\s]+)/.match(IO.read(path))
        @options["key"] = matchdata.captures[0]
      end
    end
  end

  if @options["key"].nil? || @options["key"] == ""
    error "An API key must be provided.  Get one from http://www.ultrahook.com"
  end

  if args.size != 2
    puts optparse
    exit
  end

  @options["host"] = args[0].downcase
  error "Subdomain can only contain alphanumeric characters and hyphen (-)" unless @options["host"] =~ /^[[:alnum:]\-]+$/
  error "Subdomain cannot start or end with hyphens" if @options["host"] =~ /^\-/ || @options["host"] =~ /\-$/
  error "Subdomain cannot contain consecutive hyphens" if @options["host"] =~ /\-\-/

  @options["destination"] = parse_destination(args[1])

  retrieve_config
  init_stream
end