Class: MadChatter::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/mad_chatter/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, text = nil, token = nil, channel = nil) ⇒ Message

Returns a new instance of Message.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mad_chatter/message.rb', line 8

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

Instance Attribute Details

#add_to_historyObject

Returns the value of attribute add_to_history.



6
7
8
# File 'lib/mad_chatter/message.rb', line 6

def add_to_history
  @add_to_history
end

#channelObject

Returns the value of attribute channel.



6
7
8
# File 'lib/mad_chatter/message.rb', line 6

def channel
  @channel
end

#filtered_textObject

Returns the value of attribute filtered_text.



6
7
8
# File 'lib/mad_chatter/message.rb', line 6

def filtered_text
  @filtered_text
end

#growlObject

Returns the value of attribute growl.



6
7
8
# File 'lib/mad_chatter/message.rb', line 6

def growl
  @growl
end

#htmlObject

Returns the value of attribute html.



6
7
8
# File 'lib/mad_chatter/message.rb', line 6

def html
  @html
end

#original_textObject

Returns the value of attribute original_text.



6
7
8
# File 'lib/mad_chatter/message.rb', line 6

def original_text
  @original_text
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/mad_chatter/message.rb', line 6

def token
  @token
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/mad_chatter/message.rb', line 6

def type
  @type
end

Instance Method Details

#add_to_history?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mad_chatter/message.rb', line 58

def add_to_history?
  @add_to_history
end

#filter(text) ⇒ Object



54
55
56
# File 'lib/mad_chatter/message.rb', line 54

def filter(text)
  CGI::escapeHTML(text).strip
end

#textObject

Helper method for returning filtered text.



39
40
41
# File 'lib/mad_chatter/message.rb', line 39

def text
  @filtered_text
end

#to_jsonObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/mad_chatter/message.rb', line 43

def to_json
  JSON.generate({
    type: @type,
    text: @original_text,
    html: @html,
    username: username,
    channel: @channel,
    growl: @growl,
  })
end

#userObject



34
35
36
# File 'lib/mad_chatter/message.rb', line 34

def user
  MadChatter.find_user_by_token(@token) if @token
end

#usernameObject



25
26
27
28
29
30
31
32
# File 'lib/mad_chatter/message.rb', line 25

def username
  unless @username
    MadChatter.users.each do |user|
      @username = user.username if user.has_token?(@token)
    end
  end
  @username
end

#username=(username) ⇒ Object



21
22
23
# File 'lib/mad_chatter/message.rb', line 21

def username=(username)
  @username = username
end