Module: ActiveSupport::Messages::Metadata
Do not use. This module is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | activesupport/lib/active_support/messages/metadata.rb |
Constant Summary
-
ENVELOPE_SERIALIZERS =
# File 'activesupport/lib/active_support/messages/metadata.rb', line 12[ *SerializerWithFallback::SERIALIZERS.values, ActiveSupport::JSON, ::JSON, Marshal, ]
-
TIMESTAMP_SERIALIZERS =
# File 'activesupport/lib/active_support/messages/metadata.rb', line 19[ SerializerWithFallback::SERIALIZERS.fetch(: ), SerializerWithFallback::SERIALIZERS.fetch(: ), ]
Instance Attribute Summary
Instance Method Summary
- #deserialize_from_json(serialized) private
- #deserialize_from_json_safe_string(string) private
- #deserialize_with_metadata(message, **expected_metadata) private
- #dual_serialized_metadata_envelope_json?(string) ⇒ Boolean private
- #extract_from_metadata_envelope(envelope, purpose: nil) private
- #metadata_envelope?(object) ⇒ Boolean private
- #parse_expiry(expires_at) private
- #pick_expiry(expires_at, expires_in) private
- #serialize_to_json(data) private
- #serialize_to_json_safe_string(data) private
- #serialize_with_metadata(data, **metadata) private
- #wrap_in_metadata_envelope(hash, expires_at: nil, expires_in: nil, purpose: nil) private
- #wrap_in_metadata_legacy_envelope(hash, expires_at: nil, expires_in: nil, purpose: nil) private
Instance Attribute Details
#use_message_serializer_for_metadata (rw)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 10
singleton_class.attr_accessor :
#use_message_serializer_for_metadata? ⇒ Boolean
(rw, private)
[ GitHub ]
# File 'activesupport/lib/active_support/messages/metadata.rb', line 60
def Metadata. && Metadata::ENVELOPE_SERIALIZERS.include?(serializer) end
Instance Method Details
#deserialize_from_json(serialized) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 128
def deserialize_from_json(serialized) ActiveSupport::JSON.decode(serialized) rescue ::JSON::ParserError => error # Throw :invalid_message_format instead of :invalid_message_serialization # because here a parse error is due to a bad message rather than an # incompatible `self.serializer`. throw :, error end
#deserialize_from_json_safe_string(string) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 141
def deserialize_from_json_safe_string(string) deserialize(decode(string, url_safe: false)) end
#deserialize_with_metadata(message, **expected_metadata) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 43
def (, ** ) if ( ) envelope = deserialize_from_json( ) extracted = (envelope, ** ) deserialize_from_json_safe_string(extracted["message"]) else deserialized = deserialize( ) if (deserialized) (deserialized, ** )["data"] elsif .none? { |k, v| v } deserialized else throw :, "missing metadata" end end end
#dual_serialized_metadata_envelope_json?(string) ⇒ Boolean
(private)
# File 'activesupport/lib/active_support/messages/metadata.rb', line 96
def (string) string.start_with?('{"_rails":{"message":') end
#extract_from_metadata_envelope(envelope, purpose: nil) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 78
def (envelope, purpose: nil) hash = envelope["_rails"] if hash["exp"] && Time.now.utc >= parse_expiry(hash["exp"]) throw :, "expired" end if hash["pur"].to_s != purpose.to_s throw :, "mismatched purpose" end hash end
#metadata_envelope?(object) ⇒ Boolean
(private)
# File 'activesupport/lib/active_support/messages/metadata.rb', line 92
def (object) object.is_a?(Hash) && object.key?("_rails") end
#parse_expiry(expires_at) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 114
def parse_expiry(expires_at) if !expires_at.is_a?(String) expires_at elsif ActiveSupport.use_standard_json_time_format Time.iso8601(expires_at) else Time.parse(expires_at) end end
#pick_expiry(expires_at, expires_in) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 100
def pick_expiry(expires_at, expires_in) expiry = if expires_at expires_at.utc elsif expires_in Time.now.utc.advance(seconds: expires_in) end unless Metadata::TIMESTAMP_SERIALIZERS.include?(serializer) expiry = expiry&.iso8601(3) end expiry end
#serialize_to_json(data) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 124
def serialize_to_json(data) ActiveSupport::JSON.encode(data) end
#serialize_to_json_safe_string(data) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 137
def serialize_to_json_safe_string(data) encode(serialize(data), url_safe: false) end
#serialize_with_metadata(data, **metadata) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 30
def (data, ** ) = .any? { |k, v| v } if && ! data_string = serialize_to_json_safe_string(data) envelope = ({ "message" => data_string }, ** ) serialize_to_json(envelope) else data = ({ "data" => data }, ** ) if serialize(data) end end
#wrap_in_metadata_envelope(hash, expires_at: nil, expires_in: nil, purpose: nil) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 64
def (hash, expires_at: nil, expires_in: nil, purpose: nil) expiry = pick_expiry(expires_at, expires_in) hash["exp"] = expiry if expiry hash["pur"] = purpose.to_s if purpose { "_rails" => hash } end
#wrap_in_metadata_legacy_envelope(hash, expires_at: nil, expires_in: nil, purpose: nil) (private)
[ GitHub ]# File 'activesupport/lib/active_support/messages/metadata.rb', line 71
def (hash, expires_at: nil, expires_in: nil, purpose: nil) expiry = pick_expiry(expires_at, expires_in) hash["exp"] = expiry hash["pur"] = purpose { "_rails" => hash } end