z4app 0.1.3 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/z4app/version.rb +1 -1
  3. data/lib/z4app.rb +116 -13
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04efb929103f5a4edd790b1c25fc41765890630a1807754cc5e15d6110dc6984
4
- data.tar.gz: 34d1d5ede05a7c934318d285fefbc3c686d61b74599acbf4e896664493d5ebe7
3
+ metadata.gz: 8aef9eea56d6c25507a813a577ff075e5e3ff79700cdfba5cb43b82fed2e3d31
4
+ data.tar.gz: 055a7302586c350cf558d58f3949a30fec43e54efd215f706b53ad16489dc96a
5
5
  SHA512:
6
- metadata.gz: e2f26afc4485a9cf288e4cbfed192c89ab12386df51c9e6ffaf3230ab8516e3418b3fb822804d1488cca1e1c57026e5fa6351f65fbf614ac9a1ac270584765cb
7
- data.tar.gz: 6c690217d2c9966a5a85b6c84e182d4f03ad5bf41fac7f07dfa82f97ca8ffed4cf69e3c56d57728aeae2f77cbbaf58b90fdb4cd699a0de4bd28451da37802561
6
+ metadata.gz: 8dd0fe9c4371415a341e82df5571ef0d998f55715b6b27d4d46d952a2cb20a044348134417d4313485baef302c37d29b38d0b1d6e65191e11b146ebf4e29aadd
7
+ data.tar.gz: a8039199eb7b9ec226c1ca1e8c6a7d98b54b6eca873ccc00dce7a38fbae3c2834afb74491f17fc215985aba25797e42155eaf8398ffb85fd66e7629b96467d4d
data/lib/z4app/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Z4app
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.6"
5
5
  COMMIT = `git log --format="%H" -n 1`.strip
6
6
  end
data/lib/z4app.rb CHANGED
@@ -13,6 +13,104 @@ require_relative "z4app/version"
13
13
  module Z4app
14
14
  class Error < StandardError; end
15
15
 
