lux-fw 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48df51990bdf390ef6f780d624e0bb1624f54dfc5721dee1c77749f02e2ec472
4
- data.tar.gz: bde0c404e918b91825e90b9e7a183199266d225ece56c54b28e1366366350bc7
3
+ metadata.gz: 7f17bf48ba9c67072d7a9cba132b0c8c29fb25f79bc424e39f0d4bc39b16981e
4
+ data.tar.gz: ea0a19540ee7c21cebea07b6ea6ad27e967a448d1865a51097b7fe22f305e244
5
5
  SHA512:
6
- metadata.gz: fc6c0f8bf80608399dac5afe4dd9f7d01d78fd99cbcf45e3cf746e986eabd8a08ba6650722569c6e0f64266fd202a27b44ec6e6e3cfdf8683951605bb03b030f
7
- data.tar.gz: 106570f909cc6109a9d1f6f7f719d82e4b7514e4b214b62fcc81015590c7e607bf39fbb97c74fe2e1add9986ca260c840743c3f1121b751ef81372c789e32327
6
+ metadata.gz: a286aecb1572cb1b7929449c914f4bed7faae1b199c63e663300eae36a5a58d40cb3c8480a59974d8a74a1dbc2a3a2269130f070bd2e3eafc6cf5dc64e4729f0
7
+ data.tar.gz: 4490adeaa98e7ac83bf413511baa383f152ded339ee0fa8c97d29cab141653da1eea76059f210296b4c3e465259e484c68c20bd7d172fe94420c4c9863b688cd
data/.version CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.3
@@ -21,34 +21,36 @@ module ClassCallbacks
21
21
  @@pointers = {}
22
22
 
23
23
  def add klass, unique_id, action, method
24
- key = Crypt.md5(unique_id)
24
+ klass = klass.to_s
25
+ key = Crypt.sha1(unique_id)
26
+
25
27
  @@pointers[key] = method
26
- @@methods[klass.to_s] ||= {}
27
- @@methods[klass.to_s][action] ||= []
28
- @@methods[klass.to_s][action].push(key) unless @@methods[klass.to_s][action].index(key)
28
+
29
+ @@methods[klass] ||= {}
30
+ @@methods[klass][action] ||= []
31
+ @@methods[klass][action].tap { |it| it.push(key) unless it.include?(key) }
29
32
  end
30
33
 
31
34
  def execute instance_object, action, object=nil
32
35
  object ? instance_object.send(action, object) : instance_object.send(action)
33
36
 
34
- # execute for self and parent
35
- for name in instance_object.class.ancestors.reverse.map(&:to_s)
36
- next if name == 'Object'
37
-
38
- if @@methods[name] && @@methods[name][action]
39
- for el in @@methods[name][action].map { |o| @@pointers[o] }
40
- if el.kind_of?(Symbol)
41
- object ? instance_object.send(el, object) : instance_object.send(el)
42
- else
43
- object ? instance_object.instance_exec(object, &el) : instance_object.instance_exec(&el)
44
- end
37
+ # execute for self and parents
38
+ instance_object.class.ancestors.reverse.map(&:to_s).each do |name|
39
+ next if name == 'Object'
40
+ next unless actions = @@methods.dig(name, action)
41
+
42
+ for el in actions.map { |o| @@pointers[o] }
43
+ if el.kind_of?(Symbol)
44
+ object ? instance_object.send(el, object) : instance_object.send(el)
45
+ else
46
+ object ? instance_object.instance_exec(object, &el) : instance_object.instance_exec(&el)
45
47
  end
46
48
  end
47
49
  end
48
50
  end
49
51
 
50
52
  def define(klass, *args)
