Class: Flamethrower::Campfire::Message

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

Constant Summary collapse

RETRY_SECONDS =
15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Message

Returns a new instance of Message.



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

def initialize(params = {})
  @body = params['body']
  @user = params['user']
  @room = params['room']
  @user_id = set_user_id(params)
  @message_type = params['type']
  @direction = params['direction']
  @status = "pending"
  @image_converted = false
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def body
  @body
end

#directionObject

Returns the value of attribute direction.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def direction
  @direction
end

#image_convertedObject

Returns the value of attribute image_converted.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def image_converted
  @image_converted
end

#message_typeObject

Returns the value of attribute message_type.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def message_type
  @message_type
end

#retry_atObject

Returns the value of attribute retry_at.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def retry_at
  @retry_at
end

#roomObject

Returns the value of attribute room.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def room
  @room
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def status
  @status
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def user
  @user
end

#user_idObject

Returns the value of attribute user_id.



4
5
6
# File 'lib/flamethrower/campfire/message.rb', line 4

def user_id
  @user_id
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/flamethrower/campfire/message.rb', line 32

def failed?
  @status == "failed"
end

#has_images?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/flamethrower/campfire/message.rb', line 60

def has_images?
  image_urls.size > 0
end

#image_urlsObject



52
53
54
55
56
57
58
# File 'lib/flamethrower/campfire/message.rb', line 52

def image_urls
  if @body
    @body.scan(/(https?:\/\/.+?\.(?:jpg|jpeg|gif|png))/i).flatten
  else
    []
  end
end

#inbound?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/flamethrower/campfire/message.rb', line 44

def inbound?
  @direction == "inbound"
end

#mark_delivered!Object



19
20
21
# File 'lib/flamethrower/campfire/message.rb', line 19

def mark_delivered!
  @status = "delivered"
end

#mark_failed!Object



23
24
25
26
# File 'lib/flamethrower/campfire/message.rb', line 23

def mark_failed!
  @status = "failed"
  @retry_at = Time.now + RETRY_SECONDS
end

#mark_pending!Object



28
29
30
# File 'lib/flamethrower/campfire/message.rb', line 28

def mark_pending!
  @status = "pending"
end

#needs_image_conversion?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/flamethrower/campfire/message.rb', line 48

def needs_image_conversion?
  has_images? && !@image_converted
end

#outbound?Boolean

Returns:

  • (Boolean)


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

def outbound?
  @direction == "outbound"
end

#pending?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/flamethrower/campfire/message.rb', line 36

def pending?
  @status == "pending"
end

#set_ascii_image(str) ⇒ Object



64
65
66
67
68
# File 'lib/flamethrower/campfire/message.rb', line 64

def set_ascii_image(str)
  @body << "\n#{str}"
  @message_type = "PasteMessage"
  @image_converted = true
end

#to_ircObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/flamethrower/campfire/message.rb', line 70

def to_irc
  case message_type
  when "TextMessage"
    irc_string = ":#{@user.to_irc.to_s} PRIVMSG #{@room.to_irc.name} :#{@body}"
  when "EnterMessage"
    irc_string = ":#{@user.to_irc.to_s} JOIN #{@room.to_irc.name}"
  when "KickMessage"
    irc_string = ":#{@user.to_irc.to_s} PART #{@room.to_irc.name}"
  when "LeaveMessage"
    irc_string = ":#{@user.to_irc.to_s} PART #{@room.to_irc.name}"
  when "PasteMessage"
    irc_string = format_paste_message
  else
    return
  end
  Flamethrower::Irc::Message.new(irc_string)
end