Class: Pechkin::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/pechkin.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Main

Returns a new instance of Main.



38
39
40
# File 'lib/pechkin.rb', line 38

def initialize(options)
  @options = options
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



36
37
38
# File 'lib/pechkin.rb', line 36

def configuration
  @configuration
end

#handlerObject (readonly)

Returns the value of attribute handler.



36
37
38
# File 'lib/pechkin.rb', line 36

def handler
  @handler
end

#optionsObject (readonly)

Returns the value of attribute options.



36
37
38
# File 'lib/pechkin.rb', line 36

def options
  @options
end

Instance Method Details

#add_authObject



93
94
95
96
97
# File 'lib/pechkin.rb', line 93

def add_auth
  user, password = options.add_auth.split(':')
  Pechkin::Auth::Manager.new(options.htpasswd).add(user, password)
  puts IO.read(options.htpasswd)
end

#parse_endpoint(endpoint) ⇒ Object



87
88
89
90
91
# File 'lib/pechkin.rb', line 87

def parse_endpoint(endpoint)
  endpoint.match(%r{^([^/]+)/(.+)}) do |m|
    [m[1], m[2]]
  end
end

#read_data(data) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/pechkin.rb', line 78

def read_data(data)
  return data unless data.start_with?('@')

  file = data[1..-1]
  raise "File not found #{file}" unless File.exist?(file)

  IO.read(file)
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pechkin.rb', line 42

def run
  if options.add_auth
    add_auth
    exit 0
  end

  @configuration = Configuration.load_from_directory(options.config_file)
  @handler = Handler.new(@configuration.channels)
  configuration.list if options.list?
  return if options.check?

  if options.send_data
    send_data
  else
    run_server
  end
end

#run_serverObject



60
61
62
63
64
65
# File 'lib/pechkin.rb', line 60

def run_server
  Rack::Server.start(app: AppBuilder.new.build(handler, options),
                     Port: options.port,
                     pid: options.pid_file,
                     Host: options.bind_address)
end

#send_dataObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/pechkin.rb', line 67

def send_data
  ch, msg = parse_endpoint(options.send_data)

  raise "#{ch}/#{msg} not found" unless handler.message?(ch, msg)

  data = read_data(options.data)

  handler.preview = options.preview
  handler.handle(ch, msg, JSON.parse(data))
end