Class: Rubirai::Message Abstract
- Inherits:
-
Object
- Object
- Rubirai::Message
- Defined in:
- lib/rubirai/messages/message.rb
Overview
This class is abstract.
The message abstract class.
Direct Known Subclasses
AppMessage, AtAllMessage, AtMessage, FaceMessage, FileMessage, FlashImageMessage, ForwardMessage, ImageMessage, JsonMessage, MusicShareMessage, PlainMessage, PokeMessage, QuoteMessage, SourceMessage, VoiceMessage, XmlMessage
Instance Attribute Summary collapse
-
#bot ⇒ Bot
readonly
The bot.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.all_types ⇒ Array<Symbol>
Get all message types (subclasses).
-
.check_type(type) ⇒ Object
Check if a type is in all message types.
-
.to_message(msg, bot = nil) ⇒ Rubirai::Message
Objects to Message.
Instance Method Summary collapse
-
#to_h ⇒ Hash{String => Object}
Convert the message to a hash.
Instance Attribute Details
#bot ⇒ Bot (readonly)
Returns the bot.
79 80 81 |
# File 'lib/rubirai/messages/message.rb', line 79 def bot @bot end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
79 |
# File 'lib/rubirai/messages/message.rb', line 79 attr_reader :bot, :type |
Class Method Details
.all_types ⇒ Array<Symbol>
Get all message types (subclasses)
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
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
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rubirai/messages/message.rb', line 85 def self.(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_h ⇒ Hash{String => Object}
Convert the message to a hash
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 |