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_id = nil) ⇒ Message

Returns a new instance of Message.



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

def initialize(type, text = nil, token = nil, channel_id = nil)
  @type = type
  if text
    @original_text = text
    @filtered_text = filter(text)
    @html = @filtered_text
    @growl = text
  end
  @token = token
  @channel = channel_id
  @add_to_history = true
  @timestamp = Time.now.to_i
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

#jsonObject

Returns the value of attribute json.



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

def json
  @json
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

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
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)


61
62
63
# File 'lib/mad_chatter/message.rb', line 61

def add_to_history?
  @add_to_history
end

#filter(text) ⇒ Object



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

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

#textObject

Helper method for returning filtered text.



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

def text
  @filtered_text
end

#to_jsonObject



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

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

#userObject



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

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

#usernameObject



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

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

#username=(username) ⇒ Object



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

def username=(username)
  @username = username
end