Class: AMQP::Client::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp/client/properties.rb

Overview

Encode/decode AMQP Properties

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type: nil, content_encoding: nil, headers: nil, delivery_mode: nil, priority: nil, correlation_id: nil, reply_to: nil, expiration: nil, message_id: nil, timestamp: nil, type: nil, user_id: nil, app_id: nil) ⇒ Properties

rubocop:disable Metrics/ParameterLists



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/amqp/client/properties.rb', line 10

def initialize(content_type: nil, content_encoding: nil, headers: nil, delivery_mode: nil,
               priority: nil, correlation_id: nil, reply_to: nil, expiration: nil,
               message_id: nil, timestamp: nil, type: nil, user_id: nil, app_id: nil)
  @content_type = content_type
  @content_encoding = content_encoding
  @headers = headers
  @delivery_mode = delivery_mode
  @priority = priority
  @correlation_id = correlation_id
  @reply_to = reply_to
  @expiration = expiration
  @message_id = message_id
  @timestamp = timestamp
  @type = type
  @user_id = user_id
  @app_id = app_id
end

Instance Attribute Details

#app_idString?

Name of application that generated the message

Returns:

  • (String, nil)


100
101
102
# File 'lib/amqp/client/properties.rb', line 100

def app_id
  @app_id
end

#content_encodingString?

Content encoding of the body

Returns:

  • (String, nil)


64
65
66
# File 'lib/amqp/client/properties.rb', line 64

def content_encoding
  @content_encoding
end

#content_typeString?

Content type of the message body

Returns:

  • (String, nil)


61
62
63
# File 'lib/amqp/client/properties.rb', line 61

def content_type
  @content_type
end

#correlation_idString?

Message correlation id, commonly used to correlate RPC requests and responses

Returns:

  • (String, nil)


79
80
81
# File 'lib/amqp/client/properties.rb', line 79

def correlation_id
  @correlation_id
end

#delivery_mode1, ...

Note:

The exchange and queue have to durable as well for the message to be persistent

Message persistent level

Returns:

  • (1)

    Transient message

  • (2)

    Persistent message

  • (nil)

    Not specified (implicitly transient)



73
74
75
# File 'lib/amqp/client/properties.rb', line 73

def delivery_mode
  @delivery_mode
end

#expirationString?

Number of seconds the message will stay in the queue

Returns:

  • (String, nil)


85
86
87
# File 'lib/amqp/client/properties.rb', line 85

def expiration
  @expiration
end

#headersHash<String, Object>?

Headers, for applications and header exchange routing

Returns:

  • (Hash<String, Object>, nil)


67
68
69
# File 'lib/amqp/client/properties.rb', line 67

def headers
  @headers
end

#message_idString?

Application message identifier

Returns:

  • (String, nil)


88
89
90
# File 'lib/amqp/client/properties.rb', line 88

def message_id
  @message_id
end

#priorityInteger?

A priority of the message (between 0 and 255)

Returns:

  • (Integer, nil)


76
77
78
# File 'lib/amqp/client/properties.rb', line 76

def priority
  @priority
end

#reply_toString?

Queue to reply RPC responses to

Returns:

  • (String, nil)


82
83
84
# File 'lib/amqp/client/properties.rb', line 82

def reply_to
  @reply_to
end

#timestampDate?

Message timestamp, often indicates when the message was originally generated

Returns:

  • (Date, nil)


91
92
93
# File 'lib/amqp/client/properties.rb', line 91

def timestamp
  @timestamp
end

#typeString?

Message type name

Returns:

  • (String, nil)


94
95
96
# File 'lib/amqp/client/properties.rb', line 94

def type
  @type
end

#user_idString?

The user that published the message

Returns:

  • (String, nil)


97
98
99
# File 'lib/amqp/client/properties.rb', line 97

def user_id
  @user_id
end

Class Method Details

.decode(bytes, pos = 0) ⇒ Properties

Decode a byte array

Returns:



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/amqp/client/properties.rb', line 224

def self.decode(bytes, pos = 0)
  p = new
  flags = bytes.byteslice(pos, 2).unpack1("S>")
  pos += 2
  if (flags & 0x8000).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.content_type = bytes.byteslice(pos, len).force_encoding("utf-8")
    pos += len
  end
  if (flags & 0x4000).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.content_encoding = bytes.byteslice(pos, len).force_encoding("utf-8")
    pos += len
  end
  if (flags & 0x2000).positive?
    len = bytes.byteslice(pos, 4).unpack1("L>")
    pos += 4
    p.headers = Table.decode(bytes.byteslice(pos, len))
    pos += len
  end
  if (flags & 0x1000).positive?
    p.delivery_mode = bytes.getbyte(pos)
    pos += 1
  end
  if (flags & 0x0800).positive?
    p.priority = bytes.getbyte(pos)
    pos += 1
  end
  if (flags & 0x0400).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.correlation_id = bytes.byteslice(pos, len).force_encoding("utf-8")
    pos += len
  end
  if (flags & 0x0200).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.reply_to = bytes.byteslice(pos, len).force_encoding("utf-8")
    pos += len
  end
  if (flags & 0x0100).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.expiration = bytes.byteslice(pos, len).force_encoding("utf-8")
    pos += len
  end
  if (flags & 0x0080).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.message_id = bytes.byteslice(pos, len).force_encoding("utf-8")
    pos += len
  end
  if (flags & 0x0040).positive?
    p.timestamp = Time.at(bytes.byteslice(pos, 8).unpack1("Q>"))
    pos += 8
  end
  if (flags & 0x0020).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.type = bytes.byteslice(pos, len).force_encoding("utf-8")
    pos += len
  end
  if (flags & 0x0010).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.user_id = bytes.byteslice(pos, len).force_encoding("utf-8")
    pos += len
  end
  if (flags & 0x0008).positive?
    len = bytes.getbyte(pos)
    pos += 1
    p.app_id = bytes.byteslice(pos, len).force_encoding("utf-8")
  end
  p
