class Rubirai::Message
The message abstract class.
@abstract
Attributes
bot[R]
@!attribute [r] bot
@return [Bot] the bot
@!attribute [r] type
@return [Symbol] the type
type[R]
@!attribute [r] bot
@return [Bot] the bot
@!attribute [r] type
@return [Symbol] the type
Public Class Methods
all_types()
click to toggle source
Get all message types (subclasses)
@return [Array<Symbol>] all message types
# 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
build_from(hash, bot = nil)
click to toggle source
@private
# File lib/rubirai/messages/message.rb, line 121 def self.build_from(hash, bot = nil) hash = hash.stringify_keys raise(RubiraiError, 'not a valid message') unless hash.key? 'type' type = hash['type'].to_sym check_type type klass = get_msg_klass type klass.new hash, bot end
check_type(type)
click to toggle source
Check if a type is in all message types @param type [Symbol] the type to check @return whether the type is in all message types
# 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
get_msg_klass(type)
click to toggle source
@private
# File lib/rubirai/messages/message.rb, line 116 def self.get_msg_klass(type) Object.const_get "Rubirai::#{type}Message" end
metaclass()
click to toggle source
@private
# File lib/rubirai/messages/message.rb, line 196 def self.metaclass class << self self end end
new(type, bot = nil)
click to toggle source
@private
# File lib/rubirai/messages/message.rb, line 170 def initialize(type, bot = nil) Message.check_type type @bot = bot @type = type end
set_message(type, *attr_keys, &initialize_block)
click to toggle source
@private
Calls superclass method
# File lib/rubirai/messages/message.rb, line 132 def self.set_message(type, *attr_keys, &initialize_block) attr_reader(*attr_keys) metaclass.instance_eval do define_method(:keys) do attr_keys end break if attr_keys.empty? define_method(:from) do |bot: nil, **kwargs| res = get_msg_klass(type).new({}, bot) attr_keys.each do |key| res.instance_variable_set "@#{key}", kwargs[key] end res end s = +"def from_with_param(#{attr_keys.join('= nil, ')} = nil)\n" s << "res = Rubirai::#{type}Message.new({})\n" attr_keys.each do |key| s << %[res.instance_variable_set("@#{key}", #{key})\n] end s << "res\nend" class_eval s end class_eval do define_method(:initialize) do |hash, bot = nil| # noinspection RubySuperCallWithoutSuperclassInspection super type, bot initialize_block&.call(hash) hash = hash.stringify_keys attr_keys.each do |k| instance_variable_set("@#{k}", hash[k.to_s.snake_to_camel(lower: true)]) end end end end
to_message(msg, bot = nil)
click to toggle source
Objects to {Rubirai::Message}
@param msg [Rubirai::Message, Hash{String => Object}, Object] the object to transform to a message @return [Rubirai::Message] the message
# 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
Public Instance Methods
to_h()
click to toggle source
Convert the message to a hash
@return [Hash{String => Object}]
# 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