16
+ class Embed
17
+ def initialize script
18
+ @o = []
19
+ self.instance_eval script
20
+ end
21
+ def result params={}
22
+ a = [
23
+ %[<div id='embed'>],
24
+ @o.join(""),
25
+ %[</div>]
26
+ ]
27
+ return ERB.new(a.join("")).result(binding)
28
+ end
29
+ # insert param into embed.
30
+ def param key
31
+ return %[<%= params[:#{key}] %>]
32
+ end
33
+ # insert text input.
34
+ def input key, h={}
35
+ hh = { pattern: '.*', title: 'required.' }.merge(h)
36
+ @o << %[<h1 class='e'><input name="embed[#{key}]" pattern='#{hh[:pattern]}' title='#{hh[:title]}' placeholder="#{key}"></h1>]
37
+ end
38
+ # insert email field.
39
+ def email k
40
+ @o << %[<h1 class='e'><input type='email' name='embed[#{k}]' placeholder='#{k}' pattern='.+@.+' title='valid email.'></h1>]
41
+ end
42
+ # insert url field.
43
+ def url k
44
+ @o << %[<h1 class='e'><input type='url' name='embed[#{k}] placeholder='#{k}' pattern='https://.*' title='valid url.'></h1>]
45
+ end
46
+ # insert phone field.
47
+ def tel k
48
+ @o << %[<h1 class='e'><input type='tel' name='embed[#{k}] placeholder='#{k}' pattern='\d{10}' title='valid phone number.'></h1>]
49
+ end
50
+ # insert textarea.
51
+ def textarea key, h={}
52
+ hh = { placeholder: '', value: ''}.merge(h)
53
+ @o << %[<textarea name='embed[#{key}]' placeholder='#{hh[:placeholder]}'>#{hh[:value]}</textarea>]
54
+ end
55
+ # insert color.
56
+ def color k
57
+ @o << %[<h1 class='e'><input type='color' id='e_#{e}' name='embed[#{k}]'><label for='e_#{k}'>#{k}</label></h1>]
58
+ end
59
+ # insert date.
60
+ def date k
61
+ @o << %[<h1 class='e'><input type='date' name='embed[#{k}]'></h1>]
62
+ end
63
+ # insert time.
64
+ def time k
65
+ @o << %[<h1 class='e'><input type='datetime-local' name='embed[#{k}]'></h1>]
66
+ end
67
+ # insert radio button.
68
+ def radio k
69
+ @o << %[<h1 class='e'><input type="radio" id="e_#{k}" name="embed[#{k}]" value="true"><label for="e_#{k}">#{k}</label></h1>]
70
+ end
71
+ # insert checkbox.
72
+ def check k
73
+ @o << %[<h1 class='e'><input type="checkbox" id="e_#{k}" name="embed[#{k}]" value="true"><label for="e_#{k}">#{k}</label></h1>]
74
+ end
75
+ # insert selector.
76
+ def select k, *opts
77
+ o = []
78
+ [opts].flatten.each {|e| o << %[<option value="#{e}">#{e}</option>] }
79
+ @o << %[<h1 class='e'><select name="embed[#{k}]">#{o.join("")}</select></h1>]
80
+ end
81
+ # insert number field.
82
+ def number k, h={}
83
+ hh = { min: 0, max: 100, value: 1 }.merge(h)
84
+ @o << %[<h1 class='e'><input type="number" name="embed[#{k}]" value='#{hh[:value]}' min='#{hh[:min]}' max='#{hh[:max]}' placeholder='#{k}'></h1>]
85
+ end
86
+ # insert plain text.
87
+ def text t
88
+ @o << %[<h2 class='e'>#{t}</h2>]
89
+ end
90
+ # set goto field.
91
+ def goto g, h={}
92
+ a = []; h.each_pair { |k,v| a << %[#{k}=#{v}] }
93
+ @o << %[<input type='hidden' name='goto' value='/#{g}?#{a.join("&")}'>]
94
+ end
95
+ # add submit button.
96
+ def submit p
97
+ @o << %[<h1 class='e'><button id='send'>#{p}</button></h1>]
98
+ end
99
+ # set stack push object.
100
+ def stack c, o
101
+ @o << %[<input type='hidden' name='action' value='#{c}'>]
102
+ @o << %[<input type='hidden' name='push' value='#{o}'>]
103
+ end
104
+ end
105
+
106
+ ##
107
+ # generate html content.
108
+ def self.embed s, h={}
109
+ Embed.new(s).result(h)
110
+ end
111
+
112
+ ##
113
+ # overwrite event handlers
16
114
  def self.on t, k, &b
17
115
  @@APP[t][k] = b
18
116
  end
@@ -21,7 +119,9 @@ module Z4app
21
119
  obj: Hash.new { |h,k| h[k] = lambda { |*a| puts %[[z4app][obj] #{a}]; { time: Time.now.to_s }; } },
22
120
  get: Hash.new { |h,k| h[k] = lambda { |*a| puts %[[z4app][get] #{a}]; { time: Time.now.to_s }; } },
23
121
  post: Hash.new { |h,k| h[k] = lambda { |*a| puts %[[z4app][post] #{a}]; { time: Time.now.to_s }; } }
24
- }
122
+ }
123
+
124
+ # The web-app server
25
125
  class APP < Sinatra::Base
26
126
  include Z4app
27
127
  configure do
@@ -33,10 +133,15 @@ module Z4app
33
133
  end
34
134
 
35
135
  on_start { puts "[z4app] OK." }
136
+
137
+ def die!
138
+ Process.kill('TERM', Process.pid)
139
+ end
36
140
 
37
141
  # handle dumb shit
38
142
  ['robots.txt', 'favicon.ico'].each { |e| get("/#{e}") {}}
39
-
143
+
144
+ # pwa requirement
40
145
  get('/manifest.webmanifest') {
41
146
  content_type 'application/manifest+json'
42
147
  h = {
@@ -48,6 +153,7 @@ module Z4app
48
153
  return JSON.generate(h)
49
154
  }
50
155
 
156
+ # pwa requirement
51
157
  get('/service-worker.js') {
52
158
  content_type 'application/javascript'
53
159
  erb :service_worker, layout: false
@@ -70,7 +176,8 @@ module Z4app
70
176
  content_type "application/json"
71
177
  return JSON.generate(@@APP[:obj][params[:o].to_sym].call(params[:i].to_sym, request))
72
178
  }
73
-
179
+
180
+ # handle form post
74
181
  post('/:v') {
75
182
  @here = @@APP[:post][params[:v].to_sym].call(params, request)
76
183
  if params.has_key? :goto
@@ -80,24 +187,21 @@ module Z4app
80
187
  end
81
188
  }
82
189
 
83
- # handle post
190
+ # handle json post
84
191
  post('/') {
85
192
  content_type = 'application/json'
86
193
  return JSON.generate(@@APP[:post][:index].call(params, request))
87
194
  }
88
195
  end
89
-
196
+
197
+ # start server
90
198
  def self.init!
91
- @@PROC = Process.detach(fork { APP.run! { puts "[z4app][init] OK." } })
92
- end
93
- def self.proc
94
- @@PROC
95
- end
96
- def self.app
97
- @@APP
199
+ Signal.trap('INT') { APP.die!; exit!; }
200
+ Process.detach(fork { APP.run! { puts "[z4app][init] OK." } })
98
201
  end
99
202
  end
100
203
 
204
+ # load app script if available
101
205
  if File.exist? 'app.rb'
102
206
  load 'app.rb'
103
207
  puts "[z4app][config] app."
@@ -105,4 +209,3 @@ else
105
209
  puts "[z4app][config] default."
106
210
  end
107
211
 
108
- Z4app.init!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: z4app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Olson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-21 00:00:00.000000000 Z
11
+ date: 2023-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webrick