rslack 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Gemfile +6 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/rslack +6 -0
- data/bin/setup +8 -0
- data/lib/rslack.rb +46 -0
- data/lib/rslack/action.rb +37 -0
- data/lib/rslack/actions/cron.rb +115 -0
- data/lib/rslack/actions/echo.rb +15 -0
- data/lib/rslack/actions/help.rb +32 -0
- data/lib/rslack/actions/ping.rb +15 -0
- data/lib/rslack/api.rb +42 -0
- data/lib/rslack/bot.rb +149 -0
- data/lib/rslack/version.rb +3 -0
- data/rslack.gemspec +34 -0
- metadata +201 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: ed64057e1a6a7e9a5c3541d6ec477c551b480ce6
         | 
| 4 | 
            +
              data.tar.gz: 9b0f306e0a6a311e4c862b58b48c92eee20e7f09
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: e3951ed8d9a74f734bdf9294009a24a88d8d1a4ed2c5e471ece1da4028623e6d93522f76bf820f0aee9ebbe92ddeefedf9d21d6119068577ae831225aaddd063
         | 
| 7 | 
            +
              data.tar.gz: e904de9637818adf05ad7cf9593d106a5631ba1e88dcea2e98b8687e59745ff3fd91f8306df114c5071859b0d9fbce36a323001e410e812739560ddf12d3972b
         | 
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            # RSlack
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rslack`. To experiment with that code, run `bin/console` for an interactive prompt.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            TODO: Delete this and the text above, and describe your gem
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            ## Installation
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            Add this line to your application's Gemfile:
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            ```ruby
         | 
| 12 | 
            +
            gem 'rslack'
         | 
