Class: ESM::Message

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



49
50
51
52
53
54
55
# File 'lib/esm/message.rb', line 49

def initialize
  @id = SecureRandom.uuid
  @type = :call
  @data = Data.new
  @metadata = Metadata.new
  @errors = []
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



46
47
48
# File 'lib/esm/message.rb', line 46

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



46
47
48
# File 'lib/esm/message.rb', line 46

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



46
47
48
# File 'lib/esm/message.rb', line 46

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



46
47
48
# File 'lib/esm/message.rb', line 46

def 
  @metadata
end

#typeObject (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

Parameters:

  • hash (Hash)

    The hash that contains the message contents

Returns:

  • (Self)

    The message



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

  message = new
  message.set_id(hash[:id]) if hash[:id].present?
  message.set_type(hash[:type]) if hash[:type].present?

  if hash[:data].present?
    message.set_data(**hash[:data])
  end

  if hash[:metadata].present?
    message.(
      player: hash.dig(:metadata, :player),
      target: hash.dig(:metadata, :target)
    )
  end

  message.add_errors(hash[:errors]) if hash[:errors].present?
  message
end

.from_string(json) ⇒ Self

Creates an instance of ESM::Message from JSON. See #to_h for the structure

Parameters:

  • json (String)

    The JSON to parse

Returns:

  • (Self)

See Also:



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

Parameters:

  • type (String)

    The error type. Valid types are: "code" and "message"

  • message (String)

    The message or code for this error



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_attributesObject



119
120
121
# File 'lib/esm/message.rb', line 119

def data_attributes
  @data.to_h
end

#error_messagesObject



171
172
173
# File 'lib/esm/message.rb', line 171

def error_messages
  errors.map(&:to_s)
end

#errors?Boolean

Returns if there are any errors on this message

Returns:

  • (Boolean)


167
168
169
# File 'lib/esm/message.rb', line 167

def errors?
  errors.any?
end

#inspectObject



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

Parameters:

  • player (ESM::User, nil) (defaults to: nil)

    The user that executed the command

  • target (ESM::User, ESM::User::Ephemeral, nil) (defaults to: nil)

    The user who is the target of this command

  • server_id (String, nil) (defaults to: nil)

    The server the command is being executed on Used for error messages

Returns:

  • (Message)

    A referenced to the modified message



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_hHash

Converts the message to a Hash

Parameters:

  • for_arma (Boolean)

    Is this hash arma bound?

Returns:

  • (Hash)

    The message as a Hash. It has the following keys { id: The ID of this message as a UUID type: The context of this message data: { type: Describes the structure of content content: The actual "data" }, metadata: { type: Describes the structure of content content: The actual "metadata" }, errors: Any errors associated to this message }



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_sString

Converts the message to JSON

Returns:

  • (String)

    The message as JSON



128
129
130
# File 'lib/esm/message.rb', line 128

def to_s
  to_h.to_json
end