apphttp 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/apphttp.rb +116 -10
  5. metadata +46 -26
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4cb846a2215c4c862dd838caeb74ecb4af881c6f93b6a14228d9fb16df4bb38
4
- data.tar.gz: 28ccc08e4bc6df11df826d6d294a6b6d6d19a7b8564da5bb98e0260c8403377e
3
+ metadata.gz: e0321c0751594a3b0ac3ea9ac982e4f81f8060edd0668ae4030af6a6556e3c4c
4
+ data.tar.gz: 2ea8fc4f3feda515b4f2c62e3882a9acaabd2ce78370caa1a433946abf126d4c
5
5
  SHA512:
6
- metadata.gz: 0716725be6eac3ac5ce2ed7f5380dbf3ed82b35de5b54546c5cbd0ec121b92eca878e8f686c3e91843c8d3a9d20594a6e0e7423a8a43fa25c0afb985429b6c7a
7
- data.tar.gz: f15ff123d1e6ba25acfc8273b18cb9f9d055b7443dc6e6de3dff2ce50fb842c98434bb62aae28b6ae0ab2cd54318a4aac59e620d7850fb9b8173435da30572f7
6
+ metadata.gz: 546ff95fa168993a9130feaa2cea49ef51494f3ce3a1d4b73ca38c4eb6e6533a9c079b2f781e1de1d7163345b05f43c85a302be7eb902be5e445d9ff8d941889
7
+ data.tar.gz: e81e5edc7e40d9ca5cb39d6b6e7d3513085d2fb91962c6905c3c711beb18d6640f4fe944fac0fc839743aeb25cf613bc3f1c2a5d857b3f89b225c70cf9874ef4
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -6,12 +6,19 @@
6
6
 
7
7
  require 'socket'
8
8
  require 'json'
9
+ require 'c32'
10
+ require 'kramdown'
9
11
 
10
12
 
11
13
  class AppHttp
14
+ using ColouredText
12
15
 
13
- def initialize(app, host: '0.0.0.0', port: '9232', debug: false)
16
+ def initialize(app, host: '0.0.0.0', port: '9232', filepath: '.',
17
+ headings: true, debug: false)
18
+
14
19
  @app, @host, @port, @debug = app, host, port, debug
20
+ @filepath, @headings = filepath, headings
21
+
15
22
  end
16
23
 
17
24
  def start
@@ -20,11 +27,13 @@ class AppHttp
20
27
 
21
28
  while (session = server.accept)
22
29
 
30
+
23
31
  raw_request = session.gets
24
32
  request = raw_request[/.[^\s]+(?= HTTP\/1\.\d)/].strip
25
33
 
26
- result,content_type = get(request)
27
- puts 'content_type: ' + content_type.inspect if @debug
34
+ puts ('request: ' + request.inspect).debug if @debug
35
+ result, content_type = get(request)
36
+ puts ('content_type: ' + content_type.inspect).debug if @debug
28
37
 
29
38
  if result then
30
39
  response = result
@@ -39,6 +48,7 @@ class AppHttp
39
48
  session.print "\r\n"
40
49
  session.print response
41
50
  session.close
51
+
42
52
  end
43
53
  end
44
54
 
@@ -46,6 +56,15 @@ class AppHttp
46
56
 
47
57
  def get(s)
48
58
 
59
+ if s == '/' then
60
+
61
+ fp = File.join(@filepath, 'index.html')
62
+ return (File.exists?(fp) ? File.read(fp) : default_index() )
63
+
64
+ end
65
+
66
+ return [s.to_s, 'text/plain'] if s =~ /^\/\w+\.\w+/
67
+
49
68
  args = []
50
69
 
51
70
  if s.count('/') > 1 then
@@ -53,11 +72,12 @@ class AppHttp
53
72
  name, args = *s.split('/')[1..-1]
54
73
 
55
74
  else
56
-
75
+ puts ('s: ' + s.inspect).debug if @debug
57
76
  name, raw_params = s[/(?<=^\/).*/].split('?',2)
58
77
 
59
78
  if raw_params then
60
79
 
80
+ puts ('raw_params: ' + raw_params.inspect).debug if @debug
61
81
  raw_pairs = raw_params.split('&')
62
82
 
63
83
  h = raw_pairs.inject({}) do |r,x|
@@ -86,21 +106,107 @@ class AppHttp
86
106
  if @app.respond_to? name.to_sym then
87
107
 
88
108
  begin
89
- r = @app.method(name.to_sym).call(*args)
109
+
110
+ method = @app.method(name.to_sym)
111
+
112
+ if method.arity > 0 and args.length <= 0 then
113
+ r = "
114
+ <form action='#{name}'>
115
+ <input type='text' name='arg'></input>
116
+ <input type='submit'/>
117
+ </form>"
118
+ else
119
+ r = method.call(*args)
120
+ end
121
+
122
+ fp = File.join(@filepath, File.basename(name) + '.html')
123
+
124
+ content = if File.exists?(fp) then
125
+ render_html(fp, r)
126
+ else
127
+
128
+ if @headings then
129
+ markdown = "
130
+ # #{name.capitalize}
131
+
132
+ <output>#{r}</output>
133
+ "
134
+
135
+ Kramdown::Document.new(markdown).to_html
136
+ else
137
+
138
+ r
139
+ end
140
+
141
+
142
+ end
143
+
90
144
  rescue
