Skip to content

Instantly share code, notes, and snippets.

@anlek
Forked from tmm1/gist:329682
Created June 9, 2010 17:49

Revisions

  1. @tmm1 tmm1 revised this gist Jun 9, 2010. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions gistfile1.builder
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@ module ChatClient
    quit and return if @buf =~ /(\006|\004)/ # ctrl+c or ctrl+d

    while line = @buf.slice!(/(.+)\r?\n/)
    p [@name, line]
    send_prompt

    if line =~ %r|^/nick (.+)|
    @@ -55,7 +56,7 @@ module ChatClient
    send_prompt

    elsif !line.strip.empty?
    ChatClient.channel << "#{@name.bold}: #{line}"
    ChatClient.channel << "#{@name.bold}: #{line.gsub(/[^[:print:]]/,'')}\n"
    end
    end
    end
    @@ -151,9 +152,9 @@ EM.run{
    ChatClient.channel << "\033[31mThe time is now #{Time.now}\033[0m\n"
    end

    TwitterStream.new('eventmachine', 'eventm4chine', %w[ railsconf eventmachine ]) do |tweet|
    TwitterStream.new('USERNAME', 'PASSWORD', %w[ railsconf eventmachine ]) do |tweet|
    name, msg = tweet[:user][:screen_name], tweet[:text]
    p [:tweet, name, msg]
    ChatClient.channel << "\033[34;1m@#{name}\033[0m: #{msg}\n"
    ChatClient.channel << "\033[34;1m@#{name}\033[0m: #{msg}\n"
    end
    }
  2. @tmm1 tmm1 renamed this gist Jun 8, 2010. 1 changed file with 63 additions and 35 deletions.
    98 changes: 63 additions & 35 deletions gistfile1.rb → gistfile1.builder
    Original file line number Diff line number Diff line change
    @@ -3,25 +3,29 @@
    require 'em-http' # gem install em-http-request
    require 'yajl' # gem install yajl-ruby

    class String
    def bold
    "\033[1m#{self}\033[0m"
    end
    end

    module ChatClient
    def post_init
    @name = "anonymous_#{ChatClient.client_num+=1}"
    @sid = ChatClient.channel.subscribe(method(:send_msg))
    @buf = ''
    @name = "anonymous_#{ChatClient.client_num+=1}"
    @sid = ChatClient.channel.subscribe do |msg|
    send_msg(msg)
    end

    send_data "\033[2;23r"
    send_data "\033[2J"
    send_data "\033[1H"
    header = "Welcome to the EventMachine MWRC Demo (/say, /nick, /quit)".center(80)
    send_data "\033[32m#{header}\033[0m"
    send_header
    send_prompt

    ChatClient.channel << "\033[1m#{@name}\033[0m has joined.\n"
    ChatClient.channel << "#{@name.bold} has joined.\n"
    end

    def unbind
    ChatClient.channel.unsubscribe(@sid)
    ChatClient.channel << "\033[1m#{@name}\033[0m has left.\n"
    ChatClient.channel << "#{@name.bold} has left.\n"
    end

    def receive_data data
    @@ -33,11 +37,15 @@ def receive_data data

    if line =~ %r|^/nick (.+)|
    new_name = $1.strip[0..12]
    ChatClient.channel << "\033[1m#{@name}\033[0m is now known as \033[1m#{new_name}\033[0m\n"
    ChatClient.channel << "#{@name.bold} is now known as #{new_name.bold}\n"
    @name = new_name
    send_prompt

    elsif line =~ %r|^/me (.+)|
    ChatClient.channel << "#{@name.bold} #{$1.strip}\n"

    elsif line =~ %r|^/say (.+)|
    send_msg "\033[37mAdding #{$1.strip.bold} to the say queue at position #{ChatClient.say_queue.size+1}\033[0m\n"
    ChatClient.say_queue.push "#{@name} said #{$1.strip}"

    elsif line =~ %r|^/quit|
    @@ -47,13 +55,21 @@ def receive_data data
    send_prompt

    elsif !line.strip.empty?
    ChatClient.channel << "\033[1m#{@name}\033[0m: #{line}"
    ChatClient.channel << "#{@name.bold}: #{line}"
    end
    end
    end

    private

    def send_header
    send_data "\033[2;23r"
    send_data "\033[2J"
    send_data "\033[1H"
    header = "Welcome to the EventMachine Chat Demo (/nick, /me, /say, /quit)".center(80)
    send_data "\033[32m#{header}\033[0m"
    end

    def send_msg msg
    send_data "\0337"
    send_data "\033M"
    @@ -85,47 +101,59 @@ class << self
    attr_accessor :client_num
    end

    def self.channel
    def ChatClient.channel
    @channel ||= EM::Channel.new
    end

    def self.say_queue
    unless @say_queue
    @say_queue = EM::Queue.new
    def ChatClient.say_queue
    @say_queue ||= begin
    q = EM::Queue.new

    # pop off items from the queue one at a time, and pass them to `say`
    processor = proc{ |msg|
    ChatClient.channel << "\033[37mSaying: #{msg.bold}\033[0m\n"
    EM.system("say", msg) do |out, status|
    p [msg, out, status]
    @say_queue.pop(&processor)
    p [:say, msg, status]
    q.pop(&processor)
    end
    }

    # pop off the first item
    @say_queue.pop(&processor)
    q.pop(&processor)

    q
    end
    end
    end

    @say_queue
    class TwitterStream
    def initialize(user, pass, tags, &blk)
    raise ArgumentError, 'block required' unless blk

    http = EM::HttpRequest.new(
    "http://stream.twitter.com/1/statuses/filter.json?track=#{tags.join(',')}"
    ).get(
    :head => {'authorization' => [user, pass]}
    )

    parser = Yajl::Parser.new(:symbolize_keys => true)
    parser.on_parse_complete = blk
    http.stream do |data|
    parser << data
    end
    end
    end

    EM.run{
    EM.start_server '0.0.0.0', 1337, ChatClient

    http = EM::HttpRequest.new(
    "http://stream.twitter.com/1/statuses/filter.json?track=mwrc,eventmachine"
    ).get(
    :head => {
    'authorization' => ['eventmachine', 'eventm4chine']
    }
    )

    parser = Yajl::Parser.new(:symbolize_keys => true)
    parser.on_parse_complete = proc{ |item|
    name = item[:user][:screen_name]
    tweet = item[:text]
    p [:twitter, name, tweet]
    ChatClient.channel << "\033[34;1m@#{name}\033[0m: #{tweet}\n"
    }
    http.stream &parser.method(:<<)
    EM::PeriodicTimer.new(90) do
    ChatClient.channel << "\033[31mThe time is now #{Time.now}\033[0m\n"
    end

    TwitterStream.new('eventmachine', 'eventm4chine', %w[ railsconf eventmachine ]) do |tweet|
    name, msg = tweet[:user][:screen_name], tweet[:text]
    p [:tweet, name, msg]
    ChatClient.channel << "\033[34;1m@#{name}\033[0m: #{msg}\n"
    end
    }
  3. @tmm1 tmm1 revised this gist Mar 12, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -95,7 +95,7 @@ def self.say_queue

    # pop off items from the queue one at a time, and pass them to `say`
    processor = proc{ |msg|
    EM.system("say #{msg}") do |out, status|
    EM.system("say", msg) do |out, status|
    p [msg, out, status]
    @say_queue.pop(&processor)
    end
  4. @tmm1 tmm1 revised this gist Mar 11, 2010. 1 changed file with 18 additions and 10 deletions.
    28 changes: 18 additions & 10 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    require 'rubygems'
    require 'eventmachine'
    require 'twitter/json_stream' # gem install twitter-stream
    require 'yajl' # gem install yajl-ruby
    require 'em-http' # gem install em-http-request
    require 'yajl' # gem install yajl-ruby

    module ChatClient
    def post_init
    @@ -112,12 +112,20 @@ def self.say_queue
    EM.run{
    EM.start_server '0.0.0.0', 1337, ChatClient

    Twitter::JSONStream.connect(
    :path => '/1/statuses/filter.json?track=mwrc,eventmachine',
    :auth => 'eventmachine:eventm4chine'
    ).each_item do |item|
    item = Yajl.load(item, :symbolize_keys=>true)
    p [:twitter, item[:user][:screen_name], item[:text]]
    ChatClient.channel << "\033[34;1m@#{item[:user][:screen_name]}\033[0m: #{item[:text]}\n"
    end
    http = EM::HttpRequest.new(
    "http://stream.twitter.com/1/statuses/filter.json?track=mwrc,eventmachine"
    ).get(
    :head => {
    'authorization' => ['eventmachine', 'eventm4chine']
    }
    )

    parser = Yajl::Parser.new(:symbolize_keys => true)
    parser.on_parse_complete = proc{ |item|
    name = item[:user][:screen_name]
    tweet = item[:text]
    p [:twitter, name, tweet]
    ChatClient.channel << "\033[34;1m@#{name}\033[0m: #{tweet}\n"
    }
    http.stream &parser.method(:<<)
    }
  5. @tmm1 tmm1 revised this gist Mar 11, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ def unbind

    def receive_data data
    @buf << data
    quit if @buf =~ /(\006|\004)/ # ctrl+c or ctrl+d
    quit and return if @buf =~ /(\006|\004)/ # ctrl+c or ctrl+d

    while line = @buf.slice!(/(.+)\r?\n/)
    send_prompt
  6. @tmm1 tmm1 revised this gist Mar 11, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    require 'rubygems'
    require 'eventmachine'
    require 'twitter/json_stream'
    require 'yajl'
    require 'twitter/json_stream' # gem install twitter-stream
    require 'yajl' # gem install yajl-ruby

    module ChatClient
    def post_init
  7. @tmm1 tmm1 revised this gist Mar 11, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -113,7 +113,7 @@ def self.say_queue
    EM.start_server '0.0.0.0', 1337, ChatClient

    Twitter::JSONStream.connect(
    :path => '/1/statuses/filter.json?track=mwrc,eventmachine,twitter',
    :path => '/1/statuses/filter.json?track=mwrc,eventmachine',
    :auth => 'eventmachine:eventm4chine'
    ).each_item do |item|
    item = Yajl.load(item, :symbolize_keys=>true)
  8. @tmm1 tmm1 revised this gist Mar 11, 2010. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,7 @@ def send_prompt
    def quit
    send_data "\033[2J"
    send_data "\033[1;1H"
    header = "Check out http://gist.github.com/123 for the source to the EM chat server".center(80)
    header = "Check out http://gist.github.com/329682 for the source to the EM chat server".center(80)
    send_data "\033[32m#{header}\033[0m"
    send_data "\033[r"
    send_data "\033[3;1H"
    @@ -113,11 +113,11 @@ def self.say_queue
    EM.start_server '0.0.0.0', 1337, ChatClient

    Twitter::JSONStream.connect(
    :path => '/1/statuses/filter.json?track=mwrc,eventmachine',
    :path => '/1/statuses/filter.json?track=mwrc,eventmachine,twitter',
    :auth => 'eventmachine:eventm4chine'
    ).each_item do |item|
    item = Yajl.load(item, :symbolize_keys=>true)
    p [:twitter, item]
    ChatClient.channel << "\033[34;1m@#{item[:screen_name]}\033[0m: #{item[:status]}"
    p [:twitter, item[:user][:screen_name], item[:text]]
    ChatClient.channel << "\033[34;1m@#{item[:user][:screen_name]}\033[0m: #{item[:text]}\n"
    end
    }
    }
  9. @tmm1 tmm1 created this gist Mar 11, 2010.
    123 changes: 123 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    require 'rubygems'
    require 'eventmachine'
    require 'twitter/json_stream'
    require 'yajl'

    module ChatClient
    def post_init
    @name = "anonymous_#{ChatClient.client_num+=1}"
    @sid = ChatClient.channel.subscribe(method(:send_msg))
    @buf = ''

    send_data "\033[2;23r"
    send_data "\033[2J"
    send_data "\033[1H"
    header = "Welcome to the EventMachine MWRC Demo (/say, /nick, /quit)".center(80)
    send_data "\033[32m#{header}\033[0m"
    send_prompt

    ChatClient.channel << "\033[1m#{@name}\033[0m has joined.\n"
    end

    def unbind
    ChatClient.channel.unsubscribe(@sid)
    ChatClient.channel << "\033[1m#{@name}\033[0m has left.\n"
    end

    def receive_data data
    @buf << data
    quit if @buf =~ /(\006|\004)/ # ctrl+c or ctrl+d

    while line = @buf.slice!(/(.+)\r?\n/)
    send_prompt

    if line =~ %r|^/nick (.+)|
    new_name = $1.strip[0..12]
    ChatClient.channel << "\033[1m#{@name}\033[0m is now known as \033[1m#{new_name}\033[0m\n"
    @name = new_name
    send_prompt

    elsif line =~ %r|^/say (.+)|
    ChatClient.say_queue.push "#{@name} said #{$1.strip}"

    elsif line =~ %r|^/quit|
    quit

    elsif line =~ /^\e/ # ignore escape sequences
    send_prompt

    elsif !line.strip.empty?
    ChatClient.channel << "\033[1m#{@name}\033[0m: #{line}"
    end
    end
    end

    private

    def send_msg msg
    send_data "\0337"
    send_data "\033M"
    send_data "\033[23;1H"
    send_data msg
    send_data "\0338"
    end

    def send_prompt
    send_data "\033[24H"
    send_data "\033[2K"
    send_data "\033[1m"
    send_data @name
    send_data "\033[0m: "
    end

    def quit
    send_data "\033[2J"
    send_data "\033[1;1H"
    header = "Check out http://gist.github.com/123 for the source to the EM chat server".center(80)
    send_data "\033[32m#{header}\033[0m"
    send_data "\033[r"
    send_data "\033[3;1H"
    close_connection_after_writing
    end

    @client_num = 0
    class << self
    attr_accessor :client_num
    end

    def self.channel
    @channel ||= EM::Channel.new
    end

    def self.say_queue
    unless @say_queue
    @say_queue = EM::Queue.new

    # pop off items from the queue one at a time, and pass them to `say`
    processor = proc{ |msg|
    EM.system("say #{msg}") do |out, status|
    p [msg, out, status]
    @say_queue.pop(&processor)
    end
    }

    # pop off the first item
    @say_queue.pop(&processor)
    end

    @say_queue
    end
    end

    EM.run{
    EM.start_server '0.0.0.0', 1337, ChatClient

    Twitter::JSONStream.connect(
    :path => '/1/statuses/filter.json?track=mwrc,eventmachine',
    :auth => 'eventmachine:eventm4chine'
    ).each_item do |item|
    item = Yajl.load(item, :symbolize_keys=>true)
    p [:twitter, item]
    ChatClient.channel << "\033[34;1m@#{item[:screen_name]}\033[0m: #{item[:status]}"
    end
    }