| 13 | 
            +
            ```
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            And then execute:
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                $ bundle
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            Or install it yourself as:
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                $ gem install rslack
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            ## Usage
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            TODO: Write usage instructions here
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            ## Development
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            ## Contributing
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rslack.
         | 
    
        data/Rakefile
    ADDED
    
    
    
        data/bin/console
    ADDED
    
    | @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "bundler/setup"
         | 
| 4 | 
            +
            require "rslack"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # You can add fixtures and/or initialization code here to make experimenting
         | 
| 7 | 
            +
            # with your gem easier. You can also use a different console, if you like.
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # (If you use this, don't forget to add pry to your Gemfile!)
         | 
| 10 | 
            +
            # require "pry"
         | 
| 11 | 
            +
            # Pry.start
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            require "irb"
         | 
| 14 | 
            +
            IRB.start(__FILE__)
         | 
    
        data/bin/rslack
    ADDED
    
    
    
        data/bin/setup
    ADDED
    
    
    
        data/lib/rslack.rb
    ADDED
    
    | @@ -0,0 +1,46 @@ | |
| 1 | 
            +
            require 'bundler'
         | 
| 2 | 
            +
            require 'dotenv'
         | 
| 3 | 
            +
            require 'optparse'
         | 
| 4 | 
            +
            require 'ostruct'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            module RSlack
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              class << self
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def run(args = ARGV)
         | 
| 11 | 
            +
                  bot = build(args)
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  Signal.trap(:INT) do
         | 
| 14 | 
            +
                    bot.force_stop
         | 
| 15 | 
            +
                    exit 0
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  bot.start
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                private
         | 
| 22 | 
            +
                def build(args)
         | 
| 23 | 
            +
                  Dotenv.load
         | 
| 24 | 
            +
                  Bundler.require
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                  config = OpenStruct.new
         | 
| 27 | 
            +
                  option = OptionParser.new
         | 
| 28 | 
            +
                  option.on('--log FILE',    'Path to log file.') { |v| config.logfile = v }
         | 
| 29 | 
            +
                  option.on('--token TOKEN', 'Slack API token.')  { |v| config.token   = v }
         | 
| 30 | 
            +
                  option.parse!(args)
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  config.token ||= ENV['SLACK_API_TOKEN']
         | 
| 33 | 
            +
                  RSlack::Bot.new(config.token, log: config.logfile)
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            require 'rslack/bot'
         | 
| 41 | 
            +
            require 'rslack/action'
         | 
| 42 | 
            +
            require 'rslack/actions/cron'
         | 
| 43 | 
            +
            require 'rslack/actions/echo'
         | 
| 44 | 
            +
            require 'rslack/actions/ping'
         | 
| 45 | 
            +
            require 'rslack/actions/help'
         | 
| 46 | 
            +
            require 'rslack/version'
         | 
| @@ -0,0 +1,37 @@ | |
| 1 | 
            +
            require 'ostruct'
         | 
| 2 | 
            +
            require 'singleton'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module RSlack
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              class Action
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                class << self
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  def route(regexp, **opts, &block)
         | 
| 11 | 
            +
                    case callback = opts[:callback]
         | 
| 12 | 
            +
                    when nil  then opts[:callback] = block
         | 
| 13 | 
            +
                    when Proc then nil
         | 
| 14 | 
            +
                    when String, Symbol
         | 
| 15 | 
            +
                      opts[:callback] = lambda { |msg| send(callback, msg) }
         | 
| 16 | 
            +
                    end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                    route = OpenStruct.new(**opts.merge(action: self, regexp: regexp))
         | 
| 19 | 
            +
                    RSlack::Bot.register(route)
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
             | 
| 25 | 
            +
                attr_reader :bot
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                def initialize(bot)
         | 
| 28 | 
            +
                  @bot = bot
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                def memory
         | 
| 32 | 
            +
                  bot.memory[self.class.name] ||= {}
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            end
         | 
| @@ -0,0 +1,115 @@ | |
| 1 | 
            +
            require 'stringio'
         | 
| 2 | 
            +
            require 'parse-cron'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module RSlack
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              class CronAction < Action
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                route /cron add (?<syntax>\S+ \S+ \S+ \S+ \S+) (?<command>.+)$/,
         | 
| 9 | 
            +
                  helptext: 'Add cron schedule.',
         | 
| 10 | 
            +
                  callback: 'add'
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                route /cron del (?<id>\S+)/,
         | 
| 13 | 
            +
                  helptext: 'Delete cron schedule.',
         | 
| 14 | 
            +
                  callback: 'del'
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                route /cron list/,
         | 
| 17 | 
            +
                  helptext: 'List cron schedules.',
         | 
| 18 | 
            +
                  callback: 'list'
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                class << self
         | 
| 21 | 
            +
                  attr_accessor :timer
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                def initialize(bot)
         | 
| 25 | 
            +
                  super(bot)
         | 
| 26 | 
            +
                  @channel = {}
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def add(msg)
         | 
| 30 | 
            +
                  start(msg.dup)
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  cron_id = unique_id
         | 
| 33 | 
            +
                  memory.update(cron_id => msg.dup)
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                  bot.reply(msg, text: "_added schedule_ `[#{cron_id}] #{msgtostr(msg)}`")
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                def del(msg)
         | 
| 39 | 
            +
                  stop
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                  cron_id = msg.var.id.to_i
         | 
| 42 | 
            +
                  deleted = memory.delete(cron_id)
         | 
| 43 | 
            +
                  raise "no such cron id: #{cron_id}" unless deleted
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  bot.reply(msg, text: "_deleted schedule_ `[#{cron_id}] #{msgtostr(deleted)}`")
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                def list(msg)
         | 
| 49 | 
            +
                  items = memory.select { |_, item| msg.channel == item.channel }
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                  unless items.empty?
         | 
| 52 | 
            +
                    content = StringIO.new
         | 
| 53 | 
            +
                    content.puts "```"
         | 
| 54 | 
            +
                    content.puts items.map { |cron_id, item| "[#{cron_id}] #{msgtostr(item)}" }
         | 
| 55 | 
            +
                    content.puts "```"
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                    bot.reply(msg, text: content.string)
         | 
| 58 | 
            +
                  else
         | 
| 59 | 
            +
                    bot.reply(msg, text: "_no schedules found_.")
         | 
| 60 | 
            +
                  end
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                def start(msg)
         | 
| 64 | 
            +
                  parser = CronParser.new(msg.var.syntax)
         | 
| 65 | 
            +
                  sleep  = parser.next(Time.now) - Time.now
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                  CronAction.timer = EM::Timer.new(sleep) do
         | 
| 68 | 
            +
                    item = OpenStruct.new(msg.to_h.merge(text: msg.var.command))
         | 
| 69 | 
            +
                    item.delete_field(:var)
         | 
| 70 | 
            +
                    bot.react(item)
         | 
| 71 | 
            +
                    CronAction.timer = start(msg)
         | 
| 72 | 
            +
                  end
         | 
| 73 | 
            +
                end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                def stop
         | 
| 76 | 
            +
                  CronAction.timer.cancel
         | 
| 77 | 
            +
                end
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                private
         | 
| 80 | 
            +
                def msgtostr(msg)
         | 
| 81 | 
            +
                  @channel[msg.channel] ||= bot.client.channel(msg.channel).dig('channel', 'name')
         | 
| 82 | 
            +
                  "##{@channel[msg.channel]} #{msg.var.syntax} #{msg.var.command}"
         | 
| 83 | 
            +
                end
         | 
| 84 | 
            +
             | 
| 85 | 
            +
                def unique_id
         | 
| 86 | 
            +
                  loop do
         | 
| 87 | 
            +
                    cron_id = (1000..9999).to_a.sample
         | 
| 88 | 
            +
                    return cron_id unless memory.key?(cron_id)
         | 
| 89 | 
            +
                  end
         | 
| 90 | 
            +
                end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
              end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
              class Bot
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                module CronApplicable
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                  def wait_for_message
         | 
| 99 | 
            +
                    crons = memory['RSlack::CronAction']
         | 
| 100 | 
            +
                    unless crons.empty?
         | 
| 101 | 
            +
                      crons.each do |_, item|
         | 
| 102 | 
            +
                        action = RSlack::CronAction.new(self)
         | 
| 103 | 
            +
                        action.start(item)
         | 
| 104 | 
            +
                      end
         | 
| 105 | 
            +
                      logger.info("#{self.class} cron schedules loaded.")
         | 
| 106 | 
            +
                    end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
                    super
         | 
| 109 | 
            +
                  end
         | 
| 110 | 
            +
                end
         | 
| 111 | 
            +
                prepend CronApplicable
         | 
| 112 | 
            +
             | 
| 113 | 
            +
              end
         | 
| 114 | 
            +
             | 
| 115 | 
            +
            end
         | 
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            require 'stringio'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module RSlack
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              class HelpAction < Action
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                route /help(?<options> --all)?/,
         | 
| 8 | 
            +
                  helptext: 'Show this message.',
         | 
| 9 | 
            +
                  callback: 'help'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def help(msg)
         | 
| 12 | 
            +
                  options = msg.var.options
         | 
| 13 | 
            +
                  routes  = bot.routes.select { |r| r.helptext || options.to_s.split.include?('--all') }
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  content = StringIO.new
         | 
| 16 | 
            +
                  content.puts "```"
         | 
| 17 | 
            +
                  routes.map do |route|
         | 
| 18 | 
            +
                    prefix = [(bot.name unless route.matchall), route.regexp.inspect].compact.join(' ') + ':'
         | 
| 19 | 
            +
                    indent = (prefix.size / 4 + 1) * 4
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                    content.print "#{prefix.ljust(indent)}"
         | 
| 22 | 
            +
                    content.print "`#{route.helptext}`" if route.helptext
         | 
| 23 | 
            +
                    content.puts
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
                  content.puts "```"
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  bot.reply(msg, text: content.string)
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            end
         | 
    
        data/lib/rslack/api.rb
    ADDED
    
    | @@ -0,0 +1,42 @@ | |
| 1 | 
            +
            require 'http'
         | 
| 2 | 
            +
            require 'json'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module RSlack
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              class APIClient
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                BASEURL = "https://slack.com/api/"
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                attr_accessor :token
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def initialize(token)
         | 
| 13 | 
            +
                  @token = token
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def rtm
         | 
| 17 | 
            +
                  call('rtm.start')
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                def auth
         | 
| 21 | 
            +
                  call('auth.test')
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                def post(text: '', **params)
         | 
| 25 | 
            +
                  call('chat.postMessage', params.merge(text: text))
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                def user(id)
         | 
| 29 | 
            +
                  call('users.info', user: id)
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                def channel(id)
         | 
| 33 | 
            +
                  call('channels.info', channel: id)
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                def call(endpoint, **params)
         | 
| 37 | 
            +
                  resp = HTTP.post(BASEURL + endpoint, params: { token: @token }.merge(params))
         | 
| 38 | 
            +
                  JSON.parse(resp.body)
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            end
         | 
    
        data/lib/rslack/bot.rb
    ADDED
    
    | @@ -0,0 +1,149 @@ | |
| 1 | 
            +
            require 'eventmachine'
         | 
| 2 | 
            +
            require 'faye/websocket'
         | 
| 3 | 
            +
            require 'rslack/api'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            require 'json'
         | 
| 6 | 
            +
            require 'leveldb'
         | 
| 7 | 
            +
            require 'logger'
         | 
| 8 | 
            +
            require 'thread'
         | 
| 9 | 
            +
            require 'ostruct'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            module RSlack
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              class Bot
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                class << self
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def register(route)
         | 
| 18 | 
            +
                    routes.push(route)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  private
         | 
| 22 | 
            +
                  def routes
         | 
| 23 | 
            +
                    @routes ||= []
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                attr_reader :client
         | 
| 29 | 
            +
                attr_reader :logger
         | 
| 30 | 
            +
                attr_reader :memory
         | 
| 31 | 
            +
                attr_reader :routes
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                STORAGE_KEYNAME  = 'rslack'
         | 
| 34 | 
            +
                STORAGE_INTERVAL = 5
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                def initialize(token, log: nil)
         | 
| 37 | 
            +
                  @client  = APIClient.new(token)
         | 
| 38 | 
            +
                  @logger  = Logger.new(log || STDOUT)
         | 
| 39 | 
            +
                  @memory  = {}
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                  @routes  = self.class.instance_variable_get(:@routes)
         | 
| 42 | 
            +
                  @storage = LevelDB::DB.new(File.join(Dir.pwd, "#{STORAGE_KEYNAME}.db"))
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                  @auth = @client.auth
         | 
| 45 | 
            +
                  unless @auth['ok']
         | 
| 46 | 
            +
                    @logger.fatal("#{self.class} Failed to access slack web api.")
         | 
| 47 | 
            +
                    exit 1
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                def name
         | 
| 52 | 
            +
                  "@#{@auth['user']}"
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                def linked_name
         | 
| 56 | 
            +
                  "<@#{@auth['user_id']}>"
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                def reply(msg, **opts)
         | 
| 60 | 
            +
                  @client.post(**msg.to_h.merge(**opts))
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                def react(msg)
         | 
| 64 | 
            +
                  count = 0
         | 
| 65 | 
            +
                  @routes.each do |route|
         | 
| 66 | 
            +
                    matched = route.regexp.match(msg.text)
         | 
| 67 | 
            +
                    next unless matched
         | 
| 68 | 
            +
                    next unless route.matchall || msg.prefix == linked_name
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                    count += 1
         | 
| 71 | 
            +
                    message = msg.dup
         | 
| 72 | 
            +
                    message.var = OpenStruct.new(matched.named_captures)
         | 
| 73 | 
            +
                    logger.info("#{self.class} passed msg to: '#{route.action}'.")
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                    th = Thread.start do
         | 
| 76 | 
            +
                      begin
         | 
| 77 | 
            +
                        action = route.action.new(self)
         | 
| 78 | 
            +
                        action.instance_exec(message.freeze, &route.callback)
         | 
| 79 | 
            +
                      rescue => e
         | 
| 80 | 
            +
                        reply(msg, text: "_#{e}._")
         | 
| 81 | 
            +
                      end
         | 
| 82 | 
            +
                    end
         | 
| 83 | 
            +
                    th.abort_on_exception = true
         | 
| 84 | 
            +
                  end
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                  reply(msg, text: 'I have no idea what to do...') if msg.prefix == linked_name && count.zero?
         | 
| 87 | 
            +
                end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                def start
         | 
| 90 | 
            +
                  load_storage
         | 
| 91 | 
            +
                  EM.run { wait_for_message }
         | 
| 92 | 
            +
                end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                def stop
         | 
| 95 | 
            +
                  EM.stop
         | 
| 96 | 
            +
                  dump_storage
         | 
| 97 | 
            +
                end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                def force_stop
         | 
| 100 | 
            +
                  EM.stop
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                def restart
         | 
| 104 | 
            +
                  stop
         | 
| 105 | 
            +
                  start
         | 
| 106 | 
            +
                end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
                private
         | 
| 109 | 
            +
                def wait_for_message
         | 
| 110 | 
            +
                  EM.add_periodic_timer(STORAGE_INTERVAL) { dump_storage }
         | 
| 111 | 
            +
             | 
| 112 | 
            +
                  url = @client.rtm['url']
         | 
| 113 | 
            +
                  rtm = Faye::WebSocket::Client.new(url)
         | 
| 114 | 
            +
             | 
| 115 | 
            +
                  rtm.on(:open) do |event|
         | 
| 116 | 
            +
                    logger.info("#{self.class} slack realtime api connection has opened.")
         | 
| 117 | 
            +
                  end
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                  rtm.on(:close) do |event|
         | 
| 120 | 
            +
                    logger.info("#{self.class} slack realtime api connection has closed.")
         | 
| 121 | 
            +
                    restart if EM.reactor_running?
         | 
| 122 | 
            +
                  end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                  rtm.on(:message) do |event|
         | 
| 125 | 
            +
                    msg = OpenStruct.new(JSON.parse(event.data))
         | 
| 126 | 
            +
                    if msg.type == 'message' && msg.subtype.nil?
         | 
| 127 | 
            +
                      logger.info("#{self.class} recieved msg: '#{msg.text}'.")
         | 
| 128 | 
            +
             | 
| 129 | 
            +
                      if msg.text =~ /^\s*(#{linked_name})\s*(.*)$/
         | 
| 130 | 
            +
                        msg.prefix = $1.to_s
         | 
| 131 | 
            +
                        msg.text =   $2.to_s
         | 
| 132 | 
            +
                      end
         | 
| 133 | 
            +
             | 
| 134 | 
            +
                      react(msg)
         | 
| 135 | 
            +
                    end
         | 
| 136 | 
            +
                  end
         | 
| 137 | 
            +
                end
         | 
| 138 | 
            +
             | 
| 139 | 
            +
                def dump_storage
         | 
| 140 | 
            +
                  @storage[STORAGE_KEYNAME] = Marshal.dump(@memory)
         | 
| 141 | 
            +
                end
         | 
| 142 | 
            +
             | 
| 143 | 
            +
                def load_storage
         | 
| 144 | 
            +
                  @memory = Marshal.load(@storage[STORAGE_KEYNAME]) if @storage[STORAGE_KEYNAME]
         | 
| 145 | 
            +
                end
         | 
| 146 | 
            +
             | 
| 147 | 
            +
              end
         | 
| 148 | 
            +
             | 
| 149 | 
            +
            end
         | 
    
        data/rslack.gemspec
    ADDED
    
    | @@ -0,0 +1,34 @@ | |
| 1 | 
            +
            # coding: utf-8
         | 
| 2 | 
            +
            lib = File.expand_path("../lib", __FILE__)
         | 
| 3 | 
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 4 | 
            +
            require "rslack/version"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |spec|
         | 
| 7 | 
            +
              spec.name          = "rslack"
         | 
| 8 | 
            +
              spec.version       = RSlack::VERSION
         | 
| 9 | 
            +
              spec.authors       = ["haccht"]
         | 
| 10 | 
            +
              spec.email         = ["haccht@users.noreply.github.com"]
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              spec.summary       = %q{super simple ruby slack bot framework.}
         | 
| 13 | 
            +
              spec.description   = %q{super simple ruby slack bot framework.}
         | 
| 14 | 
            +
              spec.homepage      = "http://github.com/haccht/rslack"
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              spec.files         = `git ls-files -z`.split("\x0").reject do |f|
         | 
| 17 | 
            +
                f.match(%r{^(test|spec|features)/})
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
              spec.bindir        = "bin"
         | 
| 20 | 
            +
              spec.executables   = ["rslack"]
         | 
| 21 | 
            +
              spec.require_paths = ["lib"]
         | 
| 22 | 
            +
             | 
| 23 | 
            +
              spec.add_development_dependency "bundler", "~> 1.15"
         | 
| 24 | 
            +
              spec.add_development_dependency "rake", "~> 10.0"
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              spec.add_dependency "bundler"
         | 
| 27 | 
            +
              spec.add_dependency "dotenv"
         | 
| 28 | 
            +
              spec.add_dependency "eventmachine"
         | 
| 29 | 
            +
              spec.add_dependency "faye-websocket"
         | 
| 30 | 
            +
              spec.add_dependency "http"
         | 
| 31 | 
            +
              spec.add_dependency "parse-cron"
         | 
| 32 | 
            +
              spec.add_dependency "leveldb"
         | 
| 33 | 
            +
              spec.add_dependency "rake"
         | 
| 34 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,201 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: rslack
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.2.5
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - haccht
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2018-03-05 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: bundler
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '1.15'
         | 
| 20 | 
            +
              type: :development
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '1.15'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: rake
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - "~>"
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '10.0'
         | 
| 34 | 
            +
              type: :development
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - "~>"
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '10.0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: bundler
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - ">="
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '0'
         | 
| 48 | 
            +
              type: :runtime
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - ">="
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '0'
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: dotenv
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - ">="
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: '0'
         | 
| 62 | 
            +
              type: :runtime
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - ">="
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: '0'
         | 
| 69 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: eventmachine
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - ">="
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '0'
         | 
| 76 | 
            +
              type: :runtime
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - ">="
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: faye-websocket
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - ">="
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '0'
         | 
| 90 | 
            +
              type: :runtime
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - ">="
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 97 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 98 | 
            +
              name: http
         | 
| 99 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 100 | 
            +
                requirements:
         | 
| 101 | 
            +
                - - ">="
         | 
| 102 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 103 | 
            +
                    version: '0'
         | 
| 104 | 
            +
              type: :runtime
         | 
| 105 | 
            +
              prerelease: false
         | 
| 106 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 107 | 
            +
                requirements:
         | 
| 108 | 
            +
                - - ">="
         | 
| 109 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 110 | 
            +
                    version: '0'
         | 
| 111 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 112 | 
            +
              name: parse-cron
         | 
| 113 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 114 | 
            +
                requirements:
         | 
| 115 | 
            +
                - - ">="
         | 
| 116 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 117 | 
            +
                    version: '0'
         | 
| 118 | 
            +
              type: :runtime
         | 
| 119 | 
            +
              prerelease: false
         | 
| 120 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 121 | 
            +
                requirements:
         | 
| 122 | 
            +
                - - ">="
         | 
| 123 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 124 | 
            +
                    version: '0'
         | 
| 125 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 126 | 
            +
              name: leveldb
         | 
| 127 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 128 | 
            +
                requirements:
         | 
| 129 | 
            +
                - - ">="
         | 
| 130 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 131 | 
            +
                    version: '0'
         | 
| 132 | 
            +
              type: :runtime
         | 
| 133 | 
            +
              prerelease: false
         | 
| 134 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 135 | 
            +
                requirements:
         | 
| 136 | 
            +
                - - ">="
         | 
| 137 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 138 | 
            +
                    version: '0'
         | 
| 139 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 140 | 
            +
              name: rake
         | 
| 141 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 142 | 
            +
                requirements:
         | 
| 143 | 
            +
                - - ">="
         | 
| 144 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 145 | 
            +
                    version: '0'
         | 
| 146 | 
            +
              type: :runtime
         | 
| 147 | 
            +
              prerelease: false
         | 
| 148 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 149 | 
            +
                requirements:
         | 
| 150 | 
            +
                - - ">="
         | 
| 151 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 152 | 
            +
                    version: '0'
         | 
| 153 | 
            +
            description: super simple ruby slack bot framework.
         | 
| 154 | 
            +
            email:
         | 
| 155 | 
            +
            - haccht@users.noreply.github.com
         | 
| 156 | 
            +
            executables:
         | 
| 157 | 
            +
            - rslack
         | 
| 158 | 
            +
            extensions: []
         | 
| 159 | 
            +
            extra_rdoc_files: []
         | 
| 160 | 
            +
            files:
         | 
| 161 | 
            +
            - ".gitignore"
         | 
| 162 | 
            +
            - Gemfile
         | 
| 163 | 
            +
            - README.md
         | 
| 164 | 
            +
            - Rakefile
         | 
| 165 | 
            +
            - bin/console
         | 
| 166 | 
            +
            - bin/rslack
         | 
| 167 | 
            +
            - bin/setup
         | 
| 168 | 
            +
            - lib/rslack.rb
         | 
| 169 | 
            +
            - lib/rslack/action.rb
         | 
| 170 | 
            +
            - lib/rslack/actions/cron.rb
         | 
| 171 | 
            +
            - lib/rslack/actions/echo.rb
         | 
| 172 | 
            +
            - lib/rslack/actions/help.rb
         | 
| 173 | 
            +
            - lib/rslack/actions/ping.rb
         | 
| 174 | 
            +
            - lib/rslack/api.rb
         | 
| 175 | 
            +
            - lib/rslack/bot.rb
         | 
| 176 | 
            +
            - lib/rslack/version.rb
         | 
| 177 | 
            +
            - rslack.gemspec
         | 
| 178 | 
            +
            homepage: http://github.com/haccht/rslack
         | 
| 179 | 
            +
            licenses: []
         | 
| 180 | 
            +
            metadata: {}
         | 
| 181 | 
            +
            post_install_message: 
         | 
| 182 | 
            +
            rdoc_options: []
         | 
| 183 | 
            +
            require_paths:
         | 
| 184 | 
            +
            - lib
         | 
| 185 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 186 | 
            +
              requirements:
         | 
| 187 | 
            +
              - - ">="
         | 
| 188 | 
            +
                - !ruby/object:Gem::Version
         | 
| 189 | 
            +
                  version: '0'
         | 
| 190 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 191 | 
            +
              requirements:
         | 
| 192 | 
            +
              - - ">="
         | 
| 193 | 
            +
                - !ruby/object:Gem::Version
         | 
| 194 | 
            +
                  version: '0'
         | 
| 195 | 
            +
            requirements: []
         | 
| 196 | 
            +
            rubyforge_project: 
         | 
| 197 | 
            +
            rubygems_version: 2.6.11
         | 
| 198 | 
            +
            signing_key: 
         | 
| 199 | 
            +
            specification_version: 4
         | 
| 200 | 
            +
            summary: super simple ruby slack bot framework.
         | 
| 201 | 
            +
            test_files: []
         |