yaframework 0.2.2 → 0.3.0
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/.rubocop.yml +7 -0
- data/CHANGELOG.md +11 -1
- data/Gemfile +1 -0
- data/Gemfile.lock +3 -1
- data/README.md +36 -3
- data/examples/hello-world/Gemfile +1 -1
- data/examples/hello-world/Gemfile.lock +7 -3
- data/examples/hello-world/app.rb +4 -0
- data/lib/yaframework.rb +3 -0
- data/lib/yaframework/base.rb +48 -22
- data/lib/yaframework/version.rb +1 -1
- data/yaframework.gemspec +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f292d6a4e199c7cbd9d9375649f311bfc3889c86212d8a30316a37d65685b90d
|
4
|
+
data.tar.gz: a3715a27f85ef444a996edd039323dc15e6621f518c33c278d06fceeb6cf75c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f152b5b3f1198fa48e2eb434ce38a9254970c3f4a82571d6ea9359ee531c2c39c663a65fa674c512c897d3bed269d6aa3d7858937047c22f911d5c5cfd8af73
|
7
|
+
data.tar.gz: ad9ddf07e7ce153c3b55cbb48389628dcbc055f3dd23101eede3e28040961a47cd7c83a26fe46de824c96995cf35502ab2cb49856e50fba0aa2a04f48a5d9318
|
data/.rubocop.yml
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
AllCops:
|
2
2
|
TargetRubyVersion: 2.5
|
3
|
+
SuggestExtensions: false
|
3
4
|
|
4
5
|
Style/StringLiterals:
|
5
6
|
Enabled: true
|
@@ -9,5 +10,11 @@ Style/StringLiteralsInInterpolation:
|
|
9
10
|
Enabled: true
|
10
11
|
EnforcedStyle: double_quotes
|
11
12
|
|
13
|
+
Style/Documentation:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Metrics/MethodLength:
|
17
|
+
Enabled: false
|
18
|
+
|
12
19
|
Layout/LineLength:
|
13
20
|
Max: 120
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
yaframework (0.2.
|
4
|
+
yaframework (0.2.2)
|
5
5
|
rack (~> 2.2)
|
6
6
|
|
7
7
|
GEM
|
@@ -12,6 +12,7 @@ GEM
|
|
12
12
|
parallel (1.20.1)
|
13
13
|
parser (3.0.2.0)
|
14
14
|
ast (~> 2.4.1)
|
15
|
+
pg (1.2.3)
|
15
16
|
rack (2.2.3)
|
16
17
|
rainbow (3.0.0)
|
17
18
|
rake (13.0.6)
|
@@ -37,6 +38,7 @@ PLATFORMS
|
|
37
38
|
DEPENDENCIES
|
38
39
|
bundler (~> 2.1.4)
|
39
40
|
minitest (~> 5.0)
|
41
|
+
pg (~> 1.2, >= 1.2.3)
|
40
42
|
rack (~> 2.2, >= 2.2.3)
|
41
43
|
rake (~> 13.0)
|
42
44
|
rubocop (~> 1.7)
|
data/README.md
CHANGED
@@ -1,8 +1,41 @@
|
|
1
1
|
# Yaframework
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/sinatra)
|
4
|
+
[](https://app.travis-ci.com/maxbarsukov/yaframework)
|
4
5
|
|
5
|
-
|
6
|
+
Yaframework is a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for
|
7
|
+
quickly creating web applications in Ruby with minimal effort:
|
8
|
+
|
9
|
+
> _Wait, I've seen this somewhere..._
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
# app.rb
|
13
|
+
require 'yaframework'
|
14
|
+
app = Yaframework::Application
|
15
|
+
|
16
|
+
app.get "/" do
|
17
|
+
"Hello world!"
|
18
|
+
end
|
19
|
+
|
20
|
+
app.listen(4567)
|
21
|
+
```
|
22
|
+
|
23
|
+
Install the gem:
|
24
|
+
|
25
|
+
```shell
|
26
|
+
gem install yaframework
|
27
|
+
```
|
28
|
+
|
29
|
+
And run with:
|
30
|
+
|
31
|
+
```shell
|
32
|
+
ruby app.rb
|
33
|
+
```
|
34
|
+
|
35
|
+
View at [http://localhost:4567](http://localhost:4567)
|
36
|
+
|
37
|
+
The code you changed will not take effect until you restart the server.
|
38
|
+
Please restart the server every time you change.
|
6
39
|
|
7
40
|
## Installation
|
8
41
|
|
@@ -22,7 +55,7 @@ Or install it yourself as:
|
|
22
55
|
|
23
56
|
## Usage
|
24
57
|
|
25
|
-
|
58
|
+
You can see usage examples in the [**examples**](https://github.com/maxbarsukov/yaframework/tree/master/examples) folder
|
26
59
|
|
27
60
|
## Development
|
28
61
|
|
@@ -1,15 +1,19 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /home/max/prog/ruby/yaframework
|
3
|
+
specs:
|
4
|
+
yaframework (0.3.0)
|
5
|
+
rack (~> 2.2)
|
6
|
+
|
1
7
|
GEM
|
2
8
|
remote: https://rubygems.org/
|
3
9
|
specs:
|
4
10
|
rack (2.2.3)
|
5
|
-
yaframework (0.2.0)
|
6
|
-
rack (~> 2.2)
|
7
11
|
|
8
12
|
PLATFORMS
|
9
13
|
ruby
|
10
14
|
|
11
15
|
DEPENDENCIES
|
12
|
-
yaframework
|
16
|
+
yaframework!
|
13
17
|
|
14
18
|
BUNDLED WITH
|
15
19
|
2.1.4
|
data/examples/hello-world/app.rb
CHANGED
data/lib/yaframework.rb
CHANGED
data/lib/yaframework/base.rb
CHANGED
@@ -4,38 +4,28 @@ require "rack"
|
|
4
4
|
|
5
5
|
module Yaframework
|
6
6
|
class Base
|
7
|
-
attr_reader :routes, :request, :response, :env
|
7
|
+
attr_reader :routes, :request, :response, :env, :hello
|
8
8
|
|
9
9
|
def initialize
|
10
|
-
@routes = {}
|
10
|
+
@routes = Hash.new { |h, k| h[k] = [] }
|
11
|
+
# @routes = Hash.new([])
|
11
12
|
end
|
12
13
|
|
13
14
|
%w[GET POST PATCH PUT DELETE HEAD OPTIONS].each do |verb|
|
14
15
|
define_method :"#{verb.downcase}" do |path, &handler|
|
15
|
-
|
16
|
+
add_route(verb, path, &handler)
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
20
|
def call(env)
|
20
|
-
@request = Rack::Request.new(env)
|
21
|
-
@response = Rack::Response.new
|
22
|
-
@params = request.params
|
23
21
|
@env = env
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
handler = @routes.fetch(verb, {}).fetch(path, nil)
|
29
|
-
|
30
|
-
if handler
|
31
|
-
result = instance_eval(&handler)
|
32
|
-
return result.instance_of?(String) ? [200, {}, [result]] : result
|
33
|
-
end
|
34
|
-
[404, {}, ["Route for #{verb} #{path} not found"]]
|
22
|
+
@request = Yaframework::Request.new @env
|
23
|
+
@response = Yaframework::Response.new
|
24
|
+
catch(:halt) { route_eval }
|
35
25
|
end
|
36
26
|
|
37
|
-
def
|
38
|
-
|
27
|
+
def halt(response)
|
28
|
+
throw :halt, response
|
39
29
|
end
|
40
30
|
|
41
31
|
def listen(port = 5000)
|
@@ -44,11 +34,47 @@ module Yaframework
|
|
44
34
|
|
45
35
|
private
|
46
36
|
|
47
|
-
def
|
37
|
+
def add_route(verb, path, &handler)
|
48
38
|
path = "/#{path}" unless path[0] == "/"
|
49
39
|
|
50
|
-
@routes[verb] ||=
|
51
|
-
@routes[verb]
|
40
|
+
@routes[verb] ||= []
|
41
|
+
@routes[verb] << compile_route(path, &handler)
|
42
|
+
end
|
43
|
+
|
44
|
+
def compile_route(path, &handler)
|
45
|
+
route = { handler: handler, compiled_path: nil, extra_params: [], path: path }
|
46
|
+
|
47
|
+
compiled_path = path.gsub(/:\w+/) do |match|
|
48
|
+
route[:extra_params] << match.gsub(":", "").to_sym
|
49
|
+
"([^/?#]+)"
|
50
|
+
end
|
51
|
+
route[:compiled_path] = /^#{compiled_path}$/
|
52
|
+
route
|
53
|
+
end
|
54
|
+
|
55
|
+
def route_eval
|
56
|
+
route = find_route
|
57
|
+
if route
|
58
|
+
response.write instance_eval(&route[:handler])
|
59
|
+
else
|
60
|
+
response.status = 404
|
61
|
+
end
|
62
|
+
|
63
|
+
response.finish
|
64
|
+
end
|
65
|
+
|
66
|
+
def find_route
|
67
|
+
route = @routes[request.request_method].detect do |r|
|
68
|
+
r[:compiled_path] =~ request.path_info
|
69
|
+
end
|
70
|
+
|
71
|
+
if route
|
72
|
+
$LAST_MATCH_INFO.captures.each_with_index do |value, index|
|
73
|
+
param = route[:extra_params][index]
|
74
|
+
request.params[param] = value
|
75
|
+
end
|
76
|
+
end
|
77
|
+
route
|
52
78
|
end
|
53
79
|
end
|
54
80
|
|
data/lib/yaframework/version.rb
CHANGED
data/yaframework.gemspec
CHANGED
@@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
|
|
27
27
|
end
|
28
28
|
spec.bindir = "exe"
|
29
29
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
-
spec.require_paths = ["lib"]
|
31
30
|
|
32
31
|
spec.add_development_dependency "bundler", "~> 2.1.4"
|
33
32
|
spec.add_development_dependency "rake", "~> 13.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaframework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maxbarsukov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|