Class: Lux::Current
- Defined in:
- lib/lux/current/current.rb,
lib/lux/current/lib/session.rb,
lib/lux/current/lib/encrypt_params.rb
Defined Under Namespace
Modules: EncryptParams Classes: Session
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.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#error ⇒ Object
Returns the value of attribute error.
-
#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.
-
#var ⇒ Object
readonly
Returns the value of attribute var.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, val) ⇒ Object
-
#cache(key) ⇒ Object
Cache data in scope of current request.
- #decrypt(token, opts = {}) ⇒ Object
-
#delay(*args) ⇒ Object
Thread.new but copies env to a thread.
- #encrypt(data, opts = {}) ⇒ Object
-
#files_in_use(file = nil) ⇒ Object
Add to list of files in use.
-
#host ⇒ Object
Full host with port.
-
#initialize(env = nil, opts = {}) ⇒ Current
constructor
A new instance of Current.
- #ip ⇒ Object
-
#no_cache?(shallow_check = false) ⇒ Boolean
Set Lux.current.can_clear_cache = true in production for admins.
-
#once(id = nil) ⇒ Object
Execute action once per page.
- #robot? ⇒ Boolean
-
#secure_token(token = nil) ⇒ Object
Get or check current session secure token.
-
#uid(num_only = false) ⇒ Object
Generete unique ID par page render current.uid => “uid_123_1668273316128” current.uid(true) => 123.
Constructor Details
#initialize(env = nil, opts = {}) ⇒ Current
Returns a new instance of Current.
9 10 11 12 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 |
# File 'lib/lux/current/current.rb', line 9 def initialize env = nil, opts = {} @env = env || '/mock' @env = ::Rack::MockRequest.env_for(env) if env.is_a?(String) @request = ::Rack::Request.new @env # fix params if defined if opts.keys.length > 0 opts = opts.to_hwia :params, :post, :method, :session, :cookies, :query_string if opts[:post] opts[:method] = 'POST' opts[:params] = opts[:post] end end # reset page cache Thread.current[:lux] = self # overload request method @request.env['REQUEST_METHOD'] = opts[:method].to_s.upcase if opts[:method] # set cookies @request..merge opts[:cookies] if opts[:cookies] prepare_params opts # base vars @files_in_use = [] @response = Lux::Response.new @session = Lux::Current::Session.new @request @nav = Lux::Application::Nav.new @request @var = {}.to_hwia opts[:session].or({}).each {|k,v| @session[k] = v } 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
4 5 6 |
# File 'lib/lux/current/current.rb', line 4 def can_clear_cache @can_clear_cache end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
7 8 9 |
# File 'lib/lux/current/current.rb', line 7 def env @env end |
#error ⇒ Object
Returns the value of attribute error.
6 7 8 |
# File 'lib/lux/current/current.rb', line 6 def error @error end |
#locale ⇒ Object
Returns the value of attribute locale.
6 7 8 |
# File 'lib/lux/current/current.rb', line 6 def locale @locale end |
#nav ⇒ Object (readonly)
Returns the value of attribute nav.
7 8 9 |
# File 'lib/lux/current/current.rb', line 7 def nav @nav end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
7 8 9 |
# File 'lib/lux/current/current.rb', line 7 def params @params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
7 8 9 |
# File 'lib/lux/current/current.rb', line 7 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/lux/current/current.rb', line 7 def response @response end |
#session ⇒ Object
Returns the value of attribute session.
6 7 8 |
# File 'lib/lux/current/current.rb', line 6 def session @session end |
#var ⇒ Object (readonly)
Returns the value of attribute var.
7 8 9 |
# File 'lib/lux/current/current.rb', line 7 def var @var end |
Instance Method Details
#[](name) ⇒ Object
45 46 47 |
# File 'lib/lux/current/current.rb', line 45 def [] name @var[name] end |
#[]=(name, val) ⇒ Object
49 50 51 |
# File 'lib/lux/current/current.rb', line 49 def []= name, val @var[name] = val end |
#cache(key) ⇒ Object
Cache data in scope of current request
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lux/current/current.rb', line 59 def cache key root = @var[:cache] ||= {} data = root[key] # it is array ref because we want to cache nil results too unless data data = [yield] root[key] = data end data[0] end |
#decrypt(token, opts = {}) ⇒ Object
170 171 172 173 |
# File 'lib/lux/current/current.rb', line 170 def decrypt token, opts={} opts[:password] ||= self.ip Crypt.decrypt(token, opts) end |
#delay(*args) ⇒ Object
Thread.new but copies env to a thread
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/lux/current/current.rb', line 138 def delay *args if block_given? lux_env = self.dup Thread.new do begin Thread.current[:lux] = lux_env Timeout::timeout(Lux.config.delay_timeout) do yield *args end rescue => e Lux.error.log e Lux.log ['Lux.current.delay error: %s' % e., e.backtrace].join($/) end end else raise ArgumentError, 'Block not given' end end |
#encrypt(data, opts = {}) ⇒ Object
164 165 166 167 168 |
# File 'lib/lux/current/current.rb', line 164 def encrypt data, opts={} opts[:password] ||= self.ip opts[:ttl] ||= 10.minutes Crypt.encrypt(data, opts) end |
#files_in_use(file = nil) ⇒ Object
Add to list of files in use
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/lux/current/current.rb', line 122 def files_in_use file = nil return @files_in_use unless file return unless file.class == String file = file.sub './', '' if @files_in_use.include?(file) true else @files_in_use.push file yield(file) if block_given? false end end |
#host ⇒ Object
Full host with port
54 55 56 |
# File 'lib/lux/current/current.rb', line 54 def host "#{request.env['rack.url_scheme']}://#{request.host}:#{request.port}".sub(':80','')# rescue 'http://locahost:3000' end |
#ip ⇒ Object
157 158 159 160 161 162 |
# File 'lib/lux/current/current.rb', line 157 def ip request.env['HTTP_CF_CONNECTING_IP'] || # will not work with cloudflare if removed request.env['HTTP_X_FORWARDED_FOR'] || request.env['REMOTE_ADDR'] || '127.0.0.1' end |
#no_cache?(shallow_check = false) ⇒ Boolean
Set Lux.current.can_clear_cache = true in production for admins
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/lux/current/current.rb', line 72 def no_cache? shallow_check = false check = @request.env['HTTP_CACHE_CONTROL'].to_s.downcase == 'no-cache' if check if shallow_check || Lux.env.no_cache? true else can_clear_cache ? true : false end else false end end |
#once(id = nil) ⇒ Object
Execute action once per page
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lux/current/current.rb', line 87 def once id = nil id ||= Digest::SHA1.hexdigest caller[0] @once_hash ||= {} return false if @once_hash[id] @once_hash[id] = true if block_given? yield || true else true end end |
#robot? ⇒ Boolean
116 117 118 119 |
# File 'lib/lux/current/current.rb', line 116 def robot? ua = request.env['HTTP_USER_AGENT'].to_s.downcase ua.include?('wget/') || ua.include?('curl/') end |
#secure_token(token = nil) ⇒ Object
Get or check current session secure token
111 112 113 114 |
# File 'lib/lux/current/current.rb', line 111 def secure_token token = nil generated = Crypt.sha1(self.ip) token ? (generated == token) : generated end |
#uid(num_only = false) ⇒ Object
Generete unique ID par page render current.uid => “uid_123_1668273316128” current.uid(true) => 123
104 105 106 107 108 |
# File 'lib/lux/current/current.rb', line 104 def uid num_only = false Thread.current[:lux][:uid_cnt] ||= 0 num = Thread.current[:lux][:uid_cnt] += 1 num_only ? num : "uid_#{num}_#{(Time.now.to_f*1000).to_i}" end |