Class: Rack::TimeZoneMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/time-zone-middleware.rb,
lib/rack/time-zone-middleware/version.rb

Constant Summary collapse

DEFAULT_TIME_ZONE =
'Europe/Moscow'
DEFAULT_AS_TIME_ZONE =
'Moscow'
DEFAULT_KEY =
'dummy.time_zone'
DEFAULT_KEY
MAP_WARNING =
"Please install `activesupport` gem and `require 'active_support/values/time_zone'` or provide custom TimeZone map, as a `Hash` like `{'Moscow' => 'Europe/Moscow'}`"
VERSION =
'0.1.4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}, &block) ⇒ TimeZoneMiddleware

Returns a new instance of TimeZoneMiddleware.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/time-zone-middleware.rb', line 14

def initialize(app, opts = {}, &block)
  @app = app

  @options = {}
  options[:default_tz]    = opts.fetch(:default_tz, DEFAULT_TIME_ZONE)
  options[:default_as_tz] = opts.fetch(:default_as_tz, DEFAULT_AS_TIME_ZONE)
  options[:time_zone_key] = opts.fetch(:time_zone_key, DEFAULT_KEY)
  options[:cookie_key]    = opts.fetch(:cookie_key, DEFAULT_COOKIE_KEY)

  @time_zone_map = opts.fetch(:time_zone_map, nil) || default_time_zone_map

  if block_given?
    @runner = block
  else
    @runner = ->(mw, env) { _call(mw, env) }
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



12
13
14
# File 'lib/rack/time-zone-middleware.rb', line 12

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/rack/time-zone-middleware.rb', line 12

def options
  @options
end

#runnerObject (readonly)

Returns the value of attribute runner.



12
13
14
# File 'lib/rack/time-zone-middleware.rb', line 12

def runner
  @runner
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
# File 'lib/rack/time-zone-middleware.rb', line 32

def call(env)
  runner.call(self, env)
end

#find_as_time_zone(name) ⇒ Object



36
37
38
39
40
41
# File 'lib/rack/time-zone-middleware.rb', line 36

def find_as_time_zone(name)
  zone_name, _ = time_zone_map.detect { |_, v| v.eql? name }
  zone_name || options[:default_as_tz]
rescue
  options[:default_as_tz]
end