lux-fw 0.1.35 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +5 -5
  2. data/.version +1 -1
  3. data/bin/cli/am +67 -41
  4. data/bin/cli/assets +1 -1
  5. data/bin/cli/console +3 -2
  6. data/bin/cli/eval +11 -2
  7. data/bin/cli/exceptions +1 -1
  8. data/bin/cli/generate +4 -0
  9. data/bin/cli/render +18 -0
  10. data/bin/cli/routes +3 -1
  11. data/bin/cli/server +4 -1
  12. data/bin/lux +10 -1
  13. data/bin/txt/nginx.conf +35 -18
  14. data/lib/common/class_attributes.rb +10 -10
  15. data/lib/common/{before_and_after.rb → class_callbacks.rb} +12 -21
  16. data/lib/common/crypt.rb +10 -6
  17. data/lib/common/dynamic_class.rb +12 -7
  18. data/lib/common/generic_model.rb +6 -6
  19. data/lib/common/hash_with_indifferent_access.rb +6 -284
  20. data/lib/{lux/helper/lib/html_tag.rb → common/html_tag_builder.rb} +1 -1
  21. data/lib/common/policy.rb +2 -2
  22. data/lib/common/url.rb +6 -4
  23. data/lib/lux-fw.rb +2 -13
  24. data/lib/lux/api/api.rb +34 -47
  25. data/lib/lux/api/lib/dsl.rb +2 -2
  26. data/lib/lux/api/lib/model_api.rb +40 -23
  27. data/lib/lux/api/lib/response.rb +30 -5
  28. data/lib/lux/application/application.rb +182 -0
  29. data/lib/lux/{controller → application}/lib/nav.rb +18 -21
  30. data/lib/lux/application/lib/plugs.rb +10 -0
  31. data/lib/lux/application/lib/render.rb +58 -0
  32. data/lib/lux/application/lib/route_test.rb +64 -0
  33. data/lib/lux/cache/cache.rb +27 -16
  34. data/lib/lux/cell/cell.rb +66 -54
  35. data/lib/lux/config/config.rb +51 -27
  36. data/lib/lux/current/current.rb +122 -0
  37. data/lib/lux/{page → current}/lib/encrypt_params.rb +2 -2
  38. data/lib/lux/{page → current}/lib/static_file.rb +29 -25
  39. data/lib/lux/delayed_job/delayed_job.rb +4 -4
  40. data/lib/lux/error/error.rb +31 -16
  41. data/lib/lux/helper/helper.rb +32 -37
  42. data/lib/lux/helper/helpers/application_helper.rb +3 -0
  43. data/lib/lux/helper/helpers/html_helper.rb +3 -0
  44. data/lib/lux/helper/helpers/mailer_helper.rb +11 -0
  45. data/lib/lux/lux.rb +40 -30
  46. data/lib/lux/mailer/mailer.rb +46 -29
  47. data/lib/lux/{page → response}/lib/flash.rb +1 -1
  48. data/lib/lux/response/lib/header.rb +21 -0
  49. data/lib/lux/response/response.rb +237 -0
  50. data/lib/lux/template/template.rb +20 -18
  51. data/lib/overload/array.rb +5 -0
  52. data/lib/overload/auto_loader.rb +27 -0
  53. data/lib/overload/blank.rb +8 -2
  54. data/lib/overload/float.rb +10 -0
  55. data/lib/overload/hash.rb +5 -12
  56. data/lib/overload/integer.rb +1 -5
  57. data/lib/overload/nil.rb +5 -0
  58. data/lib/overload/object.rb +1 -0
  59. data/lib/overload/r.rb +5 -0
  60. data/lib/overload/string.rb +4 -5
  61. data/lib/overload/{date.rb → time.rb} +23 -3
  62. metadata +37 -73
  63. data/bin/forever +0 -65
  64. data/bin/job_que +0 -39
  65. data/lib/lux/api/lib/application_api.rb +0 -38
  66. data/lib/lux/api/lib/doc_builder.rb +0 -19
  67. data/lib/lux/api/lib/rescue.rb +0 -8
  68. data/lib/lux/controller/controller.rb +0 -185
  69. data/lib/lux/controller/lib/plugs.rb +0 -10
  70. data/lib/lux/html/html.rb +0 -3
  71. data/lib/lux/html/lib/form.rb +0 -81
  72. data/lib/lux/html/lib/input.rb +0 -71
  73. data/lib/lux/html/lib/input_types.rb +0 -287
  74. data/lib/lux/lib/lux.rb +0 -51
  75. data/lib/lux/page/lib/response.rb +0 -178
  76. data/lib/lux/page/page.rb +0 -292
  77. data/lib/lux/rescue_from/rescue_from.rb +0 -63
  78. data/lib/plugins/assets/assets_plug.rb +0 -31
  79. data/lib/plugins/assets/helper_module_adapter.rb +0 -51
  80. data/lib/plugins/assets/init.rb +0 -4
  81. data/lib/plugins/db_helpers/array_and_hstore.rb +0 -64
  82. data/lib/plugins/db_helpers/arrays_and_tags.rb +0 -23
  83. data/lib/plugins/db_helpers/before_save.rb +0 -44
  84. data/lib/plugins/db_helpers/cached_find_by.rb +0 -45
  85. data/lib/plugins/db_helpers/class_and_instance.rb +0 -120
  86. data/lib/plugins/db_helpers/dataset_plugin.rb +0 -101
  87. data/lib/plugins/db_helpers/filter_wrappers.rb +0 -21
  88. data/lib/plugins/db_helpers/link_plugin.rb +0 -95
  89. data/lib/plugins/db_helpers/localize_plugin.rb +0 -57
  90. data/lib/plugins/db_helpers/primary_keys.rb +0 -36
  91. data/lib/plugins/db_helpers/typero_attributes.rb +0 -69
  92. data/lib/plugins/db_logger/init.rb +0 -18
  93. data/lib/plugins/db_logger/lux_response_adapter.rb +0 -9
  94. data/lib/plugins/paginate/helper.rb +0 -32
  95. data/lib/plugins/paginate/sequel_adapter.rb +0 -18
  96. data/lib/vendor/mini_assets/lib/asset.rb +0 -71
  97. data/lib/vendor/mini_assets/lib/asset/css.rb +0 -19
  98. data/lib/vendor/mini_assets/lib/asset/js.rb +0 -17
  99. data/lib/vendor/mini_assets/lib/base.rb +0 -69
  100. data/lib/vendor/mini_assets/lib/base/javascript.rb +0 -13
  101. data/lib/vendor/mini_assets/lib/base/stylesheet.rb +0 -5
  102. data/lib/vendor/mini_assets/lib/manifest.rb +0 -18
  103. data/lib/vendor/mini_assets/lib/opts.rb +0 -16
  104. data/lib/vendor/mini_assets/mini_assets.rb +0 -74
  105. data/lib/vendor/oauth/lib/facebook.rb +0 -35
  106. data/lib/vendor/oauth/lib/github.rb +0 -37
  107. data/lib/vendor/oauth/lib/google.rb +0 -41
  108. data/lib/vendor/oauth/lib/linkedin.rb +0 -41
  109. data/lib/vendor/oauth/lib/stackexchange.rb +0 -37
  110. data/lib/vendor/oauth/lib/twitter.rb +0 -41
  111. data/lib/vendor/oauth/oauth.rb +0 -46
