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

Constructor Details

#initialize(type, bot = nil) ⇒ Message

Returns a new instance of Message.



112
113
114
115
116
# File 'lib/rubirai/messages/message.rb', line 112

def initialize(type, bot = nil)
  Message.check_type type
  @bot = bot
  @type = type
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



25
26
27
# File 'lib/rubirai/messages/message.rb', line 25

def bot
  @bot
end

#typeObject (readonly)

Returns the value of attribute type.



25
26
27
# File 'lib/rubirai/messages/message.rb', line 25

def type
  @type
end

Class Method Details

.all_typesArray<Symbol>

Get all message types (subclasses)

Returns:

  • (Array<Symbol>)

    all message types



46
47
48
49
50
51
52
# File 'lib/rubirai/messages/message.rb', line 46

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

.AtAllMessage(**kwargs) ⇒ Rubirai::AtAllMessage

Parameters:

  • kwargs (Hash{Symbol => Object})

    arguments

Returns:



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rubirai/messages/message.rb', line 24

class Message
  attr_reader :bot, :type

  # Objects to {Rubirai::Message}
  #
  # @param msg [Rubirai::Message, Hash, Object] the object to transform to a message
  # @return [Rubirai::Message] the message
  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

  # Get all message types (subclasses)
  #
  # @return [Array<Symbol>] all message types
  def self.all_types
    %i[
      Source Quote At AtAll Face Plain Image
      FlashImage Voice Xml Json App Poke Forward
      File MusicShare
    ]
  end

  # 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
  def self.check_type(type)
    raise(RubiraiError, 'type not in all message types') unless Message.all_types.include? type
  end

  def self.get_msg_klass(type)
    Object.const_get "Rubirai::#{type}Message"
  end

  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

  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

  def initialize(type, bot = nil)
    Message.check_type type
    @bot = bot
    @type = type
  end

  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

  def self.metaclass
    class << self
      self
    end
  end
end

.AtMessage(**kwargs) ⇒ Rubirai::AtMessage

Parameters:

  • kwargs (Hash{Symbol => Object})

    arguments

Returns:



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rubirai/messages/message.rb', line 24

class Message
  attr_reader :bot, :type

  # Objects to {Rubirai::Message}
  #
  # @param msg [Rubirai::Message, Hash, Object] the object to transform to a message
  # @return [Rubirai::Message] the message
  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

  # Get all message types (subclasses)
  #
  # @return [Array<Symbol>] all message types
  def self.all_types
    %i[
      Source Quote At AtAll Face Plain Image
      FlashImage Voice Xml Json App Poke Forward
      File MusicShare
    ]
  end

  # 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
  def self.check_type(type)
    raise(RubiraiError, 'type not in all message types') unless Message.all_types.include? type
  end

  def self.get_msg_klass(type)
    Object.const_get "Rubirai::#{type}Message"
  end

  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

  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

  def initialize(type, bot = nil)
    Message.check_type type
    @bot = bot
    @type = type
  end

  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

  def self.metaclass
    class << self
      self
    end
  end
end

.build_from(hash, bot = nil) ⇒ Object

Raises:



65
66
67
68
69
70
71
72
73
# File 'lib/rubirai/messages/message.rb', line 65

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) ⇒ 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:



57
58
59
# File 'lib/rubirai/messages/message.rb', line 57

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

.FaceMessage(**kwargs) ⇒ Rubirai::FaceMessage

Parameters:

  • kwargs (Hash{Symbol => Object})

    arguments

Returns:



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rubirai/messages/message.rb', line 24

class Message
  attr_reader :bot, :type

  # Objects to {Rubirai::Message}
  #
  # @param msg [Rubirai::Message, Hash, Object] the object to transform to a message
  # @return [Rubirai::Message] the message
  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

  # Get all message types (subclasses)
  #
  # @return [Array<Symbol>] all message types
  def self.all_types
    %i[
      Source Quote At AtAll Face Plain Image
      FlashImage Voice Xml Json App Poke Forward
      File MusicShare
    ]
  end

  # 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
  def self.check_type(type)
    raise(RubiraiError, 'type not in all message types') unless Message.all_types.include? type
  end

  def self.get_msg_klass(type)
    Object.const_get "Rubirai::#{type}Message"
  end

  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

  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

  def initialize(type, bot = nil)
    Message.check_type type
    @bot = bot
    @type = type
  end

  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

  def self.metaclass
    class << self
      self
    end
  end
end

.get_msg_klass(type) ⇒ Object



61
62
63
# File 'lib/rubirai/messages/message.rb', line 61

def self.get_msg_klass(type)
  Object.const_get "Rubirai::#{type}Message"
end

.metaclassObject



134
135
136
137
138
# File 'lib/rubirai/messages/message.rb', line 134

def self.metaclass
  class << self
    self
  end
end

.PlainMessage(**kwargs) ⇒ Rubirai::PlainMessage



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rubirai/messages/message.rb', line 24

class Message
  attr_reader :bot, :type

  # Objects to {Rubirai::Message}
  #
  # @param msg [Rubirai::Message, Hash, Object] the object to transform to a message
  # @return [Rubirai::Message] the message
  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

  # Get all message types (subclasses)
  #
  # @return [Array<Symbol>] all message types
  def self.all_types
    %i[
      Source Quote At AtAll Face Plain Image
      FlashImage Voice Xml Json App Poke Forward
      File MusicShare
    ]
  end

  # 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
  def self.check_type(type)
    raise(RubiraiError, 'type not in all message types') unless Message.all_types.include? type
  end

  def self.get_msg_klass(type)
    Object.const_get "Rubirai::#{type}Message"
  end

  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

  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

  def initialize(type, bot = nil)
    Message.check_type type
    @bot = bot
    @type = type
  end

  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

  def self.metaclass
    class << self
      self
    end
  end
end

.QuoteMessage(**kwargs) ⇒ Rubirai::QuoteMessage

Parameters:

  • kwargs (Hash{Symbol => Object})

    arguments

Returns:



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rubirai/messages/message.rb', line 24

class Message
  attr_reader :bot, :type

  # Objects to {Rubirai::Message}
  #
  # @param msg [Rubirai::Message, Hash, Object] the object to transform to a message
  # @return [Rubirai::Message] the message
  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

  # Get all message types (subclasses)
  #
  # @return [Array<Symbol>] all message types
  def self.all_types
    %i[
      Source Quote At AtAll Face Plain Image
      FlashImage Voice Xml Json App Poke Forward
      File MusicShare
    ]
  end

  # 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
  def self.check_type(type)
    raise(RubiraiError, 'type not in all message types') unless Message.all_types.include? type
  end

  def self.get_msg_klass(type)
    Object.const_get "Rubirai::#{type}Message"
  end

  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

  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

  def initialize(type, bot = nil)
    Message.check_type type
    @bot = bot
    @type = type
  end

  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

  def self.metaclass
    class << self
      self
    end
  end
end

.set_message(type, *attr_keys, &initialize_block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubirai/messages/message.rb', line 75

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) ⇒ Rubirai::Message

Objects to Rubirai::Message

Parameters:

Returns:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubirai/messages/message.rb', line 31

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_hObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rubirai/messages/message.rb', line 118

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