lux-fw 0.1.17 → 0.1.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/bin/cli/am +38 -29
- data/bin/cli/assets +8 -4
- data/bin/cli/exceptions +6 -6
- data/bin/cli/generate +0 -0
- data/bin/cli/get +0 -0
- data/bin/cli/nginx +11 -5
- data/bin/cli/routes +0 -0
- data/bin/cli/systemd +36 -0
- data/bin/forever +0 -0
- data/bin/job_que +0 -0
- data/bin/lux +1 -0
- data/lib/common/base32.rb +0 -0
- data/lib/common/class_attributes.rb +13 -4
- data/lib/common/crypt.rb +6 -10
- data/lib/common/dynamic_class.rb +23 -0
- data/lib/common/generic_model.rb +0 -0
- data/lib/common/hash_with_indifferent_access.rb +352 -0
- data/lib/common/method_attr.rb +69 -0
- data/lib/lux/api/api.rb +26 -27
- data/lib/lux/api/lib/application_api.rb +26 -7
- data/lib/lux/api/lib/doc_builder.rb +18 -17
- data/lib/lux/api/lib/dsl.rb +23 -41
- data/lib/lux/api/lib/error.rb +3 -0
- data/lib/lux/api/lib/model_api.rb +22 -20
- data/lib/lux/api/lib/rescue.rb +5 -15
- data/lib/lux/api/lib/response.rb +46 -0
- data/lib/lux/cache/cache.rb +13 -6
- data/lib/lux/cell/cell.rb +3 -14
- data/lib/lux/config/config.rb +4 -3
- data/lib/lux/controller/controller.rb +3 -3
- data/lib/lux/controller/lib/nav.rb +6 -2
- data/lib/lux/error/error.rb +15 -14
- data/lib/lux/helper/helper.rb +5 -5
- data/lib/lux/helper/lib/html_tag.rb +67 -0
- data/lib/lux/html/lib/input_types.rb +26 -16
- data/lib/lux/lib/lux.rb +51 -0
- data/lib/lux/lux.rb +5 -52
- data/lib/lux/page/lib/response.rb +178 -0
- data/lib/lux/page/page.rb +72 -51
- data/lib/lux/rescue_from/rescue_from.rb +8 -6
- data/lib/lux/template/template.rb +1 -0
- data/lib/lux-fw.rb +2 -0
- data/lib/overload/array.rb +4 -0
- data/lib/overload/date.rb +2 -0
- data/lib/overload/hash.rb +19 -10
- data/lib/overload/it.rb +29 -0
- data/lib/overload/object.rb +3 -19
- data/lib/overload/r.rb +53 -0
- data/lib/overload/string.rb +5 -6
- data/lib/overload/string_inflections.rb +4 -3
- data/lib/plugins/assets/assets_plug.rb +9 -4
- data/lib/plugins/assets/helper_module_adapter.rb +4 -2
- data/lib/plugins/db_helpers/link_plugin.rb +2 -2
- data/lib/plugins/db_logger/init.rb +1 -1
- data/lib/vendor/mini_assets/lib/asset/css.rb +19 -0
- data/lib/vendor/mini_assets/lib/asset/js.rb +17 -0
- data/lib/vendor/mini_assets/lib/asset.rb +71 -0
- data/lib/vendor/mini_assets/lib/base/javascript.rb +13 -0
- data/lib/vendor/mini_assets/lib/base/stylesheet.rb +5 -0
- data/lib/vendor/mini_assets/lib/base.rb +69 -0
- data/lib/vendor/mini_assets/lib/manifest.rb +18 -0
- data/lib/vendor/mini_assets/lib/opts.rb +16 -0
- data/lib/vendor/mini_assets/mini_assets.rb +74 -0
- metadata +23 -10
- data/lib/common/class_method_params.rb +0 -94
- data/lib/overload/hash_wia.rb +0 -282
- data/lib/overload/inflections.rb +0 -199
- data/lib/vendor/mini_assets/mini_asset/base.rb +0 -167
- data/lib/vendor/mini_assets/mini_asset/css.rb +0 -38
- data/lib/vendor/mini_assets/mini_asset/js.rb +0 -38
- data/lib/vendor/mini_assets/mini_asset.rb +0 -31
@@ -0,0 +1,352 @@
|
|
1
|
+
class HashWithIndifferentAccess
|
2
|
+
def initialize data=nil
|
3
|
+
@data = {}
|
4
|
+
merge! data if data
|
5
|
+
end
|
6
|
+
|
7
|
+
def merge! data
|
8
|
+
data.each { |key, value| @data[convert_key(key)] = convert_value(value) }
|
9
|
+
end
|
10
|
+
alias_method :update, :merge!
|
11
|
+
|
12
|
+
def merge data
|
13
|
+
copy = self.class.new @data
|
14
|
+
copy.merge! data
|
15
|
+
copy
|
16
|
+
end
|
17
|
+
|
18
|
+
def [] key
|
19
|
+
@data[convert_key(key)]
|
20
|
+
end
|
21
|
+
|
22
|
+
def []= key, value
|
23
|
+
@data[convert_key(key)] = convert_value(value)
|
24
|
+
end
|
25
|
+
alias_method :store, :[]=
|
26
|
+
|
27
|
+
def key? name
|
28
|
+
@data.key? convert_key(name)
|
29
|
+
end
|
30
|
+
alias_method :include?, :key?
|
31
|
+
alias_method :has_key?, :key?
|
32
|
+
alias_method :member?, :key?
|
33
|
+
|
34
|
+
def to_json opts=nil
|
35
|
+
@data.to_json opts
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete_if &block
|
39
|
+
@data.delete_if(&block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def delete key
|
43
|
+
@data.delete convert_key(key)
|
44
|
+
end
|
45
|
+
|
46
|
+
def dig *args
|
47
|
+
list = args.map{ |_| _.class == Symbol ? _.to_s : _ }
|
48
|
+
@data.dig *list
|
49
|
+
end
|
50
|
+
|
51
|
+
def clear
|
52
|
+
@data.keys.each { |key| @data.delete(key) }
|
53
|
+
end
|
54
|
+
|
55
|
+
def each; @data.each { |k,v| yield(k,v) }; end
|
56
|
+
def keys; @data.keys; end
|
57
|
+
def values; @data.keys; end
|
58
|
+
def to_hash; @data; end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def convert_key key
|
63
|
+
key.kind_of?(Symbol) ? key.to_s : key
|
64
|
+
end
|
65
|
+
|
66
|
+
def convert_value value
|
67
|
+
value.is_a?(Hash) ? self.class.new(value) : value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
# exrtacted from Rails
|
73
|
+
# class HashWithIndifferentAccess < Hash
|
74
|
+
# # Returns +true+ so that <tt>Array#extract_options!</tt> finds members of
|
75
|
+
# # this class.
|
76
|
+
# def extractable_options?
|
77
|
+
# true
|
78
|
+
# end
|
79
|
+
|
80
|
+
# def with_indifferent_access
|
81
|
+
# dup
|
82
|
+
# end
|
83
|
+
|
84
|
+
# def initialize(constructor = {})
|
85
|
+
# if constructor.respond_to?(:to_hash)
|
86
|
+
# super()
|
87
|
+
# update(constructor)
|
88
|
+
|
89
|
+
# hash = constructor.to_hash
|
90
|
+
# self.default = hash.default if hash.default
|
91
|
+
# self.default_proc = hash.default_proc if hash.default_proc
|
92
|
+
# else
|
93
|
+
# super(constructor)
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
|
97
|
+
# def default(*args)
|
98
|
+
# arg_key = args.first
|
99
|
+
|
100
|
+
# if include?(key = convert_key(arg_key))
|
101
|
+
# self[key]
|
102
|
+
# else
|
103
|
+
# super
|
104
|
+
# end
|
105
|
+
# end
|
106
|
+
|
107
|
+
# def self.[](*args)
|
108
|
+
# new.merge!(Hash[*args])
|
109
|
+
# end
|
110
|
+
|
111
|
+
# alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
|
112
|
+
# alias_method :regular_update, :update unless method_defined?(:regular_update)
|
113
|
+
|
114
|
+
# # Assigns a new value to the hash:
|
115
|
+
# #
|
116
|
+
# # hash = ActiveSupport::HashWithIndifferentAccess.new
|
117
|
+
# # hash[:key] = 'value'
|
118
|
+
# #
|
119
|
+
# # This value can be later fetched using either +:key+ or <tt>'key'</tt>.
|
120
|
+
# def []=(key, value)
|
121
|
+
# regular_writer(convert_key(key), convert_value(value, for: :assignment))
|
122
|
+
# end
|
123
|
+
|
124
|
+
# alias_method :store, :[]=
|
125
|
+
|
126
|
+
# # Updates the receiver in-place, merging in the hash passed as argument:
|
127
|
+
# #
|
128
|
+
# # hash_1 = ActiveSupport::HashWithIndifferentAccess.new
|
129
|
+
# # hash_1[:key] = 'value'
|
130
|
+
# #
|
131
|
+
# # hash_2 = ActiveSupport::HashWithIndifferentAccess.new
|
132
|
+
# # hash_2[:key] = 'New Value!'
|
133
|
+
# #
|
134
|
+
# # hash_1.update(hash_2) # => {"key"=>"New Value!"}
|
135
|
+
# #
|
136
|
+
# # The argument can be either an
|
137
|
+
# # <tt>ActiveSupport::HashWithIndifferentAccess</tt> or a regular +Hash+.
|
138
|
+
# # In either case the merge respects the semantics of indifferent access.
|
139
|
+
# #
|
140
|
+
# # If the argument is a regular hash with keys +:key+ and +"key"+ only one
|
141
|
+
# # of the values end up in the receiver, but which one is unspecified.
|
142
|
+
# #
|
143
|
+
# # When given a block, the value for duplicated keys will be determined
|
144
|
+
# # by the result of invoking the block with the duplicated key, the value
|
145
|
+
# # in the receiver, and the value in +other_hash+. The rules for duplicated
|
146
|
+
# # keys follow the semantics of indifferent access:
|
147
|
+
# #
|
148
|
+
# # hash_1[:key] = 10
|
149
|
+
# # hash_2['key'] = 12
|
150
|
+
# # hash_1.update(hash_2) { |key, old, new| old + new } # => {"key"=>22}
|
151
|
+
# def update(other_hash)
|
152
|
+
# if other_hash.is_a? HashWithIndifferentAccess
|
153
|
+
# super(other_hash)
|
154
|
+
# else
|
155
|
+
# other_hash.to_hash.each_pair do |key, value|
|
156
|
+
# if block_given? && key?(key)
|
157
|
+
# value = yield(convert_key(key), self[key], value)
|
158
|
+
# end
|
159
|
+
# regular_writer(convert_key(key), convert_value(value))
|
160
|
+
# end
|
161
|
+
# self
|
162
|
+
# end
|
163
|
+
# end
|
164
|
+
|
165
|
+
# alias_method :merge!, :update
|
166
|
+
|
167
|
+
# # Checks the hash for a key matching the argument passed in:
|
168
|
+
# #
|
169
|
+
# # hash = ActiveSupport::HashWithIndifferentAccess.new
|
170
|
+
# # hash['key'] = 'value'
|
171
|
+
# # hash.key?(:key) # => true
|
172
|
+
# # hash.key?('key') # => true
|
173
|
+
# def key?(key)
|
174
|
+
# super(convert_key(key))
|
175
|
+
# end
|
176
|
+
|
177
|
+
# alias_method :include?, :key?
|
178
|
+
# alias_method :has_key?, :key?
|
179
|
+
# alias_method :member?, :key?
|
180
|
+
|
181
|
+
# # Same as <tt>Hash#[]</tt> where the key passed as argument can be
|
182
|
+
# # either a string or a symbol:
|
183
|
+
# #
|
184
|
+
# # counters = ActiveSupport::HashWithIndifferentAccess.new
|
185
|
+
# # counters[:foo] = 1
|
186
|
+
# #
|
187
|
+
# # counters['foo'] # => 1
|
188
|
+
# # counters[:foo] # => 1
|
189
|
+
# # counters[:zoo] # => nil
|
190
|
+
# def [](key)
|
191
|
+
# super(convert_key(key))
|
192
|
+
# end
|
193
|
+
|
194
|
+
# # Same as <tt>Hash#fetch</tt> where the key passed as argument can be
|
195
|
+
# # either a string or a symbol:
|
196
|
+
# #
|
197
|
+
# # counters = ActiveSupport::HashWithIndifferentAccess.new
|
198
|
+
# # counters[:foo] = 1
|
199
|
+
# #
|
200
|
+
# # counters.fetch('foo') # => 1
|
201
|
+
# # counters.fetch(:bar, 0) # => 0
|
202
|
+
# # counters.fetch(:bar) { |key| 0 } # => 0
|
203
|
+
# # counters.fetch(:zoo) # => KeyError: key not found: "zoo"
|
204
|
+
# def fetch(key, *extras)
|
205
|
+
# super(convert_key(key), *extras)
|
206
|
+
# end
|
207
|
+
|
208
|
+
# # Returns an array of the values at the specified indices:
|
209
|
+
# #
|
210
|
+
# # hash = ActiveSupport::HashWithIndifferentAccess.new
|
211
|
+
# # hash[:a] = 'x'
|
212
|
+
# # hash[:b] = 'y'
|
213
|
+
# # hash.values_at('a', 'b') # => ["x", "y"]
|
214
|
+
# def values_at(*indices)
|
215
|
+
# indices.collect { |key| self[convert_key(key)] }
|
216
|
+
# end
|
217
|
+
|
218
|
+
# # Returns an array of the values at the specified indices, but also
|
219
|
+
# # raises an exception when one of the keys can't be found.
|
220
|
+
# #
|
221
|
+
# # hash = ActiveSupport::HashWithIndifferentAccess.new
|
222
|
+
# # hash[:a] = 'x'
|
223
|
+
# # hash[:b] = 'y'
|
224
|
+
# # hash.fetch_values('a', 'b') # => ["x", "y"]
|
225
|
+
# # hash.fetch_values('a', 'c') { |key| 'z' } # => ["x", "z"]
|
226
|
+
# # hash.fetch_values('a', 'c') # => KeyError: key not found: "c"
|
227
|
+
# def fetch_values(*indices, &block)
|
228
|
+
# indices.collect { |key| fetch(key, &block) }
|
229
|
+
# end if Hash.method_defined?(:fetch_values)
|
230
|
+
|
231
|
+
# # Returns a shallow copy of the hash.
|
232
|
+
# #
|
233
|
+
# # hash = ActiveSupport::HashWithIndifferentAccess.new({ a: { b: 'b' } })
|
234
|
+
# # dup = hash.dup
|
235
|
+
# # dup[:a][:c] = 'c'
|
236
|
+
# #
|
237
|
+
# # hash[:a][:c] # => nil
|
238
|
+
# # dup[:a][:c] # => "c"
|
239
|
+
# def dup
|
240
|
+
# self.class.new(self).tap do |new_hash|
|
241
|
+
# set_defaults(new_hash)
|
242
|
+
# end
|
243
|
+
# end
|
244
|
+
|
245
|
+
# # This method has the same semantics of +update+, except it does not
|
246
|
+
# # modify the receiver but rather returns a new hash with indifferent
|
247
|
+
# # access with the result of the merge.
|
248
|
+
# def merge(hash, &block)
|
249
|
+
# dup.update(hash, &block)
|
250
|
+
# end
|
251
|
+
|
252
|
+
# # Like +merge+ but the other way around: Merges the receiver into the
|
253
|
+
# # argument and returns a new hash with indifferent access as result:
|
254
|
+
# #
|
255
|
+
# # hash = ActiveSupport::HashWithIndifferentAccess.new
|
256
|
+
# # hash['a'] = nil
|
257
|
+
# # hash.reverse_merge(a: 0, b: 1) # => {"a"=>nil, "b"=>1}
|
258
|
+
# def reverse_merge(other_hash)
|
259
|
+
# super(self.class.new(other_hash))
|
260
|
+
# end
|
261
|
+
# alias_method :with_defaults, :reverse_merge
|
262
|
+
|
263
|
+
# # Same semantics as +reverse_merge+ but modifies the receiver in-place.
|
264
|
+
# def reverse_merge!(other_hash)
|
265
|
+
# replace(reverse_merge(other_hash))
|
266
|
+
# end
|
267
|
+
# alias_method :with_defaults!, :reverse_merge!
|
268
|
+
|
269
|
+
# # Replaces the contents of this hash with other_hash.
|
270
|
+
# #
|
271
|
+
# # h = { "a" => 100, "b" => 200 }
|
272
|
+
# # h.replace({ "c" => 300, "d" => 400 }) # => {"c"=>300, "d"=>400}
|
273
|
+
# def replace(other_hash)
|
274
|
+
# super(self.class.new(other_hash))
|
275
|
+
# end
|
276
|
+
|
277
|
+
# # Removes the specified key from the hash.
|
278
|
+
# def delete(key)
|
279
|
+
# super(convert_key(key))
|
280
|
+
# end
|
281
|
+
|
282
|
+
# def stringify_keys!; self end
|
283
|
+
# def deep_stringify_keys!; self end
|
284
|
+
# def stringify_keys; dup end
|
285
|
+
# def deep_stringify_keys; dup end
|
286
|
+
# # undef :symbolize_keys!
|
287
|
+
# # undef :deep_symbolize_keys!
|
288
|
+
# def symbolize_keys; to_hash.symbolize_keys! end
|
289
|
+
# def deep_symbolize_keys; to_hash.deep_symbolize_keys! end
|
290
|
+
# def to_options!; self end
|
291
|
+
|
292
|
+
# def select(*args, &block)
|
293
|
+
# return to_enum(:select) unless block_given?
|
294
|
+
# dup.tap { |hash| hash.select!(*args, &block) }
|
295
|
+
# end
|
296
|
+
|
297
|
+
# def reject(*args, &block)
|
298
|
+
# return to_enum(:reject) unless block_given?
|
299
|
+
# dup.tap { |hash| hash.reject!(*args, &block) }
|
300
|
+
# end
|
301
|
+
|
302
|
+
# def transform_values(*args, &block)
|
303
|
+
# return to_enum(:transform_values) unless block_given?
|
304
|
+
# dup.tap { |hash| hash.transform_values!(*args, &block) }
|
305
|
+
# end
|
306
|
+
|
307
|
+
# def compact
|
308
|
+
# dup.tap(&:compact!)
|
309
|
+
# end
|
310
|
+
|
311
|
+
# # Convert to a regular hash with string keys.
|
312
|
+
# def to_hash
|
313
|
+
# _new_hash = Hash.new
|
314
|
+
# set_defaults(_new_hash)
|
315
|
+
|
316
|
+
# each do |key, value|
|
317
|
+
# _new_hash[key] = convert_value(value, for: :to_hash)
|
318
|
+
# end
|
319
|
+
# _new_hash
|
320
|
+
# end
|
321
|
+
|
322
|
+
# private
|
323
|
+
# def convert_key(key) # :doc:
|
324
|
+
# key.kind_of?(Symbol) ? key.to_s : key
|
325
|
+
# end
|
326
|
+
|
327
|
+
# def convert_value(value, options = {}) # :doc:
|
328
|
+
# if value.is_a? Hash
|
329
|
+
# if options[:for] == :to_hash
|
330
|
+
# value.to_hash
|
331
|
+
# else
|
332
|
+
# value.class == Hash ? HashWithIndifferentAccess.new(value) : value
|
333
|
+
# end
|
334
|
+
# elsif value.is_a?(Array)
|
335
|
+
# if options[:for] != :assignment || value.frozen?
|
336
|
+
# value = value.dup
|
337
|
+
# end
|
338
|
+
# value.map! { |e| convert_value(e, options) }
|
339
|
+
# else
|
340
|
+
# value
|
341
|
+
# end
|
342
|
+
# end
|
343
|
+
|
344
|
+
# def set_defaults(target) # :doc:
|
345
|
+
# if default_proc
|
346
|
+
# target.default_proc = default_proc.dup
|
347
|
+
# else
|
348
|
+
# target.default = default
|
349
|
+
# end
|
350
|
+
# end
|
351
|
+
# end
|
352
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# class Foo
|
2
|
+
# method_attr :name
|
3
|
+
# method_attr :param do |field, type=String, opts={}|
|
4
|
+
# opts[:name] = field
|
5
|
+
# opts[:type] ||= String
|
6
|
+
# opts
|
7
|
+
# end
|
8
|
+
|
9
|
+
# name "Test method desc"
|
10
|
+
# param :email, :email
|
11
|
+
# def test
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
|
15
|
+
# ap Foo.method_attr
|
16
|
+
# {
|
17
|
+
# "test": {
|
18
|
+
# "name": [
|
19
|
+
# "Test method desc"
|
20
|
+
# ],
|
21
|
+
# "param": [
|
22
|
+
# {
|
23
|
+
# "name": "email",
|
24
|
+
# "type": "String"
|
25
|
+
# }
|
26
|
+
# ]
|
27
|
+
# }
|
28
|
+
# }
|
29
|
+
|
30
|
+
module MethodAttributes
|
31
|
+
extend self
|
32
|
+
|
33
|
+
@@G_OPTS = {}
|
34
|
+
@@M_OPTS = {}
|
35
|
+
|
36
|
+
def define klass, param_name, &block
|
37
|
+
klass.define_singleton_method(param_name) do |*args|
|
38
|
+
@@M_OPTS[param_name] ||= []
|
39
|
+
@@M_OPTS[param_name].push block ? block.call(*args) : args[0]
|
40
|
+
end
|
41
|
+
|
42
|
+
klass.define_singleton_method(:method_added) do |name|
|
43
|
+
return unless @@M_OPTS.keys.first
|
44
|
+
|
45
|
+
@@G_OPTS[to_s] ||= {}
|
46
|
+
@@G_OPTS[to_s][name] = @@M_OPTS.dup
|
47
|
+
@@M_OPTS.clear
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def get klass, method_name=nil
|
52
|
+
return @@G_OPTS[klass.to_s] unless method_name
|
53
|
+
|
54
|
+
klass.ancestors.map(&:to_s).each do |a_klass|
|
55
|
+
v = @@G_OPTS[a_klass][method_name]
|
56
|
+
return v if v
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
###
|
62
|
+
|
63
|
+
class Object
|
64
|
+
def method_attr name=nil, &block
|
65
|
+
return MethodAttributes.get(self).or({}) if name.nil?
|
66
|
+
|
67
|
+
MethodAttributes.define self, name, &block
|
68
|
+
end
|
69
|
+
end
|
data/lib/lux/api/api.rb
CHANGED
@@ -10,9 +10,9 @@
|
|
10
10
|
# 503 Service Unavailable
|
11
11
|
|
12
12
|
class Lux::Api
|
13
|
+
attr_accessor :message, :response
|
13
14
|
|
14
15
|
BeforeAndAfter.define self, :before, :after
|
15
|
-
|
16
16
|
Lux::RescueFrom.define(self)
|
17
17
|
|
18
18
|
class << self
|
@@ -60,54 +60,55 @@ class Lux::Api
|
|
60
60
|
|
61
61
|
klass.new.call(action.to_sym, params)
|
62
62
|
end
|
63
|
-
|
64
63
|
end
|
65
64
|
|
66
|
-
|
67
|
-
@@rescue_from_ivar.call do
|
68
|
-
rescued_call(action, params)
|
69
|
-
BeforeAndAfter.execute(self, :after)
|
70
|
-
end
|
65
|
+
###
|
71
66
|
|
72
|
-
|
67
|
+
def call action, params={}
|
68
|
+
@@rescue_from_ivar.call(self) { rescued_call action, params }
|
69
|
+
|
70
|
+
BeforeAndAfter.execute(self, :after)
|
71
|
+
|
72
|
+
response.render
|
73
73
|
end
|
74
74
|
|
75
75
|
# internal method for running actions
|
76
76
|
# UserApi.new.call(:login, { email:'', pass:'' })
|
77
|
-
def rescued_call
|
77
|
+
def rescued_call action, params={}
|
78
78
|
raise ForbidenError, "Protected action call" if [:call, :rescued_call, :params, :error].index action
|
79
79
|
raise NotFoundError, "Action #{action} not found in #{self.class.to_s}" unless respond_to? action
|
80
80
|
|
81
|
-
@response
|
82
|
-
@
|
83
|
-
@params = params
|
81
|
+
@response = Lux::Api::Response.new
|
82
|
+
@params = params
|
84
83
|
@class_name = self.class.to_s.sub(/Api$/,'')
|
85
84
|
|
86
85
|
# load default object
|
87
86
|
if @params[:_id]
|
88
87
|
eval "@object = @#{@class_name.underscore} = #{@class_name}[@params[:_id].to_i]"
|
89
88
|
@params.delete(:_id)
|
90
|
-
@response
|
89
|
+
@response.meta :path, @object.path if @object.respond_to?(:path)
|
91
90
|
end
|
92
91
|
|
92
|
+
check_params_and_mock_instance_variables action
|
93
|
+
return if response.errors?
|
94
|
+
|
93
95
|
# execte api call and verify params if possible
|
94
96
|
BeforeAndAfter.execute(self, :before)
|
95
97
|
|
96
|
-
|
98
|
+
api_data = send action
|
97
99
|
|
98
|
-
api_data
|
99
|
-
format_response!(api_data)
|
100
|
+
format_response! api_data
|
100
101
|
end
|
101
102
|
|
102
103
|
def params
|
103
104
|
@params
|
104
105
|
end
|
105
106
|
|
106
|
-
def error
|
107
|
-
raise
|
107
|
+
def error what
|
108
|
+
raise Lux::Api::Error.new(what)
|
108
109
|
end
|
109
110
|
|
110
|
-
def format_response!
|
111
|
+
def format_response! api_data=nil
|
111
112
|
if api_data
|
112
113
|
# api_data = instance_eval(&res) if api_data.kind_of?(Proc)
|
113
114
|
api_data = api_data.all.map{ |el| el.attributes } if api_data.class.name == 'ActiveRecord::Relation'
|
@@ -123,27 +124,25 @@ class Lux::Api
|
|
123
124
|
api_data = api_data.attributes.reject{ |f| ['updated_by', 'updated_at', 'created_by', 'created_at'].index(f) }
|
124
125
|
end
|
125
126
|
|
126
|
-
|
127
|
-
@response[:message] = @message.to_s unless @message.nil?
|
128
|
-
# @response[:message] = api_data if !@message && api_data.kind_of?(String)
|
127
|
+
response.data = api_data
|
129
128
|
|
130
129
|
# if we define _redirect then we will be redirected to exact page
|
131
130
|
# useful for file uploads
|
132
131
|
if @params[:_redirect]
|
133
|
-
if
|
134
|
-
Lux.page.flash.error @response[:
|
135
|
-
elsif
|
132
|
+
if response.errors
|
133
|
+
Lux.page.flash.error @response[:errors].join(', ')
|
134
|
+
elsif response.message
|
136
135
|
Lux.page.flash.info @response[:message]
|
137
136
|
end
|
138
137
|
Lux.page.redirect(@params[:_redirect])
|
139
138
|
end
|
140
139
|
end
|
141
140
|
|
142
|
-
ap
|
141
|
+
ap response.render if Lux.config.log_to_stdout
|
143
142
|
end
|
144
143
|
|
145
144
|
def message(what)
|
146
|
-
|
145
|
+
response.message = what
|
147
146
|
end
|
148
147
|
|
149
148
|
end
|
@@ -1,15 +1,34 @@
|
|
1
1
|
class ApplicationApi < Lux::Api
|
2
2
|
|
3
|
+
method_attr :user do |value|
|
4
|
+
raise 'Method attr :%s for :user is not allowed value' % value unless [:user, :guest, :admin].include?(value)
|
5
|
+
value
|
6
|
+
end
|
7
|
+
|
8
|
+
before do
|
9
|
+
if params[:api_key]
|
10
|
+
User.current = User.find_by api_token: params[:api_key]
|
11
|
+
error 'Invalid api key' unless User.current
|
12
|
+
end
|
13
|
+
|
14
|
+
case @method_attr[:user].try(:first)
|
15
|
+
when :user
|
16
|
+
error 'Dostupno registriranim korinsicima' unless User.current
|
17
|
+
when :admin
|
18
|
+
error 'Samo za ADMIN usere' unless User.current.try(:is_admin)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
3
22
|
# called at the end of response
|
4
|
-
def decorate_response!
|
5
|
-
@response = data if data
|
23
|
+
def decorate_response!
|
6
24
|
if Lux.page
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
25
|
+
response.meta :ip, Lux.page.request.ip
|
26
|
+
response.meta :user, Lux.page.var.user ? Lux.page.var.user.email : nil
|
27
|
+
response.meta :http_status, Lux.page.status(200)
|
28
|
+
# response.error 'Bad request' if response.errors? && Lux.page.status != 200
|
11
29
|
end
|
12
|
-
|
30
|
+
|
31
|
+
response.render
|
13
32
|
end
|
14
33
|
|
15
34
|
def after
|
@@ -1,18 +1,19 @@
|
|
1
|
-
class Lux::Api
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
# class Lux::Api
|
2
|
+
# class << self
|
3
|
+
# # list all availabe actions in a class
|
4
|
+
# def actions
|
5
|
+
# UserApi.instance_methods - UserApi.ancestors[1].instance_methods
|
6
|
+
# instance_level_actions = UserApi.instance_methods - Object.instance_methods - [:sinatra, :instance_run, :params]
|
7
|
+
# @@actions[self.to_s].keys + instance_level_actions
|
8
|
+
# end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
10
|
+
# # get details about action
|
11
|
+
# def action_details(name)
|
12
|
+
# @@actions[self.to_s][name] ||= {}
|
13
|
+
# details = @@actions[self.to_s][name].reject { |key| [:proc].index(key) }
|
14
|
+
# details[:name] ||= "#{name.to_s} action"
|
15
|
+
# details[:action] ||= name
|
16
|
+
# details.h
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
# end
|