Class: Ahoy::Chat

Inherits:
Object
  • Object
show all
Defined in:
lib/ahoy/chat.rb

Overview

Ahoy::Chat models a conversation between the user and one of their contacts. It can be thought of as representing an iChat chat window, or treated more like a Ruby socket.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contact_name) ⇒ Chat

:call-seq: Chat.new(contact_name) -> chat

Create a new Ahoy::Chat.



17
18
19
20
21
# File 'lib/ahoy/chat.rb', line 17

def initialize(contact_name)
  @contact_name = contact_name
  @client = nil
  self.use_markdown = Ahoy.use_markdown
end

Instance Attribute Details

#contact_nameObject (readonly)

Returns the value of attribute contact_name.



11
12
13
# File 'lib/ahoy/chat.rb', line 11

def contact_name
  @contact_name
end

Instance Method Details

#<<(string) ⇒ Object

:call-seq: chat << string -> chat

See #send



139
140
141
142
# File 'lib/ahoy/chat.rb', line 139

def <<(string)
  send(string)
  self
end

#closeObject

:call-seq: chat.close -> nil

End the chat.



102
103
104
105
# File 'lib/ahoy/chat.rb', line 102

def close
  client.close
  @client = nil
end

#connect(host, port = nil) ⇒ Object

:call-seq: chat.connect(target, port) -> chat chat.connect(socket) -> chat

Connect to target on port, or use the connection provided by socket.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ahoy/chat.rb', line 28

def connect(host, port=nil)
  if host.respond_to?(:read) && host.respond_to?(:write)
    connect_with(host)
  else
    begin
      client.connect(host, port)
    rescue Errno::ECONNREFUSED
      raise Ahoy::ContactOfflineError.new("Contact Offline")
    end
  end
  self
end

#connected?Boolean

:call-seq: chat.connected? -> bool

Returns:

  • (Boolean)


43
44
45
# File 'lib/ahoy/chat.rb', line 43

def connected?
  client.is_connected?
end

#markdown?Boolean Also known as: use_markdown

:call-seq: chat.markdown? -> bool

Are messages sent to this chat being interpreted as markdown?

Returns:

  • (Boolean)


130
131
132
# File 'lib/ahoy/chat.rb', line 130

def markdown?
  @use_markdown && markdown_processor
end

#on_reply(&block) ⇒ Object

:call-seq: chat.on_reply {|string| block }

Set up block as a callback for when a message is received.



65
66
67
68
69
70
71
# File 'lib/ahoy/chat.rb', line 65

def on_reply(&block)
  client.delete_message_callback("on_reply")
  
  client.add_message_callback(0, "on_reply") do |message|
    block.call(message.body) if message.type == :chat
  end
end

#puts(string) ⇒ Object

:call-seq: chat.puts(string) -> nil

See #send



148
149
150
151
# File 'lib/ahoy/chat.rb', line 148

def puts(string)
  send(string)
  nil
end

#receive(*ignore_args) ⇒ Object Also known as: read, sysread, gets, readline

:call-seq: chat.receive -> string

Block until a message is received, then return the message body as a string.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ahoy/chat.rb', line 78

def receive(*ignore_args)
  thread = Thread.current
  reply = nil
  
  client.add_message_callback(0, "receive") do |message|
    if message.type == :chat
      reply = message.body
      thread.run
    end
  end
  Thread.stop
  
  client.delete_message_callback("receive")
  reply
end

#send(text) ⇒ Object

:call-seq: chat.send(string) -> message

Send string to contact. May raise Ahoy::ContactOfflineError.



51
52
53
54
55
56
57
58
59
# File 'lib/ahoy/chat.rb', line 51

def send(text)
  raise Ahoy::NotConnectedError.new("Not Connected") unless connected?
  
  message = Jabber::Message.new(contact_name, text)
  message.type = :chat
  markdown(message) if markdown?
  client.send(message)
  message
end

#use_markdown=(value) ⇒ Object

:call-seq: chat.use_markdown = bool -> bool

Set true to send a html copy of messages, by interpreting the message text as markdown.



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ahoy/chat.rb', line 112

def use_markdown=(value)
  @use_markdown = value
  if value && !markdown_processor
    %W{rdiscount kramdown maruku bluecloth}.each do |lib|
      begin
        require lib
        break
      rescue LoadError
      end
    end
  end
  value
end

#write(string) ⇒ Object Also known as: syswrite

:call-seq: chat.write -> integer

See #send



157
158
159
160
# File 'lib/ahoy/chat.rb', line 157

def write(string)
  send(string)
  string.length
end