Class: ShoutBot

Inherits:
Object
  • Object
show all
Defined in:
lib/shout-bot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, port, nick, password = nil) {|_self| ... } ⇒ ShoutBot

Returns a new instance of ShoutBot.

Yields:

  • (_self)

Yield Parameters:

  • _self (ShoutBot)

    the object that the method was called on

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shout-bot.rb', line 50

def initialize(server, port, nick, password=nil)
  raise ArgumentError unless block_given?

  @socket = TCPSocket.open(server, port || 6667)
  @socket.puts "PASSWORD #{password}" if password
  @socket.puts "NICK #{nick}"
  @socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
  sleep 1
  yield self
  @socket.puts "QUIT"
  @socket.gets until @socket.eof?
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



48
49
50
# File 'lib/shout-bot.rb', line 48

def channel
  @channel
end

Class Method Details

.shout(uri, password = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shout-bot.rb', line 34

def self.shout(uri, password = nil, &block)
  raise ArgumentError unless block_given?

  uri = Addressable::URI.parse(uri)
  irc = new(uri.host, uri.port, uri.user, uri.password) do |irc|
    if channel = uri.fragment
      irc.join(channel, password, &block)
    else
      irc.channel = uri.path[1..-1]
      yield irc
    end
  end
end

Instance Method Details

#join(channel, password = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (ShoutBot)

    the object that the method was called on

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
# File 'lib/shout-bot.rb', line 63

def join(channel, password = nil)
  raise ArgumentError unless block_given?
  params = channel.split('?')
  @channel = "##{params[0]}"
  @channel_password = params[1] || password || ""
  @socket.puts "JOIN #{@channel} #{@channel_password}"
  yield self
  @socket.puts "PART #{@channel}"
end

#say(message) ⇒ Object



73
74
75
76
# File 'lib/shout-bot.rb', line 73

def say(message)
  return unless @channel
  @socket.puts "PRIVMSG #{@channel} :#{message}"
end