Class: Twitch::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/twitch_chatter/models/channel.rb

Overview

Examples:

channel = Twitch::Channel.new(:twitchgaming)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, bot: nil) ⇒ Channel

Returns a new instance of Channel.

Parameters:

  • name (String, Symbol)
  • bot (Bot, nil) (defaults to: nil)


14
15
16
17
# File 'lib/twitch_chatter/models/channel.rb', line 14

def initialize(name, bot: nil)
  @name = name.to_sym
  @bot = bot
end

Instance Attribute Details

#nameString (readonly)

Returns:

  • (String)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/twitch_chatter/models/channel.rb', line 8

class Channel
  # @return [String]
  attr_reader :name

  # @param [String, Symbol] name
  # @param [Bot, nil] bot
  def initialize(name, bot: nil)
    @name = name.to_sym
    @bot = bot
  end

  # @yieldparam message [Twitch::Message]
  # @return [nil]
  # @example
  #   channel.join do |message|
  #     puts "##{message.channel} #{message.sender}: #{message}"
  #   end
  def join(&block)
    @bot.join(@name, &block)
  end

  # Disconnects from channel and removes all message callbacks.
  # @return [nil]
  def leave
    @bot.leave(@name)
  end

  # @return [Symbol]
  def to_sym
    @name
  end

  # @return [String]
  def to_s
    @name.to_s
  end

  # @return [String]
  def to_url
    "https://twitch.tv/#{@name}"
  end

  alias_method :link, :to_url
  alias_method :href, :to_url

  # @param [Channel, String, Symbol] other
  # @return [Boolean]
  def ==(other)
    if other.is_a?(Channel)
      return @name == other.name
    end

    @name == other.to_sym
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/twitch_chatter/models/channel.rb', line 55

def ==(other)
  if other.is_a?(Channel)
    return @name == other.name
  end

  @name == other.to_sym
end

#join {|message| ... } ⇒ nil

Examples:

channel.join do |message|
  puts "##{message.channel} #{message.sender}: #{message}"
end

Yield Parameters:

Returns:

  • (nil)


25
26
27
# File 'lib/twitch_chatter/models/channel.rb', line 25

def join(&block)
  @bot.join(@name, &block)
end

#leavenil

Disconnects from channel and removes all message callbacks.

Returns:

  • (nil)


31
32
33
# File 'lib/twitch_chatter/models/channel.rb', line 31

def leave
  @bot.leave(@name)
end

#to_sString

Returns:

  • (String)


41
42
43
# File 'lib/twitch_chatter/models/channel.rb', line 41

def to_s
  @name.to_s
end

#to_symSymbol

Returns:

  • (Symbol)


36
37
38
# File 'lib/twitch_chatter/models/channel.rb', line 36

def to_sym
  @name
end

#to_urlString Also known as: link, href

Returns:

  • (String)


46
47
48
# File 'lib/twitch_chatter/models/channel.rb', line 46

def to_url
  "https://twitch.tv/#{@name}"
end