91
- r = ($!).inspect
145
+ content = ($!).inspect
92
146
  end
93
147
 
94
- case r.class.to_s
148
+ case content.class.to_s
95
149
  when "String"
96
- [r, 'text/plain']
150
+ media = content.lstrip[0] == '<' ? 'html' : 'plain'
151
+ [content, 'text/' + media]
97
152
  when "Hash"
98
- [r.to_json,'application/json']
153
+ [content.to_json,'application/json']
99
154
  else
100
- [r.to_s, 'text/plain']
155
+ [content.to_s, 'text/plain']
101
156
  end
102
157
  end
103
158
 
104
159
  end
160
+
161
+ def default_index()
162
+
163
+ a = (@app.public_methods - Object.public_methods).sort
164
+ s = a.map {|x| "* [%s](%s)" % [x,x]}.join("\n")
165
+
166
+ markdown = "
167
+ <html>
168
+ <head>
169
+ <title>#{@app.class.to_s}</title>
170
+ <style>
171
+ h1 {color: green}
172
+ h2 {color: orange}
173
+ div {height: 60%; overflow-y: auto; width: 200px; float: left}
174
+ </style>
175
+ </head>
176
+ <body markdown='1'>
177
+
178
+ # #{@app.class.to_s} Index
179
+
180
+ ## Public Methods
181
+
182
+ <div markdown='1'>
183
+ #{s}
184
+ </div>
185
+ <iframe name='i1'></iframe>
186
+ <div style='clear: both' />
187
+ <hr/>
188
+ </body>
189
+ </html>
190
+ "
191
+ #markdown = s
192
+ doc = Rexle.new(Kramdown::Document.new(markdown).to_html)
193
+
194
+ doc.root.xpath('body/div/ul/li/a') do |link|
195
+ link.attributes[:target] = 'i1'
196
+ end
197
+
198
+ [doc.xml(pretty: true), 'text/html']
199
+ end
200
+
201
+ def render_html(fp, s)
202
+
203
+ doc = Rexle.new(File.read fp)
204
+ e = doc.root.element('//[@class="output"]')
205
+ e.text = s
206
+
207
+
208
+ doc.xml pretty: true
209
+
210
+ end
105
211
 
106
212
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apphttp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -11,32 +11,52 @@ cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
13
  MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
14
- YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMTkwOTAzMjIzMTMxWhcN
15
- MjAwOTAyMjIzMTMxWjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
16
- cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQC+oSh7
17
- hdE7JV9vwvzzMaRIegnH2rAtqShRteD9HT+kLnCf1BA9xtiMbw7RzQ/Jjf4Yraxe
18
- rv+6p6H/F7tJVU7e8Fk1iOdsWrjnmki4/CgTXRkzYCvzPfUljh8YPFTRnhEbCWQF
19
- x1vlHa4JrW5YAn+oanQk0ERGIKhUpUwrhJUe9g4lv70ZsMMCda2/dF2ahZP3et3K
20
- IvZhXsterFmnjmd/dj9dw0O0qID4BdHQq800Ef7tCxfUkHbKGtTOOJJAQYpDvLV5
21
- Xj+QPmIPKZ04X3ahLppdh5dQnzC7RgqBRIQhFHHqbEpXBYm00e+YjqZyx5QWwoOp
22
- niecdDuF9dmNez9Sh7yDTy2szFfozzBLIE0ZF4HMFHz2kAEA2JDPKLxsNOU+CdP7
23
- xTm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4t4gTxsdBUc1sx4VxPCBWhME6
33
+ cEYKfr3lvkt7pd36ZrQDLh+e6bUMWosF67in6gkHqEHI+cxDs3bPjmMxO9pMDlLW
34
+ ET0FcDwN5GiQc5tl1FtwGt55M7s6AsLwIDGxQrxFNJwVQw0x9hfMBebLBtldy1sN
35
+ fhU7xyc9cI6elrvCBt5DlvbaQn1ZdmYHfPPe4BgcB7hg9ADcQubuuZJy+KvRZADs
36
+ h/GUx4K959hkyRunR+SmqC7C
37
37
  -----END CERTIFICATE-----
38
- date: 2019-09-03 00:00:00.000000000 Z
39
- dependencies: []
38
+ date: 2020-10-25 00:00:00.000000000 Z
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: kramdown
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.3.0
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.3'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '2.3'
40
60
  description:
41
61
  email: james@jamesrobertson.eu
42
62
  executables: []
@@ -63,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
83
  - !ruby/object:Gem::Version
64
84
  version: '0'
65
85
  requirements: []
66
- rubygems_version: 3.0.1
86
+ rubygems_version: 3.0.3
67
87
  signing_key:
68
88
  specification_version: 4
69
89
  summary: Makes it trivial to web enable a Ruby project.
metadata.gz.sig CHANGED
Binary file