
Security News
Astral Launches pyx: A Python-Native Package Registry
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
rack-time-zone-middleware
Advanced tools
Adding ability to detect timezone at UI side and get it within Rack/Rails via cookies with/o custom handler.
This gem created for usecase of loading detected TimeZone into Rails environment.
You can set cookie with TimeZone name at UI side(from Angular, React, Ember, Backbone or vanilla JS).
After that all XHR requests to your Rails/Rack backend can be identified by this Middleware.
In case when TimeZone name(s) is unsupported or key not found in cookies, middleware will fallback to defaults.
Add this line to your application's Gemfile:
gem 'rack-time-zone-middleware'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rack-time-zone-middleware
key_name
with options, wich includes detected time-zone.key_name
with options, where you store its value.env['key_name']
to access saved value from your application or controller.require 'rack/time-zone-middleware'
# Default TimeZone handler.
use Rack::TimeZoneMiddleware
# Configured TimeZone handler.
use Rack::TimeZoneMiddleware, default_tz: 'Europe/Moscow',
default_as_tz: 'Moscow',
time_zone_key: 'dummy.time_zone'
cookie_key: 'dummy.time_zone'
# Your own TimeZone handler. All options & instance methods is available through middleware parameter.
use Rack::TimeZoneMiddleware do |middleware, env|
request = ::Rack::Request.new(env)
time_zone = request&.cookies['dummy.time_zone'] || middleware.options[:default_tz]
env['dummy.time_zone'] = middleware.find_as_time_zone(time_zone)
middleware.app.call(env)
end
# Default TimeZone handler.
config.middleware.use Rack::TimeZoneMiddleware
# Configured TimeZone handler.
config.middleware.use Rack::TimeZoneMiddleware, default_tz: 'Europe/Moscow',
default_as_tz: 'Moscow',
time_zone_key: 'dummy.time_zone'
cookie_key: 'dummy.time_zone'
# Your own TimeZone handler. All options & instance methods is available through middleware parameter.
config.middleware.use Rack::TimeZoneMiddleware do |middleware, env|
request = ::Rack::Request.new(env)
time_zone = request&.cookies['dummy.time_zone'] || middleware.options[:default_tz]
env['dummy.time_zone'] = middleware.find_as_time_zone(time_zone)
middleware.app.call(env)
end
In theory you can setup dynamic TimeZones detector(when its hash is managed from your Application, from Admin panel or something),
but in most of realizations what i saw, it is overhead.
name | description |
---|---|
default_tz | optional , TimeZone name fallback value (default: 'Europe/Moscow') |
default_as_tz | optional , ActiveSupport::TimeZone key name fallback value (default: 'Moscow') |
cookie_key | optional , Cookie key name (default: 'dummy.time_zone') |
time_zone_key | optional , Environment key name (default: 'dummy.time_zone') |
time_zone_map | optional , TimeZone Hash or lambda , like {'Moscow' => 'Europe/Moscow'} . If not provided ActiveSupport TZInfo map will be tried. |
TimeZone updater factory example via JsTz
web.services.factory('JsTz', ['ipCookie', function(ipCookie) {
return {
updateCookie: function() {
tz = jstz.determine();
name = tz.name();
ipCookie('dummy.time_zone', name, { path: '/', expires: 21 });
return name;
}
};
}]);
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)Bug reports and pull requests are welcome on GitHub at https://github.com/merqlove/rack-time-zone-middleware.
$ rake test
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Copyright (c) 2016 Alexander Merkulov
MIT License
FAQs
Unknown package
We found that rack-time-zone-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.