Class: ESM::Message
- Inherits:
-
Object
- Object
- ESM::Message
- Defined in:
- lib/esm/message.rb,
lib/esm/message/data.rb,
lib/esm/message/error.rb,
lib/esm/message/player.rb,
lib/esm/message/target.rb,
lib/esm/message/metadata.rb
Defined Under Namespace
Classes: Data, Error, Metadata, Player, Target
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Self
Creates a instance from a hash.
-
.from_string(json) ⇒ Self
Creates an instance of ESM::Message from JSON.
Instance Method Summary collapse
-
#add_error(type, content) ⇒ Object
Adds the provided error to this message.
-
#add_errors(errors = []) ⇒ Object
Each hash has the following attributes: type [Symbol, String] The type of error.
- #data_attributes ⇒ Object
- #error_messages ⇒ Object
-
#errors? ⇒ Boolean
Returns if there are any errors on this message.
-
#initialize ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ Object
-
#set_data(**data_attributes) ⇒ Object
The primary data for this message.
- #set_id(id) ⇒ Object
-
#set_metadata(player: nil, target: nil, server_id: nil) ⇒ Message
Sets various values used by the arma mod and internally by Message::Error.
- #set_type(type) ⇒ Object
-
#to_h ⇒ Hash
Converts the message to a Hash.
-
#to_s ⇒ String
Converts the message to JSON.
Constructor Details
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
46 47 48 |
# File 'lib/esm/message.rb', line 46 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
46 47 48 |
# File 'lib/esm/message.rb', line 46 def errors @errors end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
46 47 48 |
# File 'lib/esm/message.rb', line 46 def id @id end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
46 47 48 |
# File 'lib/esm/message.rb', line 46 def @metadata end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
46 47 48 |
# File 'lib/esm/message.rb', line 46 def type @type end |
Class Method Details
.from_hash(hash) ⇒ Self
Creates a instance from a hash
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/esm/message.rb', line 24 def self.from_hash(hash) hash = hash.deep_symbolize_keys = new .set_id(hash[:id]) if hash[:id].present? .set_type(hash[:type]) if hash[:type].present? if hash[:data].present? .set_data(**hash[:data]) end if hash[:metadata].present? .( player: hash.dig(:metadata, :player), target: hash.dig(:metadata, :target) ) end .add_errors(hash[:errors]) if hash[:errors].present? end |
.from_string(json) ⇒ Self
Creates an instance of ESM::Message from JSON. See #to_h for the structure
13 14 15 |
# File 'lib/esm/message.rb', line 13 def self.from_string(json) from_hash(json.to_deep_h) end |
Instance Method Details
#add_error(type, content) ⇒ Object
Adds the provided error to this message
111 112 113 114 115 116 117 |
# File 'lib/esm/message.rb', line 111 def add_error(type, content) # Return nil on purpose return if type.nil? || content.nil? @errors << Error.new(self, type, content) self end |
#add_errors(errors = []) ⇒ Object
Each hash has the following attributes:
type [Symbol, String] The type of error. Valid options are:
code # Uses the message to look up a predefined message in the locales
message # Treats the message like a string and sends it as is
content [String] The content of this error.
96 97 98 99 100 101 102 103 |
# File 'lib/esm/message.rb', line 96 def add_errors(errors = []) errors.each do |error| error = error.symbolize_keys add_error(error[:type].to_s, error[:content]) end self end |
#data_attributes ⇒ Object
119 120 121 |
# File 'lib/esm/message.rb', line 119 def data_attributes @data.to_h end |
#error_messages ⇒ Object
171 172 173 |
# File 'lib/esm/message.rb', line 171 def errors.map(&:to_s) end |
#errors? ⇒ Boolean
Returns if there are any errors on this message
167 168 169 |
# File 'lib/esm/message.rb', line 167 def errors? errors.any? end |
#inspect ⇒ Object
175 176 177 |
# File 'lib/esm/message.rb', line 175 def inspect "#<ESM::Message #{JSON.pretty_generate(to_h)}>" end |
#set_data(**data_attributes) ⇒ Object
The primary data for this message. It's the good stuff.
68 69 70 71 |
# File 'lib/esm/message.rb', line 68 def set_data(**data_attributes) @data = Data.new(**data_attributes) self end |
#set_id(id) ⇒ Object
57 58 59 60 |
# File 'lib/esm/message.rb', line 57 def set_id(id) @id = id self end |
#set_metadata(player: nil, target: nil, server_id: nil) ⇒ Message
Sets various values used by the arma mod and internally by Message::Error
83 84 85 86 87 88 89 |
# File 'lib/esm/message.rb', line 83 def (player: nil, target: nil, server_id: nil) @metadata.player = Player.from(player) if player @metadata.target = Target.from(target) if target @metadata.server_id = server_id if server_id self end |
#set_type(type) ⇒ Object
62 63 64 65 |
# File 'lib/esm/message.rb', line 62 def set_type(type) @type = type.to_sym self end |
#to_h ⇒ Hash
Converts the message to a Hash
152 153 154 155 156 157 158 159 160 |
# File 'lib/esm/message.rb', line 152 def to_h { id: id, type: type, data: data_attributes, metadata: .to_h, errors: errors.map(&:to_h) } end |
#to_s ⇒ String
Converts the message to JSON
128 129 130 |
# File 'lib/esm/message.rb', line 128 def to_s to_h.to_json end |