Class: Rubirai::Message Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/rubirai/messages/message.rb

Overview

This class is abstract.

The message abstract class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#botBot (readonly)

Returns the bot.

Returns:

  • (Bot)

    the bot



79
80
81
# File 'lib/rubirai/messages/message.rb', line 79

def bot
  @bot
end

#typeObject (readonly)

Returns the value of attribute type.



79
# File 'lib/rubirai/messages/message.rb', line 79

attr_reader :bot, :type

Class Method Details

.all_typesArray<Symbol>

Get all message types (subclasses)

Returns:

  • (Array<Symbol>)

    all message types



100
101
102
103
104
105
106
# File 'lib/rubirai/messages/message.rb', line 100

def self.all_types
  %i[
    Source Quote At AtAll Face Plain Image
    FlashImage Voice Xml Json App Poke Forward
    File MusicShare
  ]
end

.check_type(type) ⇒ Object

Check if a type is in all message types

Parameters:

  • type (Symbol)

    the type to check

Returns:

  • whether the type is in all message types

Raises:



111
112
113
# File 'lib/rubirai/messages/message.rb', line 111

def self.check_type(type)
  raise(RubiraiError, 'type not in all message types') unless Message.all_types.include? type
end

.to_message(msg, bot = nil) ⇒ Rubirai::Message

Objects to Rubirai::Message

Parameters:

  • msg (Rubirai::Message, Hash{String => Object}, Object)

    the object to transform to a message

Returns:



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rubirai/messages/message.rb', line 85

def self.to_message(msg, bot = nil)
  # noinspection RubyYardReturnMatch
  case msg
  when Message, MessageChain
    msg
  when Hash
    Message.build_from(msg, bot)
  else
    PlainMessage.from(text: msg.to_s, bot: bot)
  end
end

Instance Method Details

#to_hHash{String => Object}

Convert the message to a hash

Returns:

  • (Hash{String => Object})


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rubirai/messages/message.rb', line 179

def to_h
  res = self.class.keys.to_h do |k|
    v = instance_variable_get("@#{k}")
    k = k.to_s.snake_to_camel(lower: true)
    if v.is_a? MessageChain
      [k, v.to_a]
    elsif v&.respond_to?(:to_h)
      [k, v.to_h]
    else
      [k, v]
    end
  end
  res[:type] = @type.to_s
  res.compact.stringify_keys
end