@@ -1,71 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # input = Lux::Html::Input.new(User.first)
4
- # input.string :email
5
-
6
- class Lux::Html::Input
7
- attr_accessor :type
8
-
9
- def initialize(obj=nil, opts={})
10
- @object = obj
11
- @globals = opts.dup
12
- end
13
-
14
- # exports @name and @opts globals
15
- def opts_prepare(name, opts={})
16
- unless opts[:as]
17
- data_type = @object[name].class.name rescue 'String'
18
- opts[:as] = :checkbox if ['TrueClass','FalseClass'].index(data_type)
19
- end
20
-
21
- # experimental, figure out collection unless defined
22
- if name =~ /_id$/ && opts[:as] == :select && !opts[:collection]
23
- class_name = name.to_s.split('_id')[0].capitalize
24
- opts[:collection] = eval "#{class_name}.order('name').all"
25
- end
26
-
27
- opts[:as] ||= :select if opts[:collection]
28
- opts[:id] ||= Lux.uid
29
- opts[:value] ||= @object.send(name) if @object
30
- opts[:name] = name.kind_of?(Symbol) && @object ? "#{@object.class.name.underscore}[#{name}]" : name
31
-
32
- # convert decimal numbers to float
33
- opts[:value] = opts[:value].to_f if opts[:value].class == BigDecimal
34
-
35
- @label = opts.delete :label
36
- @wrap = opts.delete(:wrap) || @globals[:wrap]
37
- @name = name
38
- @opts = opts
39
- end
40
-
41
- # if type is written in parameter :as=> use this helper function
42
- def render(name, opts={})
43
- if name.is_hash?
44
- opts = name
45
- name = :null
46
- end
47
-
48
- opts = opts_prepare name, opts.dup
49
- @type = opts.delete(:as) || :text
50
- send("as_#{@type}") rescue Lux::Error.inline("as_#{@type}")
51
- end
52
-
53
- def prepare_collection(data)
54
- ret = []
55
- for el in data
56
- if data[0].respond_to?(:select_name)
57
- ret.push [el.id.to_s, el.select_name]
58
- elsif data[0].respond_to?(:name)
59
- ret.push [el.id.to_s, el.name]
60
- elsif data[0].kind_of?(Array)
61
- ret.push [el[0].to_s, el[1]]
62
- elsif data.is_hash?
63
- ret.push el
64
- else
65
- ret.push [el.to_s, el]
66
- end
67
- end
68
- ret
69
- end
70
-
71
- end
@@ -1,287 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Lux::Html::Input
4
-
5
- # if you call .memo which is not existant, it translates to .as_memo with opts_prepare first
6
- # def method_missing(meth, *args, &block)
7
- # opts_prepare *args
8
- # send "as_#{meth}"
9
- # end
10
-
11
- #############################
12
- # custom fields definitions #
13
- #############################
14
-
15
- def as_string
16
- @opts[:type] = 'text'
17
- @opts[:autocomplete] ||= 'off'
18
- @opts.tag(:input)
19
- end
20
- alias :as_text :as_string
21
-
22
- def as_password
23
- @opts[:type] = 'password'
24
- @opts.tag(:input)
25
- end
26
-
27
- def as_hidden
28
- @opts[:type] = 'hidden'
29
- @opts.tag(:input)
30
- end
31
-
32
- def as_file
33
- @opts[:type] = 'file'
34
- @opts.tag(:input)
35
- end
36
-
37
- def as_textarea
38
- val = @opts.delete(:value) || ''
39
- comp_style = val.split(/\n/).length + val.length/100
40
- comp_style = 4 if comp_style < 4
41
- comp_style = 15 if comp_style > 15
42
- @opts[:style] = "height:#{comp_style*20}px; #{@opts[:style]};"
43
- @opts.tag(:textarea, val)
44
- end
45
- alias :as_memo :as_textarea
46
-
47
- def as_checkbox
48
- id = Lux.uid
49
- hidden = { :name=>@opts.delete(:name), :type=>:hidden, :value=>@opts[:value] ? 1 : 0, :id=>id }
50
- @opts[:type] = :checkbox
51
- @opts[:onclick] = "document.getElementById('#{id}').value=this.checked ? 1 : 0; #{@opts[:onclick]}"
52
- @opts[:checked] = @opts.delete(:value) ? 1 : nil
53
- @opts.tag(:input)+hidden.tag(:input)
54
- end
55
-
56
- def as_radio
57
- @opts[:type] = :radio
58
- @opts[:checked] = @opts[:value] == @object.send(@name) ? true : nil
59
- @opts.tag(:input)
60
- end
61
-
62
- def as_select
63
- body = []
64
- collection = @opts.delete(:collection)
65
-
66
- if nullval = @opts.delete(:null)
67
- body.push %[<option value="">#{nullval}</option>] if nullval
68
- end
69
-
70
- for el in prepare_collection(collection)
71
- body.push(%[<option value="#{el[0]}"#{@opts[:value].to_s == el[0].to_s ? ' selected=""' : nil}>#{el[1]}</option>])
72
- end
73
-
74
- body = body.join("\n")
75
- @opts.tag(:select, body)
76
- end
77
-
78
- def as_tag
79
- @opts[:value] = @opts[:value].or([]).join(', ') if ['Array', 'Sequel::Postgres::PGArray'].index(@opts[:value].class.name)
80
- @opts[:id] ||= Lux.uid
81
- @opts[:type] = :text
82
- @opts[:onkeyup] = %[draw_tag('#{@opts[:id]}')]
83
- @opts[:autocomplete] ||= 'off'
84
- ret = %[
85
- <script>
86
- window.draw_tag = window.draw_tag || function (id) {
87
- tags = $.map($('#'+id).val().split(/\s*,\s*/), function(el) {
88
- val = el.replace(/\s+/,'-');
89
- return val ? '<span class="label label-default">'+val+'</span> ' : ''
90
- });
91
- $('#'+id+'_tags').html(tags)
92
- }</script>]
93
- ret += @opts.tag(:input)
94
- ret += %[<div id="#{@opts[:id]}_tags" style="margin-top:5px;"></div>]
95
- ret += %[<script>if (window.$) { draw_tag('#{@opts[:id]}'); } else { window.onload = function(){ draw_tag('#{@opts[:id]}'); } }</script>]
96
- ret
97
- end
98
-
99
- def as_date
100
- @opts[:type] = :date
101
- @opts[:style] = 'width: 160px; display: inline;'
102
-
103
- value = @opts[:value] || nil
104
- desc = value ? '&mdash;' + Time.ago(value) : ''
105
-
106
- [@opts.tag(:input), desc].join(' ')
107
- end
108
-
109
- def as_datetime
110
- value = @opts[:value]
111
- id = @opts[:id]
112
-
113
- value_day = value ? value.strftime('%Y-%m-%d') : ''
114
- value_time = value ? value.strftime('%H:%M') : ''
115
- value_all = value ? value.strftime('%H:%M') : ''
116
-
117
- base = { class: 'form-control', onchange: "datetime_set('#{id}');", style: 'width: 160px; display: inline;' }
118
-
119
- input_day = base.merge({ type: :date, id: '%s_day' % id, value: value_day }).tag :input
120
- input_time = base.merge({ style: 'width: 110px; display: inline;', type: :time, id: '%s_time' % id, value: value_time }).tag :input
121
- input_all = base.merge({ style: 'width: 150px; display: inline;', type: :text, id: id, name: @opts[:name], onfocus: 'blur();' }).tag :input
122
- script = %[<script>window.datetime_set = function(id) { $('#'+id).val($('#'+id+'_day').val()+' '+$('#'+id+'_time').val()); }; datetime_set('#{id}');</script>]
123
- desc = value ? '&mdash;' + Time.ago(value) : ''
124
-
125
- [input_day, input_time, input_all, script, desc].join(' ')
126
- end
127
-
128
- def as_datebuttons
129
- @opts[:type] = 'text'
130
- @opts[:style] = 'width:100px; display:inline;'
131
- @opts[:id] = "date_#{Lux.uid}"
132
- id = "##{@opts[:id]}"
133
- ret = @opts.tag(:input)
134
- ret += %[ <button class="btn btn-default btn-sm" onclick="$('#{id}').val('#{DateTime.now.strftime('%Y-%m-%d')}'); return false;">Today</button>]
135
- for el in [1, 3, 7, 14, 30]
136
- date = DateTime.now+el.days
137
- name = el.to_s
138
- name += " (#{date.strftime('%a')})" if el < 7
139
- ret += %[ <button class="btn btn-default btn-sm" onclick="$('#{id}').val('#{(DateTime.now+el.days).strftime('%Y-%m-%d')}'); return false;">+#{name}</button>]
140
- end
141
- ret
142
- end
143
-
144
- def as_user
145
- button_text = if @opts[:value].to_i > 0
146
- usr = User.find(@opts[:value])
147
- "#{usr.name} (#{usr.email})"
148
- else
149
- 'Select user'
150
- end
151
-
152
- @opts[:style] = "width:auto;#{@opts[:style]};"
153
- @opts[:onclick] = %[Popup.render(this,'Select user', '/part/users/single_user?product_id=#{@object.bucket_id}');return false;]
154
- @opts.tag :button, button_text
155
- end
156
-
157
- def as_photo
158
- @opts[:type] = 'hidden'
159
- if @opts[:value].present?
160
- img = Photo.find(@opts[:value])
161
- @image = %[ <img id="#{@opts[:id]}_image" style="height:34px; cursor:pointer;" src="#{img.thumbnail}" onclick="window.open('#{img.image.remote_url}')" /> <span class="btn btn-default btn-xs" onclick="$('##{@opts[:id]}').val('');$('##{@opts[:id]}_image').remove();$(this).remove();">&times;</span>]
162
- end
163
- picker = @opts.tag(:input)
164
- %[<span class="btn btn-default" onclick="Photo.pick('#{@opts[:id]}', function(id) { alert('Chosen: '+id) })">Select photo</span>#{picker}#{@image}]
165
- end
166
-
167
- def as_photos
168
- @opts[:type] = 'text'
169
- @opts[:style] = 'width:150px; display:inline;'
170
- @opts[:class] += ' mutiple'
171
- @images = []
172
- if @opts[:value].present?
173
- for el in @opts[:value].split(',').uniq
174
- img = Photo.find(el.to_i) rescue next
175
- @images.push %[ <img style="height:34px; cursor:pointer;" src="#{img.thumbnail}" onclick="window.open('#{img.image.remote_url}')" />]
176
- end
177
- end
178
- picker = @opts.tag(:input)
179
- %[<span class="btn btn-default" onclick="Photo.pick('#{@opts[:id]}', function(id) { alert('Chosen: '+id) })">Add photo</span> #{picker}<div class="images" style="padding-top:5px;">#{@images.join(' ')}</div>]
180
- end
181
-
182
- def as_admin_password
183
- @opts[:type] = 'text'
184
- @opts[:style] = 'display:none;'
185
- @opts[:value] = ''
186
- ret = @opts.tag(:input)
187
- %[<span class="btn btn-default" onclick="$(this).hide();$('##{@opts[:id]}').show().val('').attr('type','password').focus()">Set pass</span> #{ret}]
188
- end
189
-
190
- def as_color
191
- value = @opts[:value]
192
- @opts[:style] ||= 'width:150px; float: left; margin-right: 10px;'
193
- as_text + %[<span style="background-color: #{value.or('#fff')}; height:34px; width:150px; display: inline-block;"></span>]
194
- end
195
-
196
- def as_array_values
197
- name = @opts[:name]
198
- ret = []
199
- values = @opts[:value].kind_of?(String) ? @opts[:value].split(',') : @opts[:value]
200
- for el in @opts[:collection]
201
- ret.push %[<label style="position:relative; top:4px;">
202
- <input name="#{name}[#{el[1]}]" value="1" type="checkbox" #{values[el[1]].present? ? 'checked=""' : ''} style="position:relative;top:2px; left:2px;" />
203
- <span style="margin-right:10px;">#{el[0]}</span>
204
- </label>]
205
- end
206
- ret.join('')
207
- end
208
-
209
- def as_pass
210
- value = @opts[:value]
211
- id = @opts[:id]
212
- @opts[:value] = ''
213
- @opts[:style] = 'width:200px; display:inline;'
214
- ret = @opts.tag(:input)
215
- %[<span class="btn btn-default" onclick="$(this).hide(); $(this).next().show().focus();">#{value.present? ? 'Change' : 'Set'} password</span><span id="s-#{id}" style="display:none;">#{ret} or <a onclick="p=$(this).parent(); p.hide(); p.prev().show(); return false;" href="#">cancel</a></span>]
216
- end
217
-
218
- def as_image
219
- @opts[:type] = 'text'
220
- @opts[:autocomplete] ||= 'off'
221
- @opts[:style] = 'width:350px; float:left;'
222
- input = @opts.tag(:input)
223
- path_name = defined?(Storage) ? :storages : :images
224
- input += %[<span class="btn btn-default" style="float:left; margin-left:5px;" onclick="Popup.template('#{path_name}/select', function(url){ $('##{@opts[:id]}').val(url); Popup.close(); })">upload</span>]
225
- if @opts[:value].present?
226
- input = %[<img onload="i = new Image(); i.src='#{@opts[:value]}'; $('#img_size_#{@opts[:id]}').html(i.width+' x '+i.height)" src="#{@opts[:value]}" onclick="window.open('#{@opts[:value]}')" style="width:100px; border:1px solid #ccc; float:left; margin-right:10px;" /> #{input}]
227
- input += %[<br /><br /><span id="img_size_#{@opts[:id]}">...</span>]
228
- end
229
-
230
- input
231
- end
232
-
233
- def as_geo
234
- @opts[:type] = 'text'
235
- @opts[:style] = 'width:200px; float:left;'
236
- ret = @opts.tag(:input)
237
- ret += %[ <a target="new" href="http://maps.google.com/maps?q=loc:#{@opts[:value]}" style="display:block; margin-top:7px;">&nbsp;Show on map</a>]
238
- end
239
-
240
- def as_html_trix
241
- %[<textarea id="trix_#{@name}" name="#{@opts[:name]}" style="display:none;">#{@opts[:value]}</textarea>
242
- <trix-editor input="trix_#{@name}"></trix-editor>]
243
- end
244
-
245
- def as_button_select
246
- body = ['<div class="btn-group">']
247
- collection = @opts.delete(:collection)
248
- for el in prepare_collection(collection)
249
- opts = { class:'btn btn-sm' }
250
- ap "#{@opts[:name]} - #{@opts[:value]} == #{el[0]}"
251
- opts[:class] += ' btn-primary' if @opts[:value].to_s == el[0].to_s
252
- opts[:onclick] = "$(this).parent().find('.btn').removeClass('btn-primary'); $(this).addClass('btn-primary').blur(); $(this).addClass('btn-primary').blur();"
253
- opts[:onclick] += @opts[:onclick] ? "(#{@opts[:onclick].sub(/;\s*$/,'')})('#{el[0]}')" : "$('##{@opts[:id]}').val('#{el[0]}')"
254
- opts[:onclick] += '; return false;'
255
- body.push opts.tag(:button, el[1])
256
- end
257
- body.push '</div>'
258
- body.push @opts.pluck(:name, :id, :value).merge(type: :hidden).tag(:input)
259
- body.join('')
260
- end
261
-
262
- def as_address
263
- val = @opts[:value]
264
- @opts[:style] ||= 'height:55px; width:250px; float: left; margin-right:5px;'
265
- ret = as_textarea
266
- ret += %[<div><a class="btn btn-default btn-xs" onclick="window.open('https://www.google.hr/maps?q='+$('##{@opts[:id]}').val()); return false;">open in new window</a></div>] if val.to_s.length > 5
267
- ret
268
- end
269
-
270
- def as_images
271
- path_name = defined?(Storage) ? :storages : :images
272
-
273
- val = @opts[:value].to_s
274
- ret = as_memo
275
- ret += '<div style="margin-top: 7px;"></div>'
276
- ret += val.split("\n").map{ |url| %[<a href="#{url}" target="_new"><img src="#{url}" style="height:50px; margin-right:5px; border:1px solid #ccc;" /></a>] }.join(' ')
277
- ret += %[<span class="btn btn-default" style="float:left; margin-left:5px;" onclick='Popup.template("#{path_name}/select", function(url){ $("##{@opts[:id]}")[0].value += "\\n"+url; Popup.close(); })'>add image</span>]
278
- ret
279
- end
280
-
281
- def as_disabled
282
- @opts[:disabled] = true
283
- @opts.delete(:name)
284
- as_text
285
- end
286
-
287
- end
data/lib/lux/lib/lux.rb DELETED
@@ -1,51 +0,0 @@
1
- # handy :)
2
- # renders full pages and exposes page object (req, res) in yiled
3
- # for easy and powerful testing
4
- # Hash :qs, Hash :post, String :method, Hash :cookies, Hash :session
5
- # https://github.com/rack/rack/blob/master/test/spec_request.rb
6
- def Lux(path, in_opts={}, &block)
7
- allowed_opts = [:qs, :post, :method, :session, :cookies]
8
- in_opts.keys.each { |k| die "#{k} is not allowed as opts param. allowed are #{allowed_opts}" unless allowed_opts.index(k) }
9
-
10
- opts = {}
11
-
12
- if in_opts[:post]
13
- opts[:query_string] = in_opts[:post]
14
- opts[:request_method] = :post
15
- else
16
- opts[:query_string] = in_opts[:qs] || {}
17
- opts[:request_method] ||= in_opts[:method] || :get
18
- end
19
- opts[:request_method] = opts[:request_method].to_s.upcase
20
- opts[:query_string] = opts[:query_string].to_query if opts[:query_string].class.to_s == 'Hash'
21
-
22
- if path[0,4] == 'http'
23
- parsed = URI.parse(path)
24
- opts[:server_name] = parsed.host
25
- opts[:server_port] = parsed.port
26
- path = '/'+path.split('/', 4).last
27
- end
28
-
29
- env = Rack::MockRequest.env_for(path)
30
- env[:input] = opts[:post] if opts[:post]
31
- for k,v in opts
32
- env[k.to_s.upcase] = v
33
- end
34
-
35
- page = nil
36
- time_ms = Lux.speed { page = Lux::Page.prepare(env) }
37
- page.session = in_opts[:session] if in_opts[:session]
38
-
39
- return page.instance_exec &block if block_given?
40
-
41
- response = page.render
42
- body = response[2].join('')
43
- body = JSON.parse body if response[1]['content-type'].index('/json')
44
-
45
- {
46
- time: time_ms,
47
- status: response[0],
48
- headers: response[1],
49
- body: body
50
- }.h
51
- end
@@ -1,178 +0,0 @@
1
- # class Lux::Page::Response
2
- # attr_accessor :body, :headers, :cookies, :content_type, :status
3
-
4
- # def initialize request
5
- # @render_start = Time.now
6
- # @headers = {}
7
- # @cookies = {}
8
- # @request = request
9
-
10
- # for cookie in request.env['HTTP_COOKIE'].to_s.split(/;\s*/).map{ |el| el.split('=',2) }
11
- # @cookies[cookie[0]] = cookie[1]
12
- # end
13
- # end
14
-
15
- # def header name, value=:_
16
- # name = name.downcase
17
- # @headers[name] = value if value != :_
18
- # @headers[name]
19
- # end
20
-
21
- # def cache_control value=nil
22
- # raise ArgumentError.new('Vrong value') unless [:private, :public].include?(value)
23
-
24
- # # if "no-store" is present then HTTP_IF_NONE_MATCH is not sent from browser
25
- # @headers['cache-control'] = 'must-revalidate, %s, max-age=0' % value
26
- # end
27
-
28
- # def etag *args
29
- # @headers['etag'] ||= 'W/"%s"' % Lux.cache.generate_key(args)
30
-
31
- # if @request.env['HTTP_IF_NONE_MATCH'] == @headers['etag']
32
- # @status = 304
33
- # @body = 'not-modified'
34
- # true
35
- # else
36
- # false
37
- # end
38
- # end
39
-
40
- # def status num=nil
41
- # Lux.log caller if num == 500
42
- # return @status if @status
43
- # if num
44
- # if num.is_numeric?
45
- # @status ||= num.to_i
46
- # else
47
- # @status = case num.to_s
48
- # when 'StandardError'; 400
49
- # when 'BadRequestError'; 400
50
- # when 'UnauthorizedError'; 401
51
- # when 'ForbidenError'; 403
52
- # when 'NotFoundError'; 404
53
- # when 'RateLimitError'; 503
54
- # else; 500
55
- # end
56
- # end
57
- # end
58
-
59
- # @status
60
- # end
61
-
62
- # def body what=nil
63
- # @body ||= what
64
-
65
- # if @body && block_given?
66
- # @body = yield @body
67
- # Lux.error 'Lux.page.body is not a string (bad page.body filter)' unless @body.is_a?(String)
68
- # end
69
-
70
- # @body
71
- # end
72
-
73
- # def body= what
74
- # body(what)
75
- # end
76
-
77
- # def body! what
78
- # @body = what
79
- # end
80
-
81
- # def content_type type=nil
82
- # return @content_type unless type
83
-
84
- # # can be set only once
85
- # return @content_type if @content_type
86
-
87
- # type = 'application/json' if type == :json
88
- # type = 'text/plain' if type == :text
89
- # type = 'text/html' if type == :html
90
-
91
- # raise 'Invalid page content-type %s' % type if type === Symbol
92
-
93
- # @content_type = type
94
- # end
95
-
96
- # def content_type= type
97
- # content_type type
98
- # end
99
-
100
- # def redirect where=nil, opts={}
101
- # return @headers['location'] unless where
102
-
103
- # @status = opts.delete(:status) || 302
104
- # opts.map { |k,v| flash.send(k, v) }
105
-
106
- # @headers['location'] = where.index('//') ? where : "#{host}#{where}"
107
- # @body = %[redirecting to #{@headers['location']}\n\n#{opts.values.join("\n")}]
108
- # end
109
-
110
- # def permanent_redirect where
111
- # redirect where, status:301
112
- # end
113
-
114
- # def write_response_body
115
- # # if @body is not set, this is error now
116
- # unless @body
117
- # @status = 500
118
- # @body = 'Page BODY not defined. Maybe you did not call cell action but method instad.'
119
- # end
120
-
121
- # # respond as JSON if we recive hash
122
- # if @body.kind_of?(Hash)
123
- # @body = Lux.dev? ? JSON.pretty_generate(@body) : JSON.generate(@body)
124
-
125
- # if Lux.page.request.params[:callback]
126
- # @body = "#{@request.params[:callback]}(#{ret})"
127
- # @content_type ||= 'text/javascript'
128
- # else
129
- # @content_type ||= 'application/json'
130
- # end
131
-
132
- # @body += "\n"
133
- # else
134
- # # if somebody sets @content_type, respect that
135
- # @body = @body.to_s unless @body.kind_of?(String)
136
- # @content_type ||= 'text/plain' if @body[0,1] != '<'
137
- # @content_type ||= 'text/html'
138
- # end
139
- # end
140
-
141
- # def write_response_header
142
- # domain =
143
- # if request.host =~ %r{^[\d\.]+$} # if request is IP
144
- # request.host
145
- # else
146
- # base_domain.index('.') ? ".#{base_domain}" : base_domain
147
- # end
148
-
149
- # @session[:lux_flash] = flash.to_h
150
-
151
- # # skip adding of cookies and time to strong etag parameters
152
- # if !@headers['etag'] || @headers['etag'].index('/')
153
- # @headers['set-cookie'] = "__luxs=#{Crypt.encrypt(@session.to_json)}; Expires=#{(Time.now+1.month).utc}; Path=/; Domain=#{domain};"
154
- # @headers['x-lux-speed'] = "#{((Time.now-@render_start)*1000).round(1)}ms"
155
- # end
156
-
157
- # @headers['content-type'] ||= "#{@content_type}; charset=utf-8"
158
-
159
- # Lux.page.etag(@body) if Lux.page.request.request_method == 'GET'
160
- # end
161
-
162
- # def write_response
163
- # write_response_body
164
- # write_response_header
165
-
166
- # @status ||= 200
167
- # Lux.log " #{@status}, #{@headers['x-lux-speed']}"
168
-
169
- # if ENV['LUX_PRINT_ROUTES']
170
- # print '* Finished route print '
171
- # puts @status == 404 ? 'without a match'.red : 'with a match'.green
172
- # exit
173
- # end
174
-
175
- # [@status, @headers, [@body]]
176
- # end
177
-
178
- # end