Class: Lux::Current
Defined Under Namespace
Modules: EncryptParams Classes: StaticFile
Instance Attribute Summary collapse
-
#can_clear_cache ⇒ Object
set to true if user is admin and you want him to be able to clear caches in production.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#nav ⇒ Object
readonly
Returns the value of attribute nav.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#cache(key) ⇒ Object
cache data in current page.
- #domain ⇒ Object
- #files_in_use(file = nil) ⇒ Object
- #host ⇒ Object
-
#initialize(env = nil) ⇒ Current
constructor
A new instance of Current.
-
#no_cache? ⇒ Boolean
set current.can_clear_cache = true in production for admins.
-
#once(id = nil, data = nil, &block) ⇒ Object
execute action once per page.
- #redirect(*args) ⇒ Object
- #set_and_check_client_unique_hash ⇒ Object
- #uid ⇒ Object
- #var ⇒ Object
Constructor Details
#initialize(env = nil) ⇒ Current
Returns a new instance of Current.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lux/current/current.rb', line 13 def initialize env=nil env ||= '/mock' env = ::Rack::MockRequest.env_for(env) if env.class == String request = ::Rack::Request.new env # reset page cache Thread.current[:lux] = { cache:{}, page: self } @files_in_use = [] @response = Lux::Response.new @request = request @cookies = {} @session = {} for in request.env['HTTP_COOKIE'].to_s.split(/;\s*/).map{ |el| el.split('=',2) } @cookies[[0]] = [1] end @session = JSON.parse(Crypt.decrypt(@cookies['__luxs'] || '{}')) rescue {} # check for session if Lux.dev? && request.env['HTTP_REFERER'] && request.env['HTTP_REFERER'].index(request.host) && @session.keys.length == 0 puts "ERROR: There is no session set!".red end # hard sec, bind session to user agent and IP set_and_check_client_unique_hash @session = HashWithIndifferentAccess.new(@session) ap request.params if request.post? && Lux.config(:log_to_stdout) @params = request.params.h_wia Lux::Current::EncryptParams.decrypt @params @nav = Lux::Application::Nav.new request end |
Instance Attribute Details
#can_clear_cache ⇒ Object
set to true if user is admin and you want him to be able to clear caches in production
8 9 10 |
# File 'lib/lux/current/current.rb', line 8 def can_clear_cache @can_clear_cache end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
11 12 13 |
# File 'lib/lux/current/current.rb', line 11 def @cookies end |
#locale ⇒ Object
Returns the value of attribute locale.
10 11 12 |
# File 'lib/lux/current/current.rb', line 10 def locale @locale end |
#nav ⇒ Object (readonly)
Returns the value of attribute nav.
11 12 13 |
# File 'lib/lux/current/current.rb', line 11 def nav @nav end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'lib/lux/current/current.rb', line 11 def params @params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
11 12 13 |
# File 'lib/lux/current/current.rb', line 11 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
11 12 13 |
# File 'lib/lux/current/current.rb', line 11 def response @response end |
#session ⇒ Object
Returns the value of attribute session.
10 11 12 |
# File 'lib/lux/current/current.rb', line 10 def session @session end |
Instance Method Details
#cache(key) ⇒ Object
cache data in current page
86 87 88 89 90 |
# File 'lib/lux/current/current.rb', line 86 def cache key data = Thread.current[:lux][:cache][key] return data if data Thread.current[:lux][:cache][key] = yield end |
#domain ⇒ Object
70 71 72 73 74 75 |
# File 'lib/lux/current/current.rb', line 70 def domain host = Lux.current.request.host.split('.') host_country = host.pop host_name = host.pop host_name ? "#{host_name}.#{host_country}" : host_country end |
#files_in_use(file = nil) ⇒ Object
51 52 53 54 |
# File 'lib/lux/current/current.rb', line 51 def files_in_use file=nil @files_in_use.push file if file && !@files_in_use.include?(file) @files_in_use end |
#host ⇒ Object
77 78 79 |
# File 'lib/lux/current/current.rb', line 77 def host "#{request.env['rack.url_scheme']}://#{request.host}:#{request.port}".sub(':80','')# rescue 'http://locahost:3000' end |
#no_cache? ⇒ Boolean
set current.can_clear_cache = true in production for admins
93 94 95 96 |
# File 'lib/lux/current/current.rb', line 93 def no_cache? @can_clear_cache ||= true if Lux.dev? (@can_clear_cache && @request.env['HTTP_CACHE_CONTROL'].to_s.downcase == 'no-cache') ? true : false end |
#once(id = nil, data = nil, &block) ⇒ Object
execute action once per page
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/lux/current/current.rb', line 103 def once id=nil, data=nil, &block id ||= Digest::SHA1.hexdigest caller[0] if block @once_hash ||= {} return @once_hash[id] if @once_hash.key?(id) @once_hash[id] = if block_given? yield else data || true end end |
#redirect(*args) ⇒ Object
98 99 100 |
# File 'lib/lux/current/current.rb', line 98 def redirect *args response.redirect *args end |
#set_and_check_client_unique_hash ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lux/current/current.rb', line 56 def set_and_check_client_unique_hash key = '_c' check = Crypt.sha1(@request.ip.to_s+@request.env['HTTP_USER_AGENT'].to_s)[0,10] # force type array @session.delete(key) unless @session[key].class == Array # allow 5 mins delay for IP change @session = {} if @session[key] && (@session[key][0] != check && @session[key][1].to_i < Time.now.to_i - Lux.config.session_forced_validity) # add new time stamp to every request @session[key] = [check, Time.now.to_i] end |
#uid ⇒ Object
116 117 118 119 |
# File 'lib/lux/current/current.rb', line 116 def uid Thread.current[:uid_cnt] ||= 0 "uid-#{Thread.current[:uid_cnt]+=1}" end |
#var ⇒ Object
81 82 83 |
# File 'lib/lux/current/current.rb', line 81 def var Thread.current[:lux][:var] ||= Hashie::Mash.new end |