end

.encode(properties) ⇒ String

Encode properties into a byte array

Parameters:

  • properties (Hash)

Returns:

  • (String)

    byte array



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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/amqp/client/properties.rb', line 105

def self.encode(properties)
  return "\x00\x00" if properties.empty?

  flags = 0
  arr = [flags]
  fmt = String.new("S>", capacity: 37)

  if (content_type = properties[:content_type])
    content_type.is_a?(String) || raise(ArgumentError, "content_type must be a string")

    flags |= (1 << 15)
    arr << content_type.bytesize << content_type
    fmt << "Ca*"
  end

  if (content_encoding = properties[:content_encoding])
    content_encoding.is_a?(String) || raise(ArgumentError, "content_encoding must be a string")

    flags |= (1 << 14)
    arr << content_encoding.bytesize << content_encoding
    fmt << "Ca*"
  end

  if (headers = properties[:headers])
    headers.is_a?(Hash) || raise(ArgumentError, "headers must be a hash")

    flags |= (1 << 13)
    tbl = Table.encode(headers)
    arr << tbl.bytesize << tbl
    fmt << "L>a*"
  end

  if (delivery_mode = properties[:delivery_mode])
    delivery_mode.is_a?(Integer) || raise(ArgumentError, "delivery_mode must be an int")

    flags |= (1 << 12)
    arr << delivery_mode
    fmt << "C"
  end

  if (priority = properties[:priority])
    priority.is_a?(Integer) || raise(ArgumentError, "priority must be an int")

    flags |= (1 << 11)
    arr << priority
    fmt << "C"
  end

  if (correlation_id = properties[:correlation_id])
    correlation_id.is_a?(String) || raise(ArgumentError, "correlation_id must be a string")

    flags |= (1 << 10)
    arr << correlation_id.bytesize << correlation_id
    fmt << "Ca*"
  end

  if (reply_to = properties[:reply_to])
    reply_to.is_a?(String) || raise(ArgumentError, "reply_to must be a string")

    flags |= (1 << 9)
    arr << reply_to.bytesize << reply_to
    fmt << "Ca*"
  end

  if (expiration = properties[:expiration])
    expiration = expiration.to_s if expiration.is_a?(Integer)
    expiration.is_a?(String) || raise(ArgumentError, "expiration must be a string or integer")

    flags |= (1 << 8)
    arr << expiration.bytesize << expiration
    fmt << "Ca*"
  end

  if (message_id = properties[:message_id])
    message_id.is_a?(String) || raise(ArgumentError, "message_id must be a string")

    flags |= (1 << 7)
    arr << message_id.bytesize << message_id
    fmt << "Ca*"
  end

  if (timestamp = properties[:timestamp])
    timestamp.is_a?(Integer) || timestamp.is_a?(Time) || raise(ArgumentError, "timestamp must be an Integer or a Time")

    flags |= (1 << 6)
    arr << timestamp.to_i
    fmt << "Q>"
  end

  if (type = properties[:type])
    type.is_a?(String) || raise(ArgumentError, "type must be a string")

    flags |= (1 << 5)
    arr << type.bytesize << type
    fmt << "Ca*"
  end

  if (user_id = properties[:user_id])
    user_id.is_a?(String) || raise(ArgumentError, "user_id must be a string")

    flags |= (1 << 4)
    arr << user_id.bytesize << user_id
    fmt << "Ca*"
  end

  if (app_id = properties[:app_id])
    app_id.is_a?(String) || raise(ArgumentError, "app_id must be a string")

    flags |= (1 << 3)
    arr << app_id.bytesize << app_id
    fmt << "Ca*"
  end

  arr[0] = flags
  arr.pack(fmt)
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if two Property objects holds the same information

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/amqp/client/properties.rb', line 53

def ==(other)
  return false unless other.is_a? self.class

  instance_variables.all? { |v| instance_variable_get(v) == other.instance_variable_get(v) }
end

#to_hHash Also known as: to_hash

Properties as a Hash

Returns:

  • (Hash)

    Properties



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/amqp/client/properties.rb', line 31

def to_h
  {
    content_type: content_type,
    content_encoding: content_encoding,
    headers: headers,
    delivery_mode: delivery_mode,
    priority: priority,
    correlation_id: correlation_id,
    reply_to: reply_to,
    expiration: expiration,
    message_id: message_id,
    timestamp: timestamp,
    type: type,
    user_id: user_id,
    app_id: app_id
  }
end