Sha256: 3582bd0f3e03c3a02d1d3ffd5c157ef32a0b1a3ebded9e2a9dc04e3e8983ed29

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'ps_pabx_listener'
require 'getoptlong'

module PsPabxListener
  class Robot
    def initialize
      @get_time = 30
      load_options
      load_arguments
      @main = PsPabxListener::Main.new(host: @host,
                              user: @user,
                              password: @password,
                              timeout: 1)
    end

    def run
      if @daemonize
        @main.each_data do |data|
          puts data
          store_data(data, generate_filename)
        end
      else
        @main.get_data(@get_time) {|data| puts data}
      end
    end

    private

    def load_options
      @args = GetoptLong.new(
        ['--host', '-s', GetoptLong::REQUIRED_ARGUMENT],
        ['--user', '-u', GetoptLong::REQUIRED_ARGUMENT],
        ['--password', '-p', GetoptLong::REQUIRED_ARGUMENT],
        ['--get-time', '-t', GetoptLong::OPTIONAL_ARGUMENT],
        ['--daemonize', '-d', GetoptLong::OPTIONAL_ARGUMENT]
      )
    end

    def load_arguments
      @args.each do |opt, arg|
        case opt
          when '--host' then @host = arg
          when '--user' then @user = arg
          when '--password' then @password = arg
          when '--get-time' then @get_time = arg.to_i
          when '--daemonize'
            @daemonize = true
            @store_dir = arg
        end
      end
    end

    def store_data data, filename
      File.open(File.join(@store_dir,filename), 'a', crlf_newline: true) do |f|
        f.puts data
      end
    end

    def generate_filename
      Time.now.strftime("%y%m%d") + (Time.now.hour / 12).to_s + ".txt"
    end

  end
end

robot = PsPabxListener::Robot.new
robot.run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ps_pabx_listener-1.0.2 exe/ps_pabx