twitch-chat 0.0.1
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 +14 -0
 - data/.rspec +2 -0
 - data/Gemfile +10 -0
 - data/LICENSE.txt +22 -0
 - data/README.md +95 -0
 - data/Rakefile +2 -0
 - data/lib/twitch/chat.rb +13 -0
 - data/lib/twitch/chat/channel.rb +20 -0
 - data/lib/twitch/chat/client.rb +183 -0
 - data/lib/twitch/chat/connection.rb +23 -0
 - data/lib/twitch/chat/message.rb +107 -0
 - data/lib/twitch/chat/version.rb +5 -0
 - data/spec/spec_helper.rb +78 -0
 - data/spec/twitch/chat/client_spec.rb +27 -0
 - data/spec/twitch/chat/message_spec.rb +66 -0
 - data/twitch-chat.gemspec +26 -0
 - metadata +96 -0
 
    
        checksums.yaml
    ADDED
    
    | 
         @@ -0,0 +1,7 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            ---
         
     | 
| 
      
 2 
     | 
    
         
            +
            SHA1:
         
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: b9db8c3b04b0f9630b408014a039149ff269abd8
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 7c9f1ebb43429aa041719a083c12779bbbe92a1d
         
     | 
| 
      
 5 
     | 
    
         
            +
            SHA512:
         
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 50d57cb10d4ad8e3f05d9d277758b3b3c02e14b49bb195591763e03e9e39c1e857d3a8525a26d2600616208cc8243b0146cbedf0a6633284fe508baf8c6008df
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 0fe8ac5374f2950e0983db92cf09a6e9cda1cf78e99300be1d48ba89aa57345d5ba324d6cc6ba6be0edaef3d35fa669bcddacadf525afb235f19f90ccc5679ff
         
     | 
    
        data/.gitignore
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | 
         @@ -0,0 +1,22 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2014 Pavel Astraukh
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            MIT License
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 6 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 7 
     | 
    
         
            +
            "Software"), to deal in the Software without restriction, including
         
     | 
| 
      
 8 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 9 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 10 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 11 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 14 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 17 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 18 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         
     | 
| 
      
 19 
     | 
    
         
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
      
 20 
     | 
    
         
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
      
 21 
     | 
    
         
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         
     | 
| 
      
 22 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README.md
    ADDED
    
    | 
         @@ -0,0 +1,95 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Twitch::Chat
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            twitch-chat library is a Twitch chat client that uses Twitch IRC. EventMachine is used to handle connections to servers. With the help of this library you can connect to any Twitch's channel and handle various chat events. Can be used as twitch chat bot engine.
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            ## Installation
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            Add this line to your application's Gemfile:
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 10 
     | 
    
         
            +
            gem 'twitch-chat'
         
     | 
| 
      
 11 
     | 
    
         
            +
            ```
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            And then execute:
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                $ bundle
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            Or install it yourself as:
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                $ gem install twitch-chat
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            ## Usage
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 24 
     | 
    
         
            +
            require 'twitch-chat'
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
            client = Twitch::Chat::Client.new(channel: 'channel', nickname: 'nickname', password: 'twitch_oath_token') do
         
     | 
| 
      
 27 
     | 
    
         
            +
              on(:connect) do
         
     | 
| 
      
 28 
     | 
    
         
            +
                send_message 'Hi guys!'
         
     | 
| 
      
 29 
     | 
    
         
            +
              end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
              on(:subscribe) do |user|
         
     | 
| 
      
 32 
     | 
    
         
            +
                client.send_message "Hi #{user}, thank you for subscription"
         
     | 
| 
      
 33 
     | 
    
         
            +
              end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
              on(:slow_mode) do
         
     | 
| 
      
 36 
     | 
    
         
            +
                send_message "Slow down guys"
         
     | 
| 
      
 37 
     | 
    
         
            +
              end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
              on(:subscribers_mode_off) do
         
     | 
| 
      
 40 
     | 
    
         
            +
                send_message "FREEEEEDOOOOOM"
         
     | 
| 
      
 41 
     | 
    
         
            +
              end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
              on(:message) do |user, message|
         
     | 
| 
      
 44 
     | 
    
         
            +
                send_message "Current time: #{Time.now.utc}" if message == '!time'
         
     | 
| 
      
 45 
     | 
    
         
            +
              end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
              on(:message) do |user, message|
         
     | 
| 
      
 48 
     | 
    
         
            +
                send_mesage "Hi #{user}!" if message.include?("Hi #{nickname}")
         
     | 
| 
      
 49 
     | 
    
         
            +
              end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
              on(:message) do |user, message|
         
     | 
| 
      
 52 
     | 
    
         
            +
                send_message channel.moderators.join(', ') if message == '!moderators'
         
     | 
| 
      
 53 
     | 
    
         
            +
              end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
              on(:new_moderator) do |user|
         
     | 
| 
      
 56 
     | 
    
         
            +
                send_message "#{user} is our new moderator"
         
     | 
| 
      
 57 
     | 
    
         
            +
              end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
              on(:remove_moderator) do |user|
         
     | 
| 
      
 60 
     | 
    
         
            +
                send_message "#{user} is no longer moderator"
         
     | 
| 
      
 61 
     | 
    
         
            +
              end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
              on(:disconnect) do
         
     | 
| 
      
 64 
     | 
    
         
            +
                send_message 'Bye guys!'
         
     | 
| 
      
 65 
     | 
    
         
            +
              end
         
     | 
| 
      
 66 
     | 
    
         
            +
            end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            client.run!
         
     | 
| 
      
 69 
     | 
    
         
            +
            ```
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
            List of events: ``:message, :slow_mode, :r9k_mode, :subscribers_mode, :slow_mode_off, :r9k_off, :subscribers_mode_off, :subscribe, :connect, :disconnect, not_supported, raw``.
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
            ``raw`` event is triggered for every twitch irc message. ``not_supported`` event is triggered for not supported twitch irc messages.
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
            if local variable access is needed, the first block variable is the client:
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
            Twitch::Chat::Client.new(channel: 'channel', nickname: 'nickname', password: 'twitch_oath_token') do |client|
         
     | 