51
- for action in args
53
+ args.each do |action|
52
54
  klass.class_eval %[
53
55
  def #{action}(duck=nil)
54
56
  end
@@ -8,7 +8,8 @@ class Lux::Application
8
8
 
9
9
  ###
10
10
 
11
- def initialize
11
+ def initialize current
12
+ @current = current
12
13
  @route_test = Lux::Application::RouteTest.new self
13
14
  end
14
15
 
@@ -17,31 +18,32 @@ class Lux::Application
17
18
  end
18
19
 
19
20
  def body?
20
- Lux.current.response.body ? true : false
21
+ @current.response.body ? true : false
21
22
  end
22
23
 
23
24
  def body data
24
- Lux.current.response.body data
25
+ @current.response.body data
25
26
  end
26
27
 
27
28
  def current
28
- Lux.current
29
+ # Lux.current
30
+ @current
29
31
  end
30
32
 
31
33
  def nav
32
- Lux.current.nav
34
+ @current.nav
33
35
  end
34
36
 
35
37
  def params
36
- Lux.current.params
38
+ @current.params
37
39
  end
38
40
 
39
41
  def redirect where, msgs={}
40
- Lux.current.redirect where, msgs
42
+ @current.redirect where, msgs
41
43
  end
42
44
 
43
45
  def request
44
- Lux.current.request
46
+ @current.request
45
47
  end
46
48
 
47
49
  # gets only root
@@ -55,15 +57,15 @@ class Lux::Application
55
57
  end
56
58
 
57
59
  def done?
58
- throw :done if Lux.current.response.body
60
+ throw :done if @current.response.body
59
61
  end
60
62
 
61
63
  def get?
62
- Lux.current.request.request_method == 'GET'
64
+ @current.request.request_method == 'GET'
63
65
  end
64
66
 
65
67
  def post?
66
- Lux.current.request.request_method == 'POST'
68
+ @current.request.request_method == 'POST'
67
69
  end
68
70
 
69
71
  def plug name, &block
@@ -179,4 +181,14 @@ class Lux::Application
179
181
  end
180
182
  end
181
183
 
184
+ def render
185
+ Lux.log "\n#{current.request.request_method.white} #{current.request.path.white}"
186
+
187
+ Lux::Config.live_require_check! if Lux.config(:auto_code_reload)
188
+
189
+ main
190
+
191
+ current.response.render
192
+ end
193
+
182
194
  end
@@ -4,6 +4,16 @@
4
4
  # Hash :qs, Hash :post, String :method, Hash :cookies, Hash :session
5
5
  # https://github.com/rack/rack/blob/master/test/spec_request.rb
6
6
 
7
+ # Lux.app.render('/admin') -> { status: 403, ... }
8
+ # Lux.app.render('/admin', session: { user_id: User.is_admin.first.id })
9
+ # {
10
+ # time: '1ms'
11
+ # status: 200,
12
+ # headers: {...},
13
+ # session: {...},
14
+ # body: '<html> ...'
15
+ # }.h
16
+
7
17
  Lux::Application.class_eval do
8
18
  def self.render path='/mock', in_opts={}, &block
9
19
  allowed_opts = [:qs, :post, :method, :session, :cookies]
@@ -35,14 +45,14 @@ Lux::Application.class_eval do
35
45
  env[k.to_s.upcase] = v
36
46
  end
37
47
 
38
- page = Lux::Current.new(env)
39
- Lux.current.session.merge!(in_opts[:session]) if in_opts[:session]
48
+ current = Lux::Current.new(env)
49
+ current.session.merge!(in_opts[:session]) if in_opts[:session]
40
50
 
41
- if block_given?
42
- return Lux.app.new.instance_exec &block
43
- end
51
+ app = new current
52
+
53
+ return app.instance_exec &block if block_given?
44
54
 
45
- response = Lux.current.response.render
55
+ response = app.render
46
56
 
47
57
  body = response[2].join('')
48
58
  body = JSON.parse body if response[1]['content-type'].index('/json')
@@ -51,7 +61,7 @@ Lux::Application.class_eval do
51
61
  time: response[1]['x-lux-speed'],
52
62
  status: response[0],
53
63
  headers: response[1],
54
- session: Lux.current.session,
64
+ session: current.session,
55
65
  body: body
56
66
  }.h
57
67
  end
@@ -55,6 +55,18 @@ class Lux::Current::StaticFile
55
55
  true
56
56
  end
57
57
 
58
+ def resolve_content_type
59
+ mimme = MIMME_TYPES[@ext.to_sym]
60
+
61
+ unless mimme
62
+ c.response.body('Mimme type not supported')
63
+ c.response.status(406)
64
+ return
65
+ end
66
+
67
+ c.response.content_type = mimme
68
+ end
69
+
58
70
  def deliver data=nil
