erio 0.0.1.1 → 0.0.2.0.pre.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fad8b7185fc96b49a17922cc7f2c20e5efd44151b733b07de05a73bd193bd3b9
4
- data.tar.gz: 82c45e8763b8c1ccf18845b411e9056ffcc30161ab15ebee7cb4f53643634cf9
3
+ metadata.gz: 5d5c1b80873792c4fa9836c67b117d086cf01af31293523432fad915e302861f
4
+ data.tar.gz: 14bd1442c2790407a3c88989badf46327b0faf387afc752a4ab0e3897dcdb328
5
5
  SHA512:
6
- metadata.gz: 5f10bc473794c43f0bdbb07578cf754d6fa14462f00c763d3c12265b492848dff4ebcdc307fe5ca28d66c92d771a43993c65cd7972d16d652fe72feb069dfd4d
7
- data.tar.gz: cec14bb2d9b93afb8e959fb76de8dc6a6a10dd7fd89404ae77747767a9258490ed27d13b2c1164a7ee13a1aaccf83e778132864907a33b3d7600a5c18b2d35b6
6
+ metadata.gz: 97c214cc70aaee7875f14bf0e61cf417b57d335f9b9964435066699c4adcfdfba68eae59078c668473797b87a370f526dcb2c4684d9c4657046cce1d06818c4f
7
+ data.tar.gz: 5328a3160bc2ece029bffa78af36f2571702d52275673d914e293136302d12c3a93c89ab93f66faf1d88eb389c793c0ff605982c1adf25ca673d09a3f4e7acd8
data/CHANGELOG.md CHANGED
@@ -7,3 +7,8 @@
7
7
  ## [0.0.1] - 2025-03-14
8
8
 
9
9
  - Add a large of funs
10
+
11
+ ## [0.0.2] - 2025-03-23
12
+
13
+ - Add a mapper for route
14
+ - Add some demo
data/demo/blog.ru ADDED
@@ -0,0 +1,34 @@
1
+ require './lib/erio'
2
+
3
+ class Blog < Erio
4
+ enter do
5
+ # on 'hello' do
6
+ # on 'daddy' do
7
+ # on '' do
8
+ # res.write 'hi, i\'m fine.'
9
+ # end
10
+ # end
11
+ # on true do
12
+ # res.write 'hi, nobody.'
13
+ # end
14
+ # end
15
+ # on Integer do |id|
16
+ # res.write "int: #{id}"
17
+ # pp 'int'
18
+ # end
19
+ # on Numeric do |id|
20
+ # res.write "num: #{id}"
21
+ # pp 'num'
22
+ # end
23
+ # on '' do
24
+ # res.write 'index'
25
+ # end
26
+ pp path
27
+ if path =~ /\A\/(\d+)\z/
28
+ res.write "Hi, Your ID is #{$1}!"
29
+ run proc { res.write 'Fuck you!' }
30
+ end
31
+ end
32
+ end
33
+
34
+ run Blog
data/demo/map.ru ADDED
@@ -0,0 +1,31 @@
1
+ require './lib/erio'
2
+
3
+ class App < Erio
4
+ enter do
5
+ is do
6
+ res.write 'home'
7
+ end
8
+ on 'hello' do
9
+ is do
10
+ res.write 'hi'
11
+ end
12
+ is 'me' do
13
+ res.write 'hello, self?'
14
+ end
15
+ end
16
+
17
+ also
18
+
19
+ on req.params.empty? do
20
+ res.write "\nNo Parameters."
21
+ end
22
+
23
+ also
24
+
25
+ on accept? 'text' do
26
+ res.write "\nAccept: Text."
27
+ end
28
+ end
29
+ end
30
+
31
+ run App
data/lib/erio/short.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  class << Erio
2
- def ip; @_env['REMOTE_ADDR'] end
3
- def user_agent; @_env['HTTP_USER_AGENT'] end
4
- def req_body; @_env['rack.input'].read end
5
- def req_scheme; @_env['rack.url_scheme'] end
6
- def req_host; @_env['HTTP_HOST'] end
7
- def query; @_env['QUERY_STRING'] end
2
+ def ip; @env['REMOTE_ADDR'] end
3
+ def user_agent; @env['HTTP_USER_AGENT'] end
4
+ def req_body; @env['rack.input'].read end
5
+ def req_scheme; @env['rack.url_scheme'] end
6
+ def req_host; @env['HTTP_HOST'] end
7
+ def query; @env['QUERY_STRING'] end
8
8
 
9
9
  def decode_www str
10
10
  str.gsub('+','%2B').gsub(/%([\da-fA-F]{2})/) { $1.to_i(16).chr }
@@ -14,7 +14,7 @@ class << Erio
14
14
  end
