Class: Twterm::DirectMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/twterm/direct_message.rb

Defined Under Namespace

Classes: Conversation

Constant Summary collapse

@@instances =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ DirectMessage



10
11
12
13
14
15
# File 'lib/twterm/direct_message.rb', line 10

def initialize(message)
  @id = message.id
  update!(message)

  @@instances[id] = self
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



6
7
8
# File 'lib/twterm/direct_message.rb', line 6

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/twterm/direct_message.rb', line 6

def id
  @id
end

#recipientObject (readonly)

Returns the value of attribute recipient.



6
7
8
# File 'lib/twterm/direct_message.rb', line 6

def recipient
  @recipient
end

#senderObject (readonly)

Returns the value of attribute sender.



6
7
8
# File 'lib/twterm/direct_message.rb', line 6

def sender
  @sender
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/twterm/direct_message.rb', line 6

def text
  @text
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
# File 'lib/twterm/direct_message.rb', line 17

def ==(other)
  other.is_a?(self.class) && id == other.id
end

#dateObject



21
22
23
24
# File 'lib/twterm/direct_message.rb', line 21

def date
  format = Time.now - @created_at < 86_400 ? '%H:%M:%S' : '%Y-%m-%d %H:%M:%S'
  @created_at.strftime(format)
end

#matches?(q) ⇒ Boolean



26
27
28
29
30
31
32
# File 'lib/twterm/direct_message.rb', line 26

def matches?(q)
  [
    sender.name,
    sender.screen_name,
    text
  ].map(&:downcase).any? { |x| x.include?(q) }
end

#update!(message) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/twterm/direct_message.rb', line 34

def update!(message)
  @created_at = message.created_at.dup.localtime
  @recipient = User.new(message.recipient)
  @sender = User.new(message.sender)
  @text = message.text

  self
end