59
71
  file = File.exist?(@file) ? @file : Lux.root.join("public#{@file}")
60
72
 
@@ -66,16 +78,9 @@ class Lux::Current::StaticFile
66
78
  end
67
79
  end
68
80
 
69
- ext = file.to_s.split('.').last
70
- mimme = MIMME_TYPES[ext.to_sym]
71
-
72
- unless mimme
73
- c.response.body('Mimme type not supported')
74
- c.response.status(404)
75
- return
76
- end
81
+ @ext = file.to_s.split('.').last
77
82
 
78
- c.response.content_type = mimme
83
+ resolve_content_type
79
84
 
80
85
  file_mtime = File.mtime(file).utc.to_s
81
86
  key = Crypt.sha1(file+file_mtime.to_s)
@@ -91,10 +96,7 @@ class Lux::Current::StaticFile
91
96
  return
92
97
  end
93
98
 
94
- # c.response.headers['X-Sendfile'] = file
95
-
96
- data ||= File.read(file)
97
- c.response.body(data)
99
+ c.response.body data || File.read(file)
98
100
 
99
101
  true
100
102
  end
@@ -25,11 +25,12 @@ module Lux
25
25
  define_method(:cache) { Lux::Cache }
26
26
 
27
27
  # main rack response
28
- def call env=nil
29
- req = Lux::Current.new env
30
- req.response.render
31
- rescue
32
- raise $! if Lux.config(:show_server_errors)
28
+ def call env=nil
29
+ state = Lux::Current.new env
30
+ app = Lux::Application.new state
31
+ app.render
32
+ rescue => e
33
+ raise e if Lux.config(:show_server_errors)
33
34
 
34
35
  [500, {}, ['Lux server error']]
35
36
  end
@@ -165,34 +165,23 @@ class Lux::Response
165
165
 
166
166
  def write_response_header
167
167
  domain =
168
- if cookie_domain
169
- cookie_domain
170
- elsif cookie_multidomain && current.domain.index('.')
168
+ if cookie_multidomain && current.domain.index('.')
171
169
  ".#{current.domain}"
172
170
  else
173
- current.request.host
171
+ cookie_domain || current.request.host
174
172
  end
175
173
 
176
174
  # cache-control
177
175
  @headers['cache-control'] ||= Proc.new do
178
- cc = ['max-age=%d' % max_age]
179
-
180
- if max_age > 0
181
- cc.push 'public, no-cache'
182
- else
183
- cc.push 'private, must-revalidate'
184
- end
185
-
176
+ cc = ['max-age=%d' % max_age]
177
+ cc.push max_age > 0 ? 'public, no-cache' : 'private, must-revalidate'
186
178
  cc.join(', ')
187
179
  end.call
188
180
 
189
181
  current.session[:lux_flash] = flash.to_h
190
182
 
191
183
  # dont annd cookies to public pages (images, etc..)
192
- add_cookies = true
193
- add_cookies = false if @headers['cache-control'].index('public')
194
-
195
- if add_cookies
184
+ unless @headers['cache-control'].index('public')
196
185
  encrypted = Crypt.encrypt(current.session.to_json)
197
186
 
198
187
  if current.cookies[Lux.config.session_cookie_name] != encrypted
@@ -208,7 +197,7 @@ class Lux::Response
208
197
  # if "no-store" is present then HTTP_IF_NONE_MATCH is not sent from browser
209
198
  end
210
199
 
211
- def write_response
200
+ def render
212
201
  write_response_body
213
202
  write_response_header
214
203
 
@@ -224,14 +213,4 @@ class Lux::Response
224
213
  [@status, @headers.to_h, [@body]]
225
214
  end
226
215
 
227
- def render
228
- Lux.log "\n#{current.request.request_method.white} #{Lux.current.request.path.white}"
229
-
230
- Lux::Config.live_require_check! if Lux.config(:auto_code_reload)
231
-
232
- Lux::Application.new.main
233
-
234
- write_response
235
- end
236
-
237
216
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lux-fw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dino Reic