class OceanWechatRobot::Command

Public Class Methods

new(argv) click to toggle source
2

初始化 @param argv 是 CLAide::ARGV 类型

Calls superclass method
# File lib/ocean_wechat_robot/command.rb, line 34
def initialize(argv)
  super
  @webhook = argv.option('webhook')
  @msg = argv.option('msg')
  @msg_type = argv.option('msg-type')
  @at_mobiles = argv.option("at-mobiles", "").split(",")
end
options() click to toggle source

参数选项

Calls superclass method
# File lib/ocean_wechat_robot/command.rb, line 15
def self.options
  [
      ['--webhook=wechat group webhook', '微信群的webhook'],
      ['--msg=content', '微信群消息'],
      ['--msg-type=[text|markdown|news]', '微信群消息的类型'],
      ['--at-mobiles=phone', '@人的手机号,多个用,分割'],
  ].concat(super)
end
run(argv) click to toggle source
1

运行入口,这里可以坐一些初始化,其他的依赖校验,比如 git 版本等等 @param argv 是 Array 类型

Calls superclass method
# File lib/ocean_wechat_robot/command.rb, line 27
def self.run(argv)
  super(argv)
end

Public Instance Methods

run() click to toggle source
3

重写父类run,执行逻辑,不需要调用 super

# File lib/ocean_wechat_robot/command.rb, line 66
def run
  wechat = OceanWechatRobot::WechatRobot.new("#{@webhook}")
  if @msg_type == 'text'
    wechat.send_text(@msg, @at_mobiles)
  elsif @msg_type == 'markdown'
    wechat.send_markdown(@msg, @at_mobiles)
  elsif @msg_type == 'news'
    # wechat.send_news('', '', '', '', @at_mobiles)
    raise 'news 类型未实现......'
  end
end
validate!() click to toggle source
3

参数校验

Calls superclass method
# File lib/ocean_wechat_robot/command.rb, line 44
def validate!
  super

  unless @webhook
    help!('please set webhook value!')
  end

  unless @msg
    help!('please set msg value!')
  end

  unless @msg_type
    help!('please set msg type value!')
  end

  if @msg_type && !%w(text markdown news).include?(@msg_type)
    help! "`#{@msg_type}' is not a valid msg type!"
  end
end