Sha256: 992e7eb8c5e70f602565e751c72f16cd2c4e3ab53b47e1c432b71dee05a0cb2b

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require 'cgi'

module MadChatter
  class Message
    
    attr_accessor :type, :original_text, :filtered_text, :html, :token, :channel, :growl, :add_to_history
    
    def initialize(type, text = nil, token = nil, channel = nil)
      @type = type
      if text
        @original_text = text
        @filtered_text = filter(text)
        @html = @filtered_text
        @growl = text
      end
      @token = token
      @channel = channel
      @add_to_history = true
    end
    
    def username=(username)
      @username = username
    end
    
    def username
      unless @username
        MadChatter.users.each do |user|
          @username = user.username if user.has_token?(@token)
        end
      end
      @username
    end
    
    def user
      MadChatter.find_user_by_token(@token) if @token
    end
    
    # Helper method for returning filtered text.
    def text
      @filtered_text
    end
    
    def to_json
      JSON.generate({
        type: @type,
        text: @original_text,
        html: @html,
        username: username,
        channel: @channel,
        growl: @growl,
      })
    end
    
    def filter(text)
      CGI::escapeHTML(text).strip
    end
    
    def add_to_history?
      @add_to_history
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mad_chatter-0.3.1 lib/mad_chatter/message.rb
mad_chatter-0.3.0 lib/mad_chatter/message.rb