snails 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/snails/app.rb +103 -89
- data/lib/snails/rspec.rb +12 -0
- data/snails.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3590d188f5ba6f0e5d8ea137f194b56febf14da2
|
4
|
+
data.tar.gz: 8a5a30da16236c741b3f391fadf55911f709dc64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26578d70dd7892a27e41cbc655927e6444344f6f73198de4ae9cb72f7f49cb5abd8e9f8bdeae8614edac80c62e65741c4a2f6900c42fc8847f89f15aec7953a7
|
7
|
+
data.tar.gz: e9ad79ffe2670aa1c273b22e40778328b7c96bbb156ab5a5480a35998b3dc897c3ca348f8e08c8f8e9e0ecb8c99b24a817cdeaf72b230afa7b2ca15a7982662a
|
data/lib/snails/app.rb
CHANGED
@@ -8,87 +8,6 @@ sinatra/flash
|
|
8
8
|
|
9
9
|
module Snails
|
10
10
|
|
11
|
-
module RequiredParams
|
12
|
-
def requires!(req, hash = params)
|
13
|
-
if req.is_a?(Hash)
|
14
|
-
req.each do |k, vals|
|
15
|
-
if vals.is_a?(Array) or vals.is_a?(Hash)
|
16
|
-
halt(400, "Missing: #{k} in #{hash}") if hash[k].nil?
|
17
|
-
requires!(vals, hash[k])
|
18
|
-
else
|
19
|
-
requires!(k, hash)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
elsif req.nil? or (req.is_a?(Symbol) and hash[req].nil?) \
|
23
|
-
or (req.is_a?(Array) and req.any? { |p| hash[p].nil? })
|
24
|
-
halt(400, "Required parameters: #{req} (in #{hash})")
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class App < Sinatra::Base
|
30
|
-
|
31
|
-
def self.inherited(base)
|
32
|
-
Snails.apps << base
|
33
|
-
super
|
34
|
-
end
|
35
|
-
|
36
|
-
set :protection, except: :frame_options
|
37
|
-
set :views, Snails.root.join('lib', 'views')
|
38
|
-
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex }
|
39
|
-
set :static_paths, %w(/css /img /js /files /fonts favicon.ico)
|
40
|
-
|
41
|
-
enable :sessions
|
42
|
-
enable :method_override
|
43
|
-
enable :logging
|
44
|
-
|
45
|
-
register Sinatra::Flash
|
46
|
-
use Rack::CommonLogger, Snails.logger
|
47
|
-
use Rack::Static, urls: static_paths, root: 'public'
|
48
|
-
|
49
|
-
configure :production, :staging do
|
50
|
-
set :raise_errors, true
|
51
|
-
set :dump_errors, false
|
52
|
-
end
|
53
|
-
|
54
|
-
configure :development do
|
55
|
-
set :raise_errors, true
|
56
|
-
set :show_exceptions, true
|
57
|
-
set :log_level, Logger::DEBUG
|
58
|
-
end
|
59
|
-
|
60
|
-
helpers do
|
61
|
-
include RequiredParams
|
62
|
-
include Snails::ViewHelpers
|
63
|
-
include Sinatra::ContentFor
|
64
|
-
def logger; Snails.logger; end
|
65
|
-
end
|
66
|
-
|
67
|
-
error do
|
68
|
-
err = request.env['sinatra.error']
|
69
|
-
logger.error err.message
|
70
|
-
logger.error err.backtrace.first(3).join("\n")
|
71
|
-
halt(500, err.message)
|
72
|
-
end
|
73
|
-
|
74
|
-
not_found do
|
75
|
-
show_error(404)
|
76
|
-
end
|
77
|
-
|
78
|
-
protected
|
79
|
-
|
80
|
-
def deliver(data, code = 200, format = :json)
|
81
|
-
status(code)
|
82
|
-
content_type(format)
|
83
|
-
data.public_send("to_#{format}")
|
84
|
-
end
|
85
|
-
|
86
|
-
def show_error(code)
|
87
|
-
erb :"errors/#{code}", layout: false
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
11
|
module All
|
93
12
|
|
94
13
|
def self.registered(app)
|
@@ -166,9 +85,9 @@ module Snails
|
|
166
85
|
|
167
86
|
cwd = Pathname.new(Dir.pwd)
|
168
87
|
app.set :sprockets, Sprockets::Environment.new(cwd)
|
169
|
-
app.set :digest_assets, false
|
170
|
-
app.set :assets_prefix, app.assets_prefix
|
171
|
-
app.set :assets_paths, app.assets_paths
|
88
|
+
app.set :digest_assets, false
|
89
|
+
app.set :assets_prefix, app.respond_to?(:assets_prefix) ? app.assets_prefix : '/assets' # URL
|
90
|
+
app.set :assets_paths, app.respond_to?(:assets_paths) ? app.assets_paths : %w(assets) # source files
|
172
91
|
app.set :assets_public_path, -> { cwd.join('public', 'assets') } # output dir
|
173
92
|
app.set :assets_precompile, %w(js/main.js css/main.css)
|
174
93
|
app.set :assets_remove_digests, false
|
@@ -263,6 +182,24 @@ module Snails
|
|
263
182
|
|
264
183
|
end
|
265
184
|
|
185
|
+
module RequiredParams
|
186
|
+
def requires!(req, hash = params)
|
187
|
+
if req.is_a?(Hash)
|
188
|
+
req.each do |k, vals|
|
189
|
+
if vals.is_a?(Array) or vals.is_a?(Hash)
|
190
|
+
halt(400, "Missing: #{k} in #{hash}") if hash[k].nil?
|
191
|
+
requires!(vals, hash[k])
|
192
|
+
else
|
193
|
+
requires!(k, hash)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
elsif req.nil? or (req.is_a?(Symbol) and hash[req].nil?) \
|
197
|
+
or (req.is_a?(Array) and req.any? { |p| hash[p].nil? })
|
198
|
+
halt(400, "Required parameters: #{req} (in #{hash})")
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
266
203
|
module FormHelpers
|
267
204
|
|
268
205
|
def form_input(object, field, options = {})
|
@@ -301,6 +238,13 @@ module Snails
|
|
301
238
|
label + "<textarea #{style} tabindex='#{index}' id='#{id}' name='#{name}'>#{object.send(field)}</textarea>"
|
302
239
|
end
|
303
240
|
|
241
|
+
def form_select(object, field, option_list, options = {})
|
242
|
+
id, name, index, label = input_base(object, field, options)
|
243
|
+
style = options[:style] ? "style='#{options[:style]}'" : ''
|
244
|
+
options = form_select_options(option_list, object.send(field))
|
245
|
+
label + "<select #{style} tabindex='#{index}' id='#{id}' name='#{name}'>#{options}</select>"
|
246
|
+
end
|
247
|
+
|
304
248
|
def form_select_options(list, selected = nil)
|
305
249
|
list.map do |name, val|
|
306
250
|
val = name if val.nil?
|
@@ -322,12 +266,19 @@ module Snails
|
|
322
266
|
form_id = opts.delete(:form_id) || path.gsub(/[^a-z0-9]/, '_').gsub(/_+/, '_')
|
323
267
|
css_class = opts.delete(:css_class) || ''
|
324
268
|
input_html = opts.delete(:input_html) || ''
|
325
|
-
confirm_text = opts.delete(:confirm_text) || 'Seguro?'
|
326
269
|
submit_val = opts.delete(:value) || text
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
270
|
+
style = "style='display:inline; #{opts[:style]}'"
|
271
|
+
|
272
|
+
onsubmit = if str = opts[:onsubmit]
|
273
|
+
"onsubmit=\"#{str}\""
|
274
|
+
else
|
275
|
+
confirm_text = opts.delete(:confirm_text) || 'Seguro?'
|
276
|
+
"onsubmit='return confirm(\"#{confirm_text}\");'"
|
277
|
+
end
|
278
|
+
|
279
|
+
form_tag = "<form id='#{form_id}' method='post' action='#{url(path)}' #{style} #{onsubmit}>"
|
280
|
+
button = "<button name='submit' type='submit' class='button #{css_class}' value='#{submit_val}'>#{text}</button>"
|
281
|
+
form_tag + input_html + button + '</form>'
|
331
282
|
end
|
332
283
|
|
333
284
|
def delete_link(options = {})
|
@@ -480,4 +431,67 @@ module Snails
|
|
480
431
|
|
481
432
|
end
|
482
433
|
|
434
|
+
class App < Sinatra::Base
|
435
|
+
|
436
|
+
def self.inherited(base)
|
437
|
+
Snails.apps << base
|
438
|
+
super
|
439
|
+
end
|
440
|
+
|
441
|
+
set :protection, except: :frame_options
|
442
|
+
set :views, Snails.root.join('lib', 'views')
|
443
|
+
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex }
|
444
|
+
set :static_paths, %w(/css /img /js /files /fonts favicon.ico)
|
445
|
+
|
446
|
+
enable :sessions
|
447
|
+
enable :method_override
|
448
|
+
enable :logging
|
449
|
+
|
450
|
+
register Sinatra::Flash
|
451
|
+
use Rack::CommonLogger, Snails.logger
|
452
|
+
use Rack::Static, urls: static_paths, root: 'public'
|
453
|
+
|
454
|
+
configure :production, :staging do
|
455
|
+
set :raise_errors, true
|
456
|
+
set :dump_errors, false
|
457
|
+
end
|
458
|
+
|
459
|
+
configure :development do
|
460
|
+
set :raise_errors, true
|
461
|
+
set :show_exceptions, true
|
462
|
+
set :log_level, Logger::DEBUG
|
463
|
+
end
|
464
|
+
|
465
|
+
helpers do
|
466
|
+
include RequiredParams
|
467
|
+
include ViewHelpers
|
468
|
+
include Sinatra::ContentFor
|
469
|
+
def logger; Snails.logger; end
|
470
|
+
end
|
471
|
+
|
472
|
+
error do
|
473
|
+
err = request.env['sinatra.error']
|
474
|
+
logger.error err.message
|
475
|
+
logger.error err.backtrace.first(3).join("\n")
|
476
|
+
halt(500, err.message)
|
477
|
+
end
|
478
|
+
|
479
|
+
not_found do
|
480
|
+
show_error(404)
|
481
|
+
end
|
482
|
+
|
483
|
+
protected
|
484
|
+
|
485
|
+
def deliver(data, code = 200, format = :json)
|
486
|
+
status(code)
|
487
|
+
content_type(format)
|
488
|
+
data.public_send("to_#{format}")
|
489
|
+
end
|
490
|
+
|
491
|
+
def show_error(code)
|
492
|
+
erb :"errors/#{code}", layout: false
|
493
|
+
end
|
494
|
+
|
495
|
+
end
|
496
|
+
|
483
497
|
end
|
data/lib/snails/rspec.rb
ADDED
data/snails.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- lib/snails.rb
|
144
144
|
- lib/snails/app.rb
|
145
145
|
- lib/snails/mailer.rb
|
146
|
+
- lib/snails/rspec.rb
|
146
147
|
- lib/snails/tasks.rb
|
147
148
|
- snails.gemspec
|
148
149
|
homepage:
|