yaframework 0.2.2 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +27 -1
- data/Gemfile +1 -0
- data/Gemfile.lock +3 -1
- data/README.md +36 -3
- data/examples/README.md +9 -2
- data/examples/halts-and-redirects/Gemfile +5 -0
- data/examples/halts-and-redirects/README.md +12 -0
- data/examples/halts-and-redirects/app.rb +19 -0
- data/examples/halts-and-redirects/config.ru +6 -0
- data/examples/params/Gemfile +5 -0
- data/examples/params/README.md +12 -0
- data/examples/params/app.rb +14 -0
- data/examples/params/config.ru +6 -0
- data/lib/yaframework.rb +2 -0
- data/lib/yaframework/base.rb +43 -21
- data/lib/yaframework/request.rb +12 -0
- data/lib/yaframework/response.rb +39 -0
- data/lib/yaframework/version.rb +1 -1
- data/yaframework.gemspec +1 -2
- metadata +16 -7
- data/examples/hello-world/Gemfile.lock +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8009597e2e41965f0ad078918d629c4b88eaf2536d9c001053079cdfd24705a
|
4
|
+
data.tar.gz: 44386071847fd8a44299d967a8d8605b615a3fc865afd21a4f555410c10b3cba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 505e79eee033484f4ecd2eb08daca91521e2d93f5b780051fe82968d9a5510b0ac5152d4cc61aee77827e85d1e40d053451881e623e9f0f713fc1e0f0e03b1fa
|
7
|
+
data.tar.gz: 842210c79e38f9c984018c38377f704d235bd4f795ce3753cd89fdcddceb9c2d93db654c94d68dec23a4de818ade9719a08d382f5b6abb4fed5d29b5259cf8a1
|
data/.gitignore
CHANGED
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,17 @@ 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
|
+
|
19
|
+
Metrics/AbcSize:
|
20
|
+
Max: 25
|
21
|
+
|
12
22
|
Layout/LineLength:
|
13
23
|
Max: 120
|
24
|
+
|
25
|
+
Style/SpecialGlobalVars:
|
26
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,30 @@
|
|
1
|
-
## [
|
1
|
+
## [0.4.2] - 2021-07-24
|
2
|
+
|
3
|
+
- DB module removed
|
4
|
+
|
5
|
+
## [0.4.1] - 2021-07-24
|
6
|
+
|
7
|
+
- A little fix. Fixed an error with the wrong order of declaring variables (lol)
|
8
|
+
|
9
|
+
## [0.4.0] - 2021-07-24
|
10
|
+
|
11
|
+
- Global routing update and large-scale code base changes
|
12
|
+
|
13
|
+
## [0.3.0] - 2021-07-24
|
14
|
+
|
15
|
+
- Added tools for interacting with the database
|
16
|
+
|
17
|
+
## [0.2.2] - 2021-07-23
|
18
|
+
|
19
|
+
- Params fixed
|
20
|
+
|
21
|
+
## [0.2.1] - 2021-07-23
|
22
|
+
|
23
|
+
- Response/env/params variables added
|
24
|
+
|
25
|
+
## [0.2.0] - 2021-07-23
|
26
|
+
|
27
|
+
- Main functionality implemented
|
2
28
|
|
3
29
|
## [0.1.0] - 2021-07-23
|
4
30
|
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
yaframework (0.
|
4
|
+
yaframework (0.4.1)
|
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
|
+
[![Gem Version](https://badge.fury.io/rb/yaframework.svg)](https://badge.fury.io/rb/sinatra)
|
4
|
+
[![Build Status](https://app.travis-ci.com/maxbarsukov/yaframework.svg?token=T4CL2EqKG6FY816F3W3F&branch=master)](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
|
|
data/examples/README.md
CHANGED
@@ -2,9 +2,16 @@
|
|
2
2
|
|
3
3
|
Here are some examples of applications created with Yaframework:
|
4
4
|
|
5
|
-
1. [
|
5
|
+
1. ###[Hello World](https://github.com/maxbarsukov/yaframework/tree/master/examples/hello-world)
|
6
6
|
|
7
7
|
A simple application that displays "Hello world" to you.
|
8
8
|
|
9
9
|
|
10
|
-
2.
|
10
|
+
2. ###[Params](https://github.com/maxbarsukov/yaframework/tree/master/examples/params)
|
11
|
+
|
12
|
+
Shows how the application parameters can be used
|
13
|
+
|
14
|
+
|
15
|
+
3. ###[Halts and Redirects](https://github.com/maxbarsukov/yaframework/tree/master/examples/halts-and-redirects)
|
16
|
+
|
17
|
+
Shows how to use halts and redirects
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "yaframework"
|
4
|
+
app = Yaframework::Application
|
5
|
+
|
6
|
+
app.get "/" do
|
7
|
+
response.redirect "/hello"
|
8
|
+
end
|
9
|
+
|
10
|
+
app.get "/hello" do
|
11
|
+
"Hello world!"
|
12
|
+
end
|
13
|
+
|
14
|
+
app.get "/error" do
|
15
|
+
response.status = 401
|
16
|
+
halt response.finish
|
17
|
+
end
|
18
|
+
|
19
|
+
app.listen(4567)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# Params
|
2
|
+
|
3
|
+
Shows how the application parameters can be used
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Clone this repo and go to this folder.
|
8
|
+
Then, run `bundle install` to install this gem.
|
9
|
+
|
10
|
+
## Run
|
11
|
+
|
12
|
+
Run with `ruby app.rb` or `rackup` and view at http://localhost:4567
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "yaframework"
|
4
|
+
app = Yaframework::Application
|
5
|
+
|
6
|
+
app.get "/:name" do
|
7
|
+
"Hello #{request.params[:name]}!"
|
8
|
+
end
|
9
|
+
|
10
|
+
app.get "/:name/foo/:bar" do
|
11
|
+
"Hello #{request.params[:name]} from #{request.params[:bar]}!"
|
12
|
+
end
|
13
|
+
|
14
|
+
app.listen(4567)
|
data/lib/yaframework.rb
CHANGED
data/lib/yaframework/base.rb
CHANGED
@@ -7,35 +7,24 @@ module Yaframework
|
|
7
7
|
attr_reader :routes, :request, :response, :env
|
8
8
|
|
9
9
|
def initialize
|
10
|
-
@routes =
|
10
|
+
@routes = Hash.new([])
|
11
11
|
end
|
12
12
|
|
13
13
|
%w[GET POST PATCH PUT DELETE HEAD OPTIONS].each do |verb|
|
14
14
|
define_method :"#{verb.downcase}" do |path, &handler|
|
15
|
-
|
15
|
+
add_route(verb, path, &handler)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
def call(env)
|
20
|
-
@request = Rack::Request.new(env)
|
21
|
-
@response = Rack::Response.new
|
22
|
-
@params = request.params
|
23
20
|
@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"]]
|
21
|
+
@request = Yaframework::Request.new @env
|
22
|
+
@response = Yaframework::Response.new
|
23
|
+
catch(:halt) { route_eval }
|
35
24
|
end
|
36
25
|
|
37
|
-
def
|
38
|
-
|
26
|
+
def halt(response)
|
27
|
+
throw :halt, response
|
39
28
|
end
|
40
29
|
|
41
30
|
def listen(port = 5000)
|
@@ -44,11 +33,44 @@ module Yaframework
|
|
44
33
|
|
45
34
|
private
|
46
35
|
|
47
|
-
def
|
36
|
+
def add_route(verb, path, &handler)
|
48
37
|
path = "/#{path}" unless path[0] == "/"
|
38
|
+
@routes[verb] << compile(path, &handler)
|
39
|
+
end
|
40
|
+
|
41
|
+
def compile(path, &handler)
|
42
|
+
route = { handler: handler, compiled_path: nil, extra_params: [], path: path }
|
49
43
|
|
50
|
-
|
51
|
-
|
44
|
+
compiled_path = path.gsub(/:\w+/) do |match|
|
45
|
+
route[:extra_params] << match.gsub(":", "").to_sym
|
46
|
+
"([^/?#]+)"
|
47
|
+
end
|
48
|
+
route[:compiled_path] = /^#{compiled_path}$/
|
49
|
+
route
|
50
|
+
end
|
51
|
+
|
52
|
+
def route_eval
|
53
|
+
route = find_route
|
54
|
+
if route
|
55
|
+
response.write instance_eval(&route[:handler])
|
56
|
+
else
|
57
|
+
response.status = 404
|
58
|
+
end
|
59
|
+
response.finish
|
60
|
+
end
|
61
|
+
|
62
|
+
def find_route
|
63
|
+
route = routes[request.request_method].detect do |r|
|
64
|
+
r[:compiled_path] =~ request.path_info
|
65
|
+
end
|
66
|
+
|
67
|
+
if route
|
68
|
+
$~.captures.each_with_index do |value, index|
|
69
|
+
param = route[:extra_params][index]
|
70
|
+
request.params[param] = value
|
71
|
+
end
|
72
|
+
end
|
73
|
+
route
|
52
74
|
end
|
53
75
|
end
|
54
76
|
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaframework
|
4
|
+
class Response
|
5
|
+
attr_accessor :status
|
6
|
+
attr_reader :headers, :body
|
7
|
+
|
8
|
+
def initialize(body = [], status = 200, headers = { "Content-Type" => "text/html; charset=utf-8" })
|
9
|
+
@body = []
|
10
|
+
@headers = headers
|
11
|
+
@status = status
|
12
|
+
@length = 0
|
13
|
+
|
14
|
+
if body.respond_to? :to_str
|
15
|
+
write body.to_str
|
16
|
+
elsif body.respond_to? :each
|
17
|
+
body.each { |i| write i.to_s }
|
18
|
+
else
|
19
|
+
raise TypeError, "Body must #respond_to? #to_str or #each"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def finish
|
24
|
+
headers["Content-Length"] = @length.to_s unless (100..199).include?(status) || status == 204
|
25
|
+
[status, headers, body]
|
26
|
+
end
|
27
|
+
|
28
|
+
def redirect(target, status = 302)
|
29
|
+
self.status = status
|
30
|
+
headers["Location"] = target
|
31
|
+
end
|
32
|
+
|
33
|
+
def write(string)
|
34
|
+
s = string.to_s
|
35
|
+
@length += s.bytesize
|
36
|
+
body << s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/yaframework/version.rb
CHANGED
data/yaframework.gemspec
CHANGED
@@ -27,9 +27,8 @@ 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
|
-
spec.add_development_dependency "bundler", "
|
31
|
+
spec.add_development_dependency "bundler", ">= 2.2.10"
|
33
32
|
spec.add_development_dependency "rake", "~> 13.0"
|
34
33
|
spec.add_development_dependency "rubocop", "~> 1.7"
|
35
34
|
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaframework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.2
|
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
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.
|
19
|
+
version: 2.2.10
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 2.
|
26
|
+
version: 2.2.10
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,13 +86,22 @@ files:
|
|
86
86
|
- bin/console
|
87
87
|
- bin/setup
|
88
88
|
- examples/README.md
|
89
|
+
- examples/halts-and-redirects/Gemfile
|
90
|
+
- examples/halts-and-redirects/README.md
|
91
|
+
- examples/halts-and-redirects/app.rb
|
92
|
+
- examples/halts-and-redirects/config.ru
|
89
93
|
- examples/hello-world/Gemfile
|
90
|
-
- examples/hello-world/Gemfile.lock
|
91
94
|
- examples/hello-world/README.md
|
92
95
|
- examples/hello-world/app.rb
|
93
96
|
- examples/hello-world/config.ru
|
97
|
+
- examples/params/Gemfile
|
98
|
+
- examples/params/README.md
|
99
|
+
- examples/params/app.rb
|
100
|
+
- examples/params/config.ru
|
94
101
|
- lib/yaframework.rb
|
95
102
|
- lib/yaframework/base.rb
|
103
|
+
- lib/yaframework/request.rb
|
104
|
+
- lib/yaframework/response.rb
|
96
105
|
- lib/yaframework/version.rb
|
97
106
|
- test/test_helper.rb
|
98
107
|
- test/yaframework_test.rb
|