| 
      
 80 
     | 
    
         
            +
              # client is the client instance
         
     | 
| 
      
 81 
     | 
    
         
            +
            end
         
     | 
| 
      
 82 
     | 
    
         
            +
            ```
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
            By default, logging is done to the ``STDOUT``, but you can change it by passing log file path as ``:output`` parameter in initializer
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 87 
     | 
    
         
            +
            Twitch::Chat::Client.new(channel: 'channel', nickname: 'nickname', password: 'twitch_oath_token', output: 'file.log')
         
     | 
| 
      
 88 
     | 
    
         
            +
            ```
         
     | 
| 
      
 89 
     | 
    
         
            +
            ## Contributing
         
     | 
| 
      
 90 
     | 
    
         
            +
             
     | 
| 
      
 91 
     | 
    
         
            +
            1. Fork it ( https://github.com/enotpoloskun/twitch-chat/fork )
         
     | 
| 
      
 92 
     | 
    
         
            +
            2. Create your feature branch (`git checkout -b my-new-feature`)
         
     | 
| 
      
 93 
     | 
    
         
            +
            3. Commit your changes (`git commit -am 'Add some feature'`)
         
     | 
| 
      
 94 
     | 
    
         
            +
            4. Push to the branch (`git push origin my-new-feature`)
         
     | 
| 
      
 95 
     | 
    
         
            +
            5. Create a new Pull Request
         
     | 
    
        data/Rakefile
    ADDED
    
    
    
        data/lib/twitch/chat.rb
    ADDED
    
    | 
         @@ -0,0 +1,13 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'eventmachine'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'logger'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'active_support/core_ext/hash/keys'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "twitch/chat/version"
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'twitch/chat/client'
         
     | 
| 
      
 6 
     | 
    
         
            +
            require 'twitch/chat/connection'
         
     | 
| 
      
 7 
     | 
    
         
            +
            require 'twitch/chat/message'
         
     | 
| 
      
 8 
     | 
    
         
            +
            require 'twitch/chat/channel'
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            module Twitch
         
     | 
| 
      
 11 
     | 
    
         
            +
              module Chat
         
     | 
| 
      
 12 
     | 
    
         
            +
              end
         
     | 
| 
      
 13 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,20 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Twitch
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Chat
         
     | 
| 
      
 3 
     | 
    
         
            +
                class Channel
         
     | 
| 
      
 4 
     | 
    
         
            +
                  attr_reader :name, :moderators
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                  def initialize(name)
         
     | 
| 
      
 7 
     | 
    
         
            +
                    @name = name
         
     | 
| 
      
 8 
     | 
    
         
            +
                    @moderators = []
         
     | 
| 
      
 9 
     | 
    
         
            +
                  end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                  def add_moderator(moderator)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    @moderators << moderator
         
     | 
| 
      
 13 
     | 
    
         
            +
                  end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  def remove_moderator(moderator)
         
     | 
| 
      
 16 
     | 
    
         
            +
                    @moderators.delete(moderator)
         
     | 
| 
      
 17 
     | 
    
         
            +
                  end
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,183 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Twitch
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Chat
         
     | 
| 
      
 3 
     | 
    
         
            +
                class Client
         
     | 
| 
      
 4 
     | 
    
         
            +
                  MODERATOR_MESSAGES_COUNT = 100
         
     | 
| 
      
 5 
     | 
    
         
            +
                  USER_MESSAGES_COUNT = 20
         
     | 
| 
      
 6 
     | 
    
         
            +
                  TWITCH_PERIOD = 30.0
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                  attr_accessor :host, :port, :nickname, :password, :connection
         
     | 
| 
      
 9 
     | 
    
         
            +
                  attr_reader :channel, :callbacks
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                  def initialize(options = {}, &blk)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    options.symbolize_keys!
         
     | 
| 
      
 13 
     | 
    
         
            +
                    options = {
         
     | 
| 
      
 14 
     | 
    
         
            +
                      host: 'irc.twitch.tv',
         
     | 
| 
      
 15 
     | 
    
         
            +
                      port: '6667',
         
     | 
| 
      
 16 
     | 
    
         
            +
                      output: STDOUT
         
     | 
| 
      
 17 
     | 
    
         
            +
                    }.merge!(options)
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                    @logger = Logger.new(options[:output]) if options[:output]
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                    @host = options[:host]
         
     | 
| 
      
 22 
     | 
    
         
            +
                    @port = options[:port]
         
     | 
| 
      
 23 
     | 
    
         
            +
                    @nickname = options[:nickname]
         
     | 
| 
      
 24 
     | 
    
         
            +
                    @password = options[:password]
         
     | 
| 
      
 25 
     | 
    
         
            +
                    @channel = Channel.new(options[:channel]) if options[:channel]
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                    @messages_queue = []
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                    @connected = false
         
     | 
| 
      
 30 
     | 
    
         
            +
                    @callbacks = {}
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                    check_attributes!
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                    if block_given?
         
     | 
| 
      
 35 
     | 
    
         
            +
                      if blk.arity == 1
         
     | 
| 
      
 36 
     | 
    
         
            +
                        yield self
         
     | 
| 
      
 37 
     | 
    
         
            +
                      else
         
     | 
| 
      
 38 
     | 
    
         
            +
                        instance_eval(&blk)
         
     | 
| 
      
 39 
     | 
    
         
            +
                      end
         
     | 
| 
      
 40 
     | 
    
         
            +
                    end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                    self.on(:new_moderator) do |user|
         
     | 
| 
      
 43 
     | 
    
         
            +
                      @channel.add_moderator(user)
         
     | 
| 
      
 44 
     | 
    
         
            +
                    end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                    self.on(:remove_moderator) do |user|
         
     | 
| 
      
 47 
     | 
    
         
            +
                      @channel.remove_moderator(user)
         
     | 
| 
      
 48 
     | 
    
         
            +
                    end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                    self.on(:ping) do
         
     | 
| 
      
 51 
     | 
    
         
            +
                      send_data("PONG :tmi.twitch.tv")
         
     | 
| 
      
 52 
     | 
    
         
            +
                    end
         
     | 
| 
      
 53 
     | 
    
         
            +
                  end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                  def connect
         
     | 
| 
      
 56 
     | 
    
         
            +
                    @connection ||= EventMachine::connect(@host, @port, Connection, self)
         
     | 
| 
      
 57 
     | 
    
         
            +
                  end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                  def connected?
         
     | 
| 
      
 60 
     | 
    
         
            +
                    @connected
         
     | 
| 
      
 61 
     | 
    
         
            +
                  end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                  def on(callback, &blk)
         
     | 
| 
      
 64 
     | 
    
         
            +
                    (@callbacks[callback.to_sym] ||= []) << blk
         
     | 
| 
      
 65 
     | 
    
         
            +
                  end
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
                  def trigger(event_name, *args)
         
     | 
| 
      
 68 
     | 
    
         
            +
                    (@callbacks[event_name.to_sym] || []).each { |blk| blk.call(*args) }
         
     | 
| 
      
 69 
     | 
    
         
            +
                  end
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                  def run!
         
     | 
| 
      
 72 
     | 
    
         
            +
                    EM.epoll
         
     | 
| 
      
 73 
     | 
    
         
            +
                    EventMachine.run do
         
     | 
| 
      
 74 
     | 
    
         
            +
                      trap("TERM") { EM::stop }
         
     | 
| 
      
 75 
     | 
    
         
            +
                      trap("INT")  { EM::stop }
         
     | 
| 
      
 76 
     | 
    
         
            +
                      handle_message_queue
         
     | 
| 
      
 77 
     | 
    
         
            +
                      connect
         
     | 
| 
      
 78 
     | 
    
         
            +
                    end
         
     | 
| 
      
 79 
     | 
    
         
            +
                  end
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
                  def join(channel)
         
     | 
| 
      
 82 
     | 
    
         
            +
                    @channel = Channel.new(channel)
         
     | 
| 
      
 83 
     | 
    
         
            +
                    send_data "JOIN ##{@channel.name}"
         
     | 
| 
      
 84 
     | 
    
         
            +
                  end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                  def part
         
     | 
| 
      
 87 
     | 
    
         
            +
                    send_data "PART ##{@channel.name}"
         
     | 
| 
      
 88 
     | 
    
         
            +
                    @channel = nil
         
     | 
| 
      
 89 
     | 
    
         
            +
                    @messages_queue = []
         
     | 
| 
      
 90 
     | 
    
         
            +
                  end
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
                  def send_message(message)
         
     | 
| 
      
 93 
     | 
    
         
            +
                    @messages_queue << message if @messages_queue.last != message
         
     | 
| 
      
 94 
     | 
    
         
            +
                  end
         
     | 
| 
      
 95 
     | 
    
         
            +
             
     | 
| 
      
 96 
     | 
    
         
            +
                  def ready
         
     | 
| 
      
 97 
     | 
    
         
            +
                    @connected = true
         
     | 
| 
      
 98 
     | 
    
         
            +
                    authenticate
         
     | 
| 
      
 99 
     | 
    
         
            +
                    join(@channel.name) if @channel
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                    trigger(:connected)
         
     | 
| 
      
 102 
     | 
    
         
            +
                  end
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
                  def max_messages_count
         
     | 
| 
      
 105 
     | 
    
         
            +
                    @channel.moderators.include?(@nickname) ? MODERATOR_MESSAGES_COUNT : USER_MESSAGES_COUNT
         
     | 
| 
      
 106 
     | 
    
         
            +
                  end
         
     | 
| 
      
 107 
     | 
    
         
            +
             
     | 
| 
      
 108 
     | 
    
         
            +
                  def message_delay
         
     | 
| 
      
 109 
     | 
    
         
            +
                    TWITCH_PERIOD / max_messages_count
         
     | 
| 
      
 110 
     | 
    
         
            +
                  end
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
                private
         
     | 
| 
      
 113 
     | 
    
         
            +
             
     | 
| 
      
 114 
     | 
    
         
            +
                  def handle_message_queue
         
     | 
| 
      
 115 
     | 
    
         
            +
                    EM.add_timer(message_delay) do
         
     | 
| 
      
 116 
     | 
    
         
            +
                      if message = @messages_queue.pop
         
     | 
| 
      
 117 
     | 
    
         
            +
                        send_data "PRIVMSG ##{@channel.name} :#{message}"
         
     | 
| 
      
 118 
     | 
    
         
            +
                        @logger.debug("Sent message: PRIVMSG ##{@channel.name} :#{message}")
         
     | 
| 
      
 119 
     | 
    
         
            +
                      end
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
                      handle_message_queue
         
     | 
| 
      
 122 
     | 
    
         
            +
                    end
         
     | 
| 
      
 123 
     | 
    
         
            +
                  end
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                  def unbind(arg = nil)
         
     | 
| 
      
 126 
     | 
    
         
            +
                    part if @channel
         
     | 
| 
      
 127 
     | 
    
         
            +
                    trigger(:disconnect)
         
     | 
| 
      
 128 
     | 
    
         
            +
                  end
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
      
 130 
     | 
    
         
            +
                  def receive_data(data)
         
     | 
| 
      
 131 
     | 
    
         
            +
                    data.split(/\r?\n/).each do |message|
         
     | 
| 
      
 132 
     | 
    
         
            +
                      @logger.debug(message)
         
     | 
| 
      
 133 
     | 
    
         
            +
             
     | 
| 
      
 134 
     | 
    
         
            +
                      Message.new(message).tap do |message|
         
     | 
| 
      
 135 
     | 
    
         
            +
                        trigger(:raw, message)
         
     | 
| 
      
 136 
     | 
    
         
            +
             
     | 
| 
      
 137 
     | 
    
         
            +
                        case message.type
         
     | 
| 
      
 138 
     | 
    
         
            +
                          when :ping
         
     | 
| 
      
 139 
     | 
    
         
            +
                            trigger(:ping)
         
     | 
| 
      
 140 
     | 
    
         
            +
                          when :message
         
     | 
| 
      
 141 
     | 
    
         
            +
                            trigger(:message, message.user, message.message) if message.target == @channel.name
         
     | 
| 
      
 142 
     | 
    
         
            +
                          when :mode
         
     | 
| 
      
 143 
     | 
    
         
            +
                            trigger(:mode, *message.params.last(2))
         
     | 
| 
      
 144 
     | 
    
         
            +
             
     | 
| 
      
 145 
     | 
    
         
            +
                            if message.params[1] == '+o'
         
     | 
| 
      
 146 
     | 
    
         
            +
                              trigger(:new_moderator, message.params.last)
         
     | 
| 
      
 147 
     | 
    
         
            +
                            elsif message.params[1] == '-o'
         
     | 
| 
      
 148 
     | 
    
         
            +
                              trigger(:remove_moderator, message.params.last)
         
     | 
| 
      
 149 
     | 
    
         
            +
                            end
         
     | 
| 
      
 150 
     | 
    
         
            +
                          when :slow_mode, :r9k_mode, :subscribers_mode, :slow_mode_off, :r9k_mode_off, :subscribers_mode_off
         
     | 
| 
      
 151 
     | 
    
         
            +
                            trigger(message.type)
         
     | 
| 
      
 152 
     | 
    
         
            +
                          when :subscribe
         
     | 
| 
      
 153 
     | 
    
         
            +
                            trigger(:subscribe, message.params.last.split(' ').first)
         
     | 
| 
      
 154 
     | 
    
         
            +
                          when :not_supported
         
     | 
| 
      
 155 
     | 
    
         
            +
                            trigger(:not_supported, *message.params)
         
     | 
| 
      
 156 
     | 
    
         
            +
                        end
         
     | 
| 
      
 157 
     | 
    
         
            +
                      end
         
     | 
| 
      
 158 
     | 
    
         
            +
                    end
         
     | 
| 
      
 159 
     | 
    
         
            +
                  end
         
     | 
| 
      
 160 
     | 
    
         
            +
             
     | 
| 
      
 161 
     | 
    
         
            +
                  def send_data(message)
         
     | 
| 
      
 162 
     | 
    
         
            +
                    return false unless connected?
         
     | 
| 
      
 163 
     | 
    
         
            +
             
     | 
| 
      
 164 
     | 
    
         
            +
                    message = message + "\n"
         
     | 
| 
      
 165 
     | 
    
         
            +
                    connection.send_data(message)
         
     | 
| 
      
 166 
     | 
    
         
            +
                  end
         
     | 
| 
      
 167 
     | 
    
         
            +
             
     | 
| 
      
 168 
     | 
    
         
            +
                  def check_attributes!
         
     | 
| 
      
 169 
     | 
    
         
            +
                    [:host, :port, :nickname, :password].each do |attribute|
         
     | 
| 
      
 170 
     | 
    
         
            +
                      raise ArgumentError.new("#{attribute.capitalize} is not defined") if send(attribute).nil?
         
     | 
| 
      
 171 
     | 
    
         
            +
                    end
         
     | 
| 
      
 172 
     | 
    
         
            +
             
     | 
| 
      
 173 
     | 
    
         
            +
                    nil
         
     | 
| 
      
 174 
     | 
    
         
            +
                  end
         
     | 
| 
      
 175 
     | 
    
         
            +
             
     | 
| 
      
 176 
     | 
    
         
            +
                  def authenticate
         
     | 
| 
      
 177 
     | 
    
         
            +
                    send_data "PASS #{password}"
         
     | 
| 
      
 178 
     | 
    
         
            +
                    send_data "NICK #{nickname}"
         
     | 
| 
      
 179 
     | 
    
         
            +
                    send_data "TWITCHCLIENT 3"
         
     | 
| 
      
 180 
     | 
    
         
            +
                  end
         
     | 
| 
      
 181 
     | 
    
         
            +
                end
         
     | 
| 
      
 182 
     | 
    
         
            +
              end
         
     | 
| 
      
 183 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,23 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Twitch
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Chat
         
     | 
| 
      
 3 
     | 
    
         
            +
                class Connection < EventMachine::Connection
         
     | 
| 
      
 4 
     | 
    
         
            +
                  extend Forwardable
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                  def_delegators :@client, :receive_data, :unbind
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                  def initialize(client)
         
     | 
| 
      
 9 
     | 
    
         
            +
                    raise ArgumentError.new("client argument is required TC::Connection") unless client
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                    @client = client
         
     | 
| 
      
 12 
     | 
    
         
            +
                  end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                  def post_init
         
     | 
| 
      
 15 
     | 
    
         
            +
                    @client.connection = self
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                  def connection_completed
         
     | 
| 
      
 19 
     | 
    
         
            +
                    @client.ready
         
     | 
| 
      
 20 
     | 
    
         
            +
                  end
         
     | 
| 
      
 21 
     | 
    
         
            +
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,107 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Twitch
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Chat
         
     | 
| 
      
 3 
     | 
    
         
            +
                class Message
         
     | 
| 
      
 4 
     | 
    
         
            +
                  attr_reader :type, :message, :user, :params, :command, :raw, :prefix, :error, :channel, :target
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                  def initialize(msg)
         
     | 
| 
      
 7 
     | 
    
         
            +
                    @raw = msg
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                    @prefix, @command, raw_params = msg.match(/(^:(\S+) )?(\S+)(.*)/).captures.last(3)
         
     | 
| 
      
 10 
     | 
    
         
            +
                    @params = parse_params(raw_params)
         
     | 
| 
      
 11 
     | 
    
         
            +
                    @user = parse_user
         
     | 
| 
      
 12 
     | 
    
         
            +
                    @channel = parse_channel
         
     | 
| 
      
 13 
     | 
    
         
            +
                    @target  = @channel || @user
         
     | 
| 
      
 14 
     | 
    
         
            +
                    @error = parse_error
         
     | 
| 
      
 15 
     | 
    
         
            +
                    @message = parse_message
         
     | 
| 
      
 16 
     | 
    
         
            +
                    @type = parse_type
         
     | 
| 
      
 17 
     | 
    
         
            +
                  end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                  def error?
         
     | 
| 
      
 20 
     | 
    
         
            +
                    !@error.nil?
         
     | 
| 
      
 21 
     | 
    
         
            +
                  end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                  def numeric_reply?
         
     | 
| 
      
 24 
     | 
    
         
            +
                    !!@command.match(/^\d{3}$/)
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                private
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                  def parse_params(raw_params)
         
     | 
| 
      
 30 
     | 
    
         
            +
                    raw_params = raw_params.strip
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                    params     = []
         
     | 
| 
      
 33 
     | 
    
         
            +
                    if match = raw_params.match(/(?:^:| :)(.*)$/)
         
     | 
| 
      
 34 
     | 
    
         
            +
                      params = match.pre_match.split(" ")
         
     | 
| 
      
 35 
     | 
    
         
            +
                      params << match[1]
         
     | 
| 
      
 36 
     | 
    
         
            +
                    else
         
     | 
| 
      
 37 
     | 
    
         
            +
                      params = raw_params.split(" ")
         
     | 
| 
      
 38 
     | 
    
         
            +
                    end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                    params
         
     | 
| 
      
 41 
     | 
    
         
            +
                  end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                  def parse_user
         
     | 
| 
      
 44 
     | 
    
         
            +
                    return unless @prefix
         
     | 
| 
      
 45 
     | 
    
         
            +
                    nick = @prefix[/^(\S+)!/, 1]
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                    return nil if nick.nil?
         
     | 
| 
      
 48 
     | 
    
         
            +
                    nick
         
     | 
| 
      
 49 
     | 
    
         
            +
                  end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                  def parse_channel
         
     | 
| 
      
 52 
     | 
    
         
            +
                    if @params.first.to_s.start_with?('#')
         
     | 
| 
      
 53 
     | 
    
         
            +
                      @params.first.gsub('#', '')
         
     | 
| 
      
 54 
     | 
    
         
            +
                    end
         
     | 
| 
      
 55 
     | 
    
         
            +
                  end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                  def parse_error
         
     | 
| 
      
 58 
     | 
    
         
            +
                    @command.to_i if numeric_reply? && @command[/[45]\d\d/]
         
     | 
| 
      
 59 
     | 
    
         
            +
                  end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                  def parse_message
         
     | 
| 
      
 62 
     | 
    
         
            +
                    if error?
         
     | 
| 
      
 63 
     | 
    
         
            +
                      @error.to_s
         
     | 
| 
      
 64 
     | 
    
         
            +
                    elsif regular_command?
         
     | 
| 
      
 65 
     | 
    
         
            +
                      @params.last
         
     | 
| 
      
 66 
     | 
    
         
            +
                    end
         
     | 
| 
      
 67 
     | 
    
         
            +
                  end
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                  def numeric_reply?
         
     | 
| 
      
 70 
     | 
    
         
            +
                    !!@command.match(/^\d{3}$/)
         
     | 
| 
      
 71 
     | 
    
         
            +
                  end
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                  def regular_command?
         
     | 
| 
      
 74 
     | 
    
         
            +
                    !numeric_reply?
         
     | 
| 
      
 75 
     | 
    
         
            +
                  end
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
                  def parse_type
         
     | 
| 
      
 78 
     | 
    
         
            +
                    case @command
         
     | 
| 
      
 79 
     | 
    
         
            +
                      when 'PRIVMSG'
         
     | 
| 
      
 80 
     | 
    
         
            +
                        if @user == 'jtv'
         
     | 
| 
      
 81 
     | 
    
         
            +
                          case @message
         
     | 
| 
      
 82 
     | 
    
         
            +
                            when /This room is now in slow mode/ then :slow_mode
         
     | 
| 
      
 83 
     | 
    
         
            +
                            when /This room is now in subscribers-only mode/ then :subscribers_mode
         
     | 
| 
      
 84 
     | 
    
         
            +
                            when /This room is now in r9k mode/ then :r9k_mode
         
     | 
| 
      
 85 
     | 
    
         
            +
                            when /This room is no longer in slow mode/ then :slow_mode_off
         
     | 
| 
      
 86 
     | 
    
         
            +
                            when /This room is no longer in r9k mode/ then :r9k_mode_off
         
     | 
| 
      
 87 
     | 
    
         
            +
                            when /This room is no longer in subscribers-only mode/ then :subscribers_mode_off
         
     | 
| 
      
 88 
     | 
    
         
            +
                          end
         
     | 
| 
      
 89 
     | 
    
         
            +
                        elsif @user == 'twitchnotify'
         
     | 
| 
      
 90 
     | 
    
         
            +
                          if message =~ /just subscribed!/
         
     | 
| 
      
 91 
     | 
    
         
            +
                            :subscribe
         
     | 
| 
      
 92 
     | 
    
         
            +
                          end
         
     | 
| 
      
 93 
     | 
    
         
            +
                        else
         
     | 
| 
      
 94 
     | 
    
         
            +
                          :message
         
     | 
| 
      
 95 
     | 
    
         
            +
                        end
         
     | 
| 
      
 96 
     | 
    
         
            +
                      when 'MODE' then :mode
         
     | 
| 
      
 97 
     | 
    
         
            +
                      when 'PING' then :ping
         
     | 
| 
      
 98 
     | 
    
         
            +
                      when 'NOTICE'
         
     | 
| 
      
 99 
     | 
    
         
            +
                        if @params.last == 'Login unsuccessful'
         
     | 
| 
      
 100 
     | 
    
         
            +
                          :login_unsuccessful
         
     | 
| 
      
 101 
     | 
    
         
            +
                        end
         
     | 
| 
      
 102 
     | 
    
         
            +
                      else :not_supported
         
     | 
| 
      
 103 
     | 
    
         
            +
                    end
         
     | 
| 
      
 104 
     | 
    
         
            +
                  end
         
     | 
| 
      
 105 
     | 
    
         
            +
                end
         
     | 
| 
      
 106 
     | 
    
         
            +
              end
         
     | 
| 
      
 107 
     | 
    
         
            +
            end
         
     | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | 
         @@ -0,0 +1,78 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            $LOAD_PATH.unshift ::File.realpath "../lib", __dir__
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'twitch/chat'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'rspec/expectations'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            RSpec.configure do |config|
         
     | 
| 
      
 7 
     | 
    
         
            +
              # rspec-expectations config goes here. You can use an alternate
         
     | 
| 
      
 8 
     | 
    
         
            +
              # assertion/expectation library such as wrong or the stdlib/minitest
         
     | 
| 
      
 9 
     | 
    
         
            +
              # assertions if you prefer.
         
     | 
| 
      
 10 
     | 
    
         
            +
              config.expect_with :rspec do |expectations|
         
     | 
| 
      
 11 
     | 
    
         
            +
                # This option will default to `true` in RSpec 4. It makes the `description`
         
     | 
| 
      
 12 
     | 
    
         
            +
                # and `failure_message` of custom matchers include text for helper methods
         
     | 
| 
      
 13 
     | 
    
         
            +
                # defined using `chain`, e.g.:
         
     | 
| 
      
 14 
     | 
    
         
            +
                # be_bigger_than(2).and_smaller_than(4).description
         
     | 
| 
      
 15 
     | 
    
         
            +
                #   # => "be bigger than 2 and smaller than 4"
         
     | 
| 
      
 16 
     | 
    
         
            +
                # ...rather than:
         
     | 
| 
      
 17 
     | 
    
         
            +
                #   # => "be bigger than 2"
         
     | 
| 
      
 18 
     | 
    
         
            +
                expectations.include_chain_clauses_in_custom_matcher_descriptions = true
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              # rspec-mocks config goes here. You can use an alternate test double
         
     | 
| 
      
 22 
     | 
    
         
            +
              # library (such as bogus or mocha) by changing the `mock_with` option here.
         
     | 
| 
      
 23 
     | 
    
         
            +
              config.mock_with :rspec do |mocks|
         
     | 
| 
      
 24 
     | 
    
         
            +
                # Prevents you from mocking or stubbing a method that does not exist on
         
     | 
| 
      
 25 
     | 
    
         
            +
                # a real object. This is generally recommended, and will default to
         
     | 
| 
      
 26 
     | 
    
         
            +
                # `true` in RSpec 4.
         
     | 
| 
      
 27 
     | 
    
         
            +
                mocks.verify_partial_doubles = true
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
            # The settings below are suggested to provide a good initial experience
         
     | 
| 
      
 31 
     | 
    
         
            +
            # with RSpec, but feel free to customize to your heart's content.
         
     | 
| 
      
 32 
     | 
    
         
            +
            =begin
         
     | 
| 
      
 33 
     | 
    
         
            +
              # These two settings work together to allow you to limit a spec run
         
     | 
| 
      
 34 
     | 
    
         
            +
              # to individual examples or groups you care about by tagging them with
         
     | 
| 
      
 35 
     | 
    
         
            +
              # `:focus` metadata. When nothing is tagged with `:focus`, all examples
         
     | 
| 
      
 36 
     | 
    
         
            +
              # get run.
         
     | 
| 
      
 37 
     | 
    
         
            +
              config.filter_run :focus
         
     | 
| 
      
 38 
     | 
    
         
            +
              config.run_all_when_everything_filtered = true
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
              # Limits the available syntax to the non-monkey patched syntax that is recommended.
         
     | 
| 
      
 41 
     | 
    
         
            +
              # For more details, see:
         
     | 
| 
      
 42 
     | 
    
         
            +
              #   - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
         
     | 
| 
      
 43 
     | 
    
         
            +
              #   - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
         
     | 
| 
      
 44 
     | 
    
         
            +
              #   - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
         
     | 
| 
      
 45 
     | 
    
         
            +
              config.disable_monkey_patching!
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
              # This setting enables warnings. It's recommended, but in some cases may
         
     | 
| 
      
 48 
     | 
    
         
            +
              # be too noisy due to issues in dependencies.
         
     | 
| 
      
 49 
     | 
    
         
            +
              config.warnings = true
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
              # Many RSpec users commonly either run the entire suite or an individual
         
     | 
| 
      
 52 
     | 
    
         
            +
              # file, and it's useful to allow more verbose output when running an
         
     | 
| 
      
 53 
     | 
    
         
            +
              # individual spec file.
         
     | 
| 
      
 54 
     | 
    
         
            +
              if config.files_to_run.one?
         
     | 
| 
      
 55 
     | 
    
         
            +
                # Use the documentation formatter for detailed output,
         
     | 
| 
      
 56 
     | 
    
         
            +
                # unless a formatter has already been configured
         
     | 
| 
      
 57 
     | 
    
         
            +
                # (e.g. via a command-line flag).
         
     | 
| 
      
 58 
     | 
    
         
            +
                config.default_formatter = 'doc'
         
     | 
| 
      
 59 
     | 
    
         
            +
              end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
              # Print the 10 slowest examples and example groups at the
         
     | 
| 
      
 62 
     | 
    
         
            +
              # end of the spec run, to help surface which specs are running
         
     | 
| 
      
 63 
     | 
    
         
            +
              # particularly slow.
         
     | 
| 
      
 64 
     | 
    
         
            +
              config.profile_examples = 10
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
              # Run specs in random order to surface order dependencies. If you find an
         
     | 
| 
      
 67 
     | 
    
         
            +
              # order dependency and want to debug it, you can fix the order by providing
         
     | 
| 
      
 68 
     | 
    
         
            +
              # the seed, which is printed after each run.
         
     | 
| 
      
 69 
     | 
    
         
            +
              #     --seed 1234
         
     | 
| 
      
 70 
     | 
    
         
            +
              config.order = :random
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
              # Seed global randomization in this process using the `--seed` CLI option.
         
     | 
| 
      
 73 
     | 
    
         
            +
              # Setting this allows you to use `--seed` to deterministically reproduce
         
     | 
| 
      
 74 
     | 
    
         
            +
              # test failures related to randomization by passing the same `--seed` value
         
     | 
| 
      
 75 
     | 
    
         
            +
              # as the one that triggered the failure.
         
     | 
| 
      
 76 
     | 
    
         
            +
              Kernel.srand config.seed
         
     | 
| 
      
 77 
     | 
    
         
            +
            =end
         
     | 
| 
      
 78 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,27 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Twitch::Chat::Client do
         
     | 
| 
      
 4 
     | 
    
         
            +
              let(:client) { Twitch::Chat::Client.new(password: 'password', nickname: 'enotpoloskun') }
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
              describe '#on' do
         
     | 
| 
      
 7 
     | 
    
         
            +
                context 'There is one message callback' do
         
     | 
| 
      
 8 
     | 
    
         
            +
                  before { client.on(:message) { 'callback1' } }
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                  it { client.callbacks[:message].count.should eq 1 }
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                  context "There is another message callback" do
         
     | 
| 
      
 13 
     | 
    
         
            +
                    before { client.on(:message) { 'callback2' } }
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                    it { client.callbacks[:message].count.should eq 2 }
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
              describe '#trigger' do
         
     | 
| 
      
 21 
     | 
    
         
            +
                before { client.on(:message) { client.inspect } }
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                it { client.should_receive(:inspect) }
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                after { client.trigger(:message) }
         
     | 
| 
      
 26 
     | 
    
         
            +
              end
         
     | 
| 
      
 27 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,66 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Twitch::Chat::Message do
         
     | 
| 
      
 4 
     | 
    
         
            +
              context :type do
         
     | 
| 
      
 5 
     | 
    
         
            +
                context 'PRIVMSG' do
         
     | 
| 
      
 6 
     | 
    
         
            +
                  context :message do
         
     | 
| 
      
 7 
     | 
    
         
            +
                    let(:message) { Twitch::Chat::Message.new(":enotpoloskun!enotpoloskun@enotpoloskun.tmi.twitch.tv PRIVMSG #enotpoloskun :BibleThump") }
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                    it { message.type.should eq :message }
         
     | 
| 
      
 10 
     | 
    
         
            +
                    it { message.message.should eq 'BibleThump' }
         
     | 
| 
      
 11 
     | 
    
         
            +
                    it { message.user.should eq 'enotpoloskun' }
         
     | 
| 
      
 12 
     | 
    
         
            +
                  end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                  context :slow_mode do
         
     | 
| 
      
 15 
     | 
    
         
            +
                    let(:message) { Twitch::Chat::Message.new(":jtv!jtv@jtv.tmi.twitch.tv PRIVMSG #enotpoloskun :This room is now in slow mode. You may send messages every 123 seconds") }
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                    it { message.type.should eq :slow_mode }
         
     | 
| 
      
 18 
     | 
    
         
            +
                    it { message.user.should eq 'jtv' }
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                  context :r9k_mode do
         
     | 
| 
      
 22 
     | 
    
         
            +
                    let(:message) { Twitch::Chat::Message.new(":jtv!jtv@jtv.tmi.twitch.tv PRIVMSG #enotpoloskun :This room is now in r9k mode.") }
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                    it { message.type.should eq :r9k_mode }
         
     | 
| 
      
 25 
     | 
    
         
            +
                    it { message.user.should eq 'jtv' }
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  context :r9k_mode do
         
     | 
| 
      
 29 
     | 
    
         
            +
                    let(:message) { Twitch::Chat::Message.new(":jtv!jtv@jtv.tmi.twitch.tv PRIVMSG #enotpoloskun :This room is now in subscribers-only mode.") }
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                    it { message.type.should eq :subscribers_mode }
         
     | 
| 
      
 32 
     | 
    
         
            +
                    it { message.user.should eq 'jtv' }
         
     | 
| 
      
 33 
     | 
    
         
            +
                  end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                  context :subscribe do
         
     | 
| 
      
 36 
     | 
    
         
            +
                    let(:message) { Twitch::Chat::Message.new(":twitchnotify!twitchnotify@twitchnotify.tmi.twitch.tv PRIVMSG #enotpoloskun :enotpoloskun just subscribed!") }
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                    it { message.type.should eq :subscribe }
         
     | 
| 
      
 39 
     | 
    
         
            +
                    it { message.user.should eq 'twitchnotify' }
         
     | 
| 
      
 40 
     | 
    
         
            +
                  end
         
     | 
| 
      
 41 
     | 
    
         
            +
                end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                context 'MODE' do
         
     | 
| 
      
 44 
     | 
    
         
            +
                  let(:message) { Twitch::Chat::Message.new(":jtv MODE #enotpoloskun +o enotpoloskun") }
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                  it { message.user.should eq nil }
         
     | 
| 
      
 47 
     | 
    
         
            +
                  it { message.type.should eq :mode }
         
     | 
| 
      
 48 
     | 
    
         
            +
                end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                context 'PING' do
         
     | 
| 
      
 51 
     | 
    
         
            +
                  let(:message) { Twitch::Chat::Message.new("PING :tmi.twitch.tv") }
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                  it { message.user.should eq nil }
         
     | 
| 
      
 54 
     | 
    
         
            +
                  it { message.type.should eq :ping }
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                context 'NOTIFY' do
         
     | 
| 
      
 58 
     | 
    
         
            +
                  context :login_unsuccessful do
         
     | 
| 
      
 59 
     | 
    
         
            +
                    let(:message) { Twitch::Chat::Message.new(":tmi.twitch.tv NOTICE * :Login unsuccessful") }
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                    it { message.user.should eq nil }
         
     | 
| 
      
 62 
     | 
    
         
            +
                    it { message.type.should eq :login_unsuccessful }
         
     | 
| 
      
 63 
     | 
    
         
            +
                  end
         
     | 
| 
      
 64 
     | 
    
         
            +
                end
         
     | 
| 
      
 65 
     | 
    
         
            +
              end
         
     | 
| 
      
 66 
     | 
    
         
            +
            end
         
     | 
    
        data/twitch-chat.gemspec
    ADDED
    
    | 
         @@ -0,0 +1,26 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # coding: utf-8
         
     | 
| 
      
 2 
     | 
    
         
            +
            lib = File.expand_path('../lib', __FILE__)
         
     | 
| 
      
 3 
     | 
    
         
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'twitch/chat/version'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            Gem::Specification.new do |spec|
         
     | 
| 
      
 7 
     | 
    
         
            +
              spec.name          = "twitch-chat"
         
     | 
| 
      
 8 
     | 
    
         
            +
              spec.version       = Twitch::Chat::VERSION
         
     | 
| 
      
 9 
     | 
    
         
            +
              spec.authors       = ["Pavel Astraukh"]
         
     | 
| 
      
 10 
     | 
    
         
            +
              spec.email         = ["paladin111333@gmail.com"]
         
     | 
| 
      
 11 
     | 
    
         
            +
              spec.summary       = %q{twitch-chat is a Twitch chat client that uses Twitch IRC. Can be used as twitch chat bot engine.}
         
     | 
| 
      
 12 
     | 
    
         
            +
              spec.description   = %q{twitch-chat library is a Twitch chat client that uses Twitch IRC.
         
     | 
| 
      
 13 
     | 
    
         
            +
                                    EventMachine is used to handle connections to servers.
         
     | 
| 
      
 14 
     | 
    
         
            +
                                    With the help of this library you can connect to any Twitch's channel and handle various chat events.
         
     | 
| 
      
 15 
     | 
    
         
            +
                                    Can be used as twitch chat bot engine.}
         
     | 
| 
      
 16 
     | 
    
         
            +
              spec.homepage      = "https://github.com/EnotPoloskun/twitch-chat"
         
     | 
| 
      
 17 
     | 
    
         
            +
              spec.license       = "MIT"
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
              spec.files         = `git ls-files -z`.split("\x0")
         
     | 
| 
      
 20 
     | 
    
         
            +
              spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
         
     | 
| 
      
 21 
     | 
    
         
            +
              spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
         
     | 
| 
      
 22 
     | 
    
         
            +
              spec.require_paths = ["lib"]
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
              spec.add_development_dependency "bundler", "~> 1.7"
         
     | 
| 
      
 25 
     | 
    
         
            +
              spec.add_development_dependency "rake", "~> 10.0"
         
     | 
| 
      
 26 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,96 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: twitch-chat
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 5 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 6 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 7 
     | 
    
         
            +
            - Pavel Astraukh
         
     | 
| 
      
 8 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 9 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 10 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2014-12-07 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.7'
         
     | 
| 
      
 20 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 21 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 22 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 23 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 24 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 25 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 26 
     | 
    
         
            +
                    version: '1.7'
         
     | 
| 
      
 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 
     | 
    
         
            +
            description: |-
         
     | 
| 
      
 42 
     | 
    
         
            +
              twitch-chat library is a Twitch chat client that uses Twitch IRC.
         
     | 
| 
      
 43 
     | 
    
         
            +
                                      EventMachine is used to handle connections to servers.
         
     | 
| 
      
 44 
     | 
    
         
            +
                                      With the help of this library you can connect to any Twitch's channel and handle various chat events.
         
     | 
| 
      
 45 
     | 
    
         
            +
                                      Can be used as twitch chat bot engine.
         
     | 
| 
      
 46 
     | 
    
         
            +
            email:
         
     | 
| 
      
 47 
     | 
    
         
            +
            - paladin111333@gmail.com
         
     | 
| 
      
 48 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 49 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 50 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 51 
     | 
    
         
            +
            files:
         
     | 
| 
      
 52 
     | 
    
         
            +
            - ".gitignore"
         
     | 
| 
      
 53 
     | 
    
         
            +
            - ".rspec"
         
     | 
| 
      
 54 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 55 
     | 
    
         
            +
            - LICENSE.txt
         
     | 
| 
      
 56 
     | 
    
         
            +
            - README.md
         
     | 
| 
      
 57 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 58 
     | 
    
         
            +
            - lib/twitch/chat.rb
         
     | 
| 
      
 59 
     | 
    
         
            +
            - lib/twitch/chat/channel.rb
         
     | 
| 
      
 60 
     | 
    
         
            +
            - lib/twitch/chat/client.rb
         
     | 
| 
      
 61 
     | 
    
         
            +
            - lib/twitch/chat/connection.rb
         
     | 
| 
      
 62 
     | 
    
         
            +
            - lib/twitch/chat/message.rb
         
     | 
| 
      
 63 
     | 
    
         
            +
            - lib/twitch/chat/version.rb
         
     | 
| 
      
 64 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     | 
| 
      
 65 
     | 
    
         
            +
            - spec/twitch/chat/client_spec.rb
         
     | 
| 
      
 66 
     | 
    
         
            +
            - spec/twitch/chat/message_spec.rb
         
     | 
| 
      
 67 
     | 
    
         
            +
            - twitch-chat.gemspec
         
     | 
| 
      
 68 
     | 
    
         
            +
            homepage: https://github.com/EnotPoloskun/twitch-chat
         
     | 
| 
      
 69 
     | 
    
         
            +
            licenses:
         
     | 
| 
      
 70 
     | 
    
         
            +
            - MIT
         
     | 
| 
      
 71 
     | 
    
         
            +
            metadata: {}
         
     | 
| 
      
 72 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 73 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 74 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 75 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 76 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 77 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 78 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 79 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 80 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 81 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 82 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 83 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 84 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 85 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 86 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 87 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 88 
     | 
    
         
            +
            rubygems_version: 2.2.2
         
     | 
| 
      
 89 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 90 
     | 
    
         
            +
            specification_version: 4
         
     | 
| 
      
 91 
     | 
    
         
            +
            summary: twitch-chat is a Twitch chat client that uses Twitch IRC. Can be used as
         
     | 
| 
      
 92 
     | 
    
         
            +
              twitch chat bot engine.
         
     | 
| 
      
 93 
     | 
    
         
            +
            test_files:
         
     | 
| 
      
 94 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     | 
| 
      
 95 
     | 
    
         
            +
            - spec/twitch/chat/client_spec.rb
         
     | 
| 
      
 96 
     | 
    
         
            +
            - spec/twitch/chat/message_spec.rb
         
     |