Class: Waylon::RSpec::TestChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/waylon/rspec/test_channel.rb

Overview

The TestChannel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel_id, details = {}) ⇒ TestChannel

Returns a new instance of TestChannel.

Parameters:

  • channel_id (Integer)

    The Channel ID for the new TestChannel

  • details (Hash) (defaults to: {})

    Details (namely ‘name’ and ‘created_at’) for the new TestChannel



37
38
39
40
# File 'lib/waylon/rspec/test_channel.rb', line 37

def initialize(channel_id, details = {})
  @id = channel_id.to_i
  @details = details
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/waylon/rspec/test_channel.rb', line 7

def id
  @id
end

Class Method Details

.allArray<TestChannel>

Simple way to list all TestChannels

Returns:



11
12
13
# File 'lib/waylon/rspec/test_channel.rb', line 11

def self.all
  TestSense.channel_list.each_index.map { |id| new(id) }
end

.find_by_name(name) ⇒ TestChannel?

Looks up an existing TestChannel by name

Returns:



30
31
32
33
# File 'lib/waylon/rspec/test_channel.rb', line 30

def self.find_by_name(name)
  channel_id = TestSense.channel_list.index { |channel| channel[:name] == name }
  channel_id ? new(channel_id) : nil
end

.find_or_create(name) ⇒ TestChannel

Always provides a TestChannel, either by finding an existing or creating a new one

Returns:



17
18
19
20
21
22
23
24
25
26
# File 'lib/waylon/rspec/test_channel.rb', line 17

def self.find_or_create(name)
  existing_channel = find_by_name(name)
  if existing_channel
    existing_channel
  else
    channel_details = { name:, created_at: Time.now }
    TestSense.channel_list << channel_details
    new(TestSense.channel_list.size - 1)
  end
end

Instance Method Details

#created_atTime

Easy access to when the TestChannel was created

Returns:

  • (Time)


44
45
46
# File 'lib/waylon/rspec/test_channel.rb', line 44

def created_at
  details[:created_at]
end

#detailsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lazily provides the details for a TestUser

Returns:

  • (Hash)

    details for this instance



77
78
79
80
# File 'lib/waylon/rspec/test_channel.rb', line 77

def details
  @details = TestSense.room_list[id] if @details.empty?
  @details.dup
end

#nameString

The name of the TestChannel

Returns:

  • (String)


50
51
52
# File 'lib/waylon/rspec/test_channel.rb', line 50

def name
  details[:name]
end

#post_message(content, from: TestUser.whoami) ⇒ Message

Send a TestMessage to a TestChannel

Parameters:

  • content (String, Message)

    The Message to send

Returns:

  • (Message)

    The sent Message object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/waylon/rspec/test_channel.rb', line 57

def post_message(content, from: TestUser.whoami)
  msg = if content.is_a?(Message)
          content.text
        else
          content
        end
  msg_details = {
    user_id: from.id,
    text: msg,
    type: :channel,
    channel_id: id,
    created_at: Time.now
  }
  TestSense.message_list << msg_details
  TestMessage.new(TestSense.message_list.size - 1)
end