lib/timber/contexts/session.rb in timber-2.5.1 vs lib/timber/contexts/session.rb in timber-2.6.0.pre.beta1
- old
+ new
@@ -9,20 +9,29 @@
# story of how many time a user has engaged your site.
#
# @note This is tracked automatically with the {Integrations::Rack::SessionContext} rack
# middleware.
class Session < Context
+ ID_MAX_BYTES = 256.freeze
+
@keyspace = :session
attr_reader :id
def initialize(attributes)
- @id = Timber::Util::Object.try(attributes[:id], :to_s) || raise(ArgumentError.new(":id is required"))
+ normalizer = Util::AttributeNormalizer.new(attributes)
+ @id = normalizer.fetch!(:id, :string, :limit => ID_MAX_BYTES)
end
# Builds a hash representation containing simple objects, suitable for serialization (JSON).
+ def to_hash
+ @to_hash ||= Util::NonNilHashBuilder.build do |h|
+ h.add(:id, id)
+ end
+ end
+
def as_json(_options = {})
- {id: id}
+ to_hash
end
end
end
end
\ No newline at end of file