15
15
 
16
16
  def queries q_str=nil
17
- (q_str || @_env['QUERY_STRING']).split('&')
17
+ (q_str || @env['QUERY_STRING']).split('&')
18
18
  .map { k,v = [*_1.split('='),''][0,2]; [k, decode_www(v)] }.to_h
19
19
  end
20
20
 
@@ -40,14 +40,14 @@ class << Erio
40
40
  File.binread filename
41
41
  end
42
42
 
43
- def request; Rack::Request.new(@_env) end
44
- def header(**kws); kws.empty? ? @header : @header.merge!(kws.to_a.map { [_1.to_s.gsub('_','-'),_2] }.to_h) end
45
- def status(s=nil); s ? @status=s : @status end
46
- def path; @_env['PATH_INFO'] end
47
- def path? pattern=nil; block_given? ? (yield if pattern === path) : path end
48
- def verb word=nil; block_given? ? (yield if verb == word) : @_env['REQUEST_METHOD'] end
43
+ def req; Rack::Request.new(@env) end
44
+ def header **kws; kws.empty? ? @header : @header.merge!(kws.to_a.map { [_1.to_s.tr('_','-'),_2] }.to_h) end
45
+ def status s=nil; s ? @status=s : @status end
46
+ def path; @env['REQUEST_PATH'] end
47
+ def path? pattern=nil; block_given? ? (yield if pattern === path) : pattern === path end
48
+ def verb word=nil; block_given? ? (yield if verb == word) : @env['REQUEST_METHOD'] end
49
49
  def body str=nil; str ? @body=str : @body end
50
- def accept; @_env['HTTP_ACCEPT'] end
50
+ def accept; @env['HTTP_ACCEPT'] end
51
51
 
52
52
  def status? s=200, &block
53
53
  bool = s === @status
@@ -56,19 +56,19 @@ class << Erio
56
56
  end
57
57
 
58
58
  def ip? cond, &block
59
- bool = cond === @_env['REMOTE_ADDR']
59
+ bool = cond === @env['REMOTE_ADDR']
60
60
  return bool unless bool && block
61
61
  block.call
62
62
  end
63
63
 
64
64
  def header? **kws, &block
65
- bool = kws.keys.map { kws[_1] === @header[_1] ? return false : true }.all?
65
+ bool = kws.keys.map { k=_1.to_s.tr('_','-'); kws[k] === @header[k] ? return false : true }.all?
66
66
  return bool unless bool && block
67
67
  block.call
68
68
  end
69
69
 
70
70
  def host? url, &block
71
- bool = url === env['HOST']
71
+ bool = url === env['HTTP_HOST']
72
72
  return bool unless bool && block
73
73
  block.call
74
74
  end
@@ -82,7 +82,7 @@ class << Erio
82
82
  end
83
83
 
84
84
  def accept? *types, &block
85
- acc = @_env['HTTP_ACCEPT']
85
+ acc = @env['HTTP_ACCEPT']
86
86
  bool = types.map do |type|
87
87
  rt = %r[\b#{type}\b]
88
88
  case type
data/lib/erio/topo.rb ADDED
@@ -0,0 +1,106 @@
1
+ class << Erio
2
+
3
+ # a map router for Erio.
4
+ # nested with `on`
5
+ # root in `is`
6
+ # repeated slashes(/) is 1 slash:
7
+ # `////` == `/`
8
+ # `/hello/` == `/hello`
9
+ #
10
+ # on 'hello' # match '/hello', '/hello/mine', '/hello/'
11
+ # # not '/helloabc'
12
+ # on param? :a # match '/?a'
13
+ # on accept? 'text' # match Header[Accept-Type] like Text.
14
+ #
15
+ # class App < Erio
16
+ # enter do
17
+ # on 'hello' do
18
+ # res.write 'hi'
19
+ # on String, res.params.empty? do |name|
20
+ # res.write "hi, #{name}!"
21
+ # end
22
+ # end
23
+ # end
24
+ # end
25
+ #
26
+ # @param Bool, String, Class, Proc match path-pattern or any condition then run
27
+ # @yield take matched args and run.
28
+ def on(*arg)
29
+ def path(p)
30
+ if @_isis
31
+ lambda {
32
+ if env['PATH_INFO'] =~ /\A(\/*#{p}\/*)\z/
33
+ # if env['PATH_INFO'] =~ /\A\/*\z/
34
+ env['SCRIPT_NAME'] += $1||''
35
+ env['PATH_INFO'] = ''
36
+ $1
37
+ end
38
+ }
39
+ else
40
+ lambda {
41
+ if env['PATH_INFO'] =~ /\A\/(#{p})(\/|\z)/
42
+ env['SCRIPT_NAME'] += "/#{$1}"
43
+ env['PATH_INFO'] = $2 + $'
44
+ $1
45
+ end
46
+ }
47
+ end
48
+ end
49
+
50
+ def match(pat)
51
+ case pat
52
+ when Class
53
+ case pat
54
+ when Numeric; path('\\d+(?:\\.\\d+)?').call
55
+ when Integer; path('\\d+').call
56
+ when String; path('[^\\/]+').call
57
+ end
58
+ when String, Numeric; path(pat).call
59
+ when Regexp; path(pat.source).call
60
+ when true, false; pat
61
+ when Proc; pat.call
62
+ end
63
+ end
64
+
65
+ return if @_matched
66
+ s, p = env['SCRIPT_NAME'], env['PATH_INFO']
67
+ yield *arg.map { |pat| match(pat) || (@_isis=false; return) }
68
+ env['SCRIPT_NAME'], env['PATH_INFO'] = s, p
69
+ @_matched = true
70
+ end
71
+ #
72
+ # match path excluded rest characters
73
+
74
+ # is 'hi' # match '/hi' not match '/hi/123'
75
+ # is 'hi/mine' # match '/hi/mine'
76
+ #
77
+ # @param String, Regexp whole match
78
+ # @yield run if matched
79
+ def is(s='', &block)
80
+ @_isis = true
81
+ on(s, &block)
82
+ end
83
+
84
+ # matched but also match another for run block
85
+ def also; @_matched = false; end
86
+
87
+ # alias res.finish, app needed
88
+ def finish; res.finish; end
89
+
90
+ def _call(env)
91
+ @env = env
92
+ @res = Rack::Response.new
93
+ @req = Rack::Request.new(env)
94
+ catch :erio_run_next_app do
95
+ @res.status = 404 unless @_matched
96
+ enter
97
+ return @res.finish
98
+ end.call(env)
99
+ end
100
+
101
+ attr :env, :req, :res
102
+
103
+ def run(app)
104
+ throw :erio_run_next_app, app
105
+ end
106
+ end
data/lib/erio/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Erio
4
- VERSION = "0.0.1.1"
4
+ VERSION = "0.0.2.0-1"
5
5
  end
data/lib/erio.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "erio/version"
4
4
  require_relative "erio/short"
5
+ require_relative "erio/topo"
5
6
  require'rack'
6
7
  require'rack/handler/puma'
7
8
 
@@ -57,22 +58,28 @@ class << Erio
57
58
  #
58
59
  # @yield run for rack-triad
59
60
  # @return [String, Object] maybe response body
60
- def enter &blk; blk ? @_enter = blk : @_enter.arity == 1 ? @_enter.call(self) : class_exec(&@_enter) end
61
-
62
- def _call env
63
- @_env = env
64
- @header = {}
65
- @status = nil
66
- @body = nil
67
- last_res = enter
68
- @body ||= last_res || ''
69
- [@status, @header, [@body]]
61
+ def enter(&blk)
62
+ if blk
63
+ @_enter = blk
64
+ else
65
+ @_enter.arity == 1 ? @_enter.call(self) : class_exec(&@_enter)
66
+ end
70
67
  end
71
68
 
69
+ # def _call env
70
+ # @_env = env
71
+ # @header = {}
72
+ # @status = nil
73
+ # @body = nil
74
+ # last_res = enter
75
+ # @body ||= last_res || ''
76
+ # [@status, @header, [@body]]
77
+ # end
78
+
72
79
  # create a dup to indiv variables scope
73
80
  # and call its enter.
74
81
  # returns rack-triad for rack
75
- #
82
+
76
83
  # @param env
77
84
  # @return Array<Numeric, Hash, Array<String>> triad of
78
85
  def call env
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.1
4
+ version: 0.0.2.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kozmozEnjel
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-03-14 00:00:00.000000000 Z
10
+ date: 2025-03-22 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: For Education but powerful and useful.
13
13
  email:
@@ -24,8 +24,11 @@ files:
24
24
  - README.md
25
25
  - Rakefile
26
26
  - config.ru
27
+ - demo/blog.ru
28
+ - demo/map.ru
27
29
  - lib/erio.rb
28
30
  - lib/erio/short.rb
31
+ - lib/erio/topo.rb
29
32
  - lib/erio/version.rb
30
33
  - sig/erio.rbs
31
34
  homepage: https://github.com/saisui/erio-rb
@@ -50,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
53
  - !ruby/object:Gem::Version
51
54
  version: '0'
52
55
  requirements: []
53
- rubygems_version: 3.6.2
56
+ rubygems_version: 3.6.6
54
57
  specification_version: 4
55
58
  summary: Touwa Erio - A very tiny and lightweight web framework using Rack.
56
59
  test_files: []