yaframework 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/examples/hello-world/Gemfile +1 -1
- data/examples/hello-world/Gemfile.lock +3 -7
- data/examples/hello-world/app.rb +1 -1
- data/lib/yaframework.rb +2 -2
- data/lib/yaframework/base.rb +7 -11
- data/lib/yaframework/database.rb +18 -0
- data/lib/yaframework/request.rb +12 -0
- data/lib/yaframework/response.rb +39 -0
- data/lib/yaframework/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33b8bd38ee7fbf3d16bdef94f830384f979ad465ff674d085378caa6a46b6971
|
4
|
+
data.tar.gz: 1da887439ec28a18fc4ee8a4812d9bf3d9f70be894636f181a6af98a965d0dd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ff977d266bccebbc67aa17c7ff2116be8f51c057be070807afa4d2c6e6a090c1053f146434e3a9ed78defa9ee840dfaa08b760f4807b0976b7bef9a09b181db
|
7
|
+
data.tar.gz: a62b06535d579d4b552aec2c1a0856514d5b27628aec77e267cfa4d98bc9b4b6185d00d6c919cae5fdf198a295870a7d0a8233eb4b3df08e39c967663791887f
|
data/Gemfile
CHANGED
@@ -1,19 +1,15 @@
|
|
1
|
-
PATH
|
2
|
-
remote: /home/max/prog/ruby/yaframework
|
3
|
-
specs:
|
4
|
-
yaframework (0.3.0)
|
5
|
-
rack (~> 2.2)
|
6
|
-
|
7
1
|
GEM
|
8
2
|
remote: https://rubygems.org/
|
9
3
|
specs:
|
10
4
|
rack (2.2.3)
|
5
|
+
yaframework (0.2.2)
|
6
|
+
rack (~> 2.2)
|
11
7
|
|
12
8
|
PLATFORMS
|
13
9
|
ruby
|
14
10
|
|
15
11
|
DEPENDENCIES
|
16
|
-
yaframework
|
12
|
+
yaframework
|
17
13
|
|
18
14
|
BUNDLED WITH
|
19
15
|
2.1.4
|
data/examples/hello-world/app.rb
CHANGED
data/lib/yaframework.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "yaframework/base"
|
4
|
+
require_relative "yaframework/request"
|
5
|
+
require_relative "yaframework/response"
|
4
6
|
require_relative "yaframework/database"
|
5
|
-
require_relative 'yaframework/request'
|
6
|
-
require_relative 'yaframework/response'
|
7
7
|
|
8
8
|
module Yaframework
|
9
9
|
end
|
data/lib/yaframework/base.rb
CHANGED
@@ -4,11 +4,10 @@ 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
|
8
8
|
|
9
9
|
def initialize
|
10
|
-
@routes = Hash.new
|
11
|
-
# @routes = Hash.new([])
|
10
|
+
@routes = Hash.new([])
|
12
11
|
end
|
13
12
|
|
14
13
|
%w[GET POST PATCH PUT DELETE HEAD OPTIONS].each do |verb|
|
@@ -18,9 +17,9 @@ module Yaframework
|
|
18
17
|
end
|
19
18
|
|
20
19
|
def call(env)
|
21
|
-
@env = env
|
22
20
|
@request = Yaframework::Request.new @env
|
23
21
|
@response = Yaframework::Response.new
|
22
|
+
@env = env
|
24
23
|
catch(:halt) { route_eval }
|
25
24
|
end
|
26
25
|
|
@@ -36,12 +35,10 @@ module Yaframework
|
|
36
35
|
|
37
36
|
def add_route(verb, path, &handler)
|
38
37
|
path = "/#{path}" unless path[0] == "/"
|
39
|
-
|
40
|
-
@routes[verb] ||= []
|
41
|
-
@routes[verb] << compile_route(path, &handler)
|
38
|
+
@routes[verb] << compile(path, &handler)
|
42
39
|
end
|
43
40
|
|
44
|
-
def
|
41
|
+
def compile(path, &handler)
|
45
42
|
route = { handler: handler, compiled_path: nil, extra_params: [], path: path }
|
46
43
|
|
47
44
|
compiled_path = path.gsub(/:\w+/) do |match|
|
@@ -59,17 +56,16 @@ module Yaframework
|
|
59
56
|
else
|
60
57
|
response.status = 404
|
61
58
|
end
|
62
|
-
|
63
59
|
response.finish
|
64
60
|
end
|
65
61
|
|
66
62
|
def find_route
|
67
|
-
route =
|
63
|
+
route = routes[request.request_method].detect do |r|
|
68
64
|
r[:compiled_path] =~ request.path_info
|
69
65
|
end
|
70
66
|
|
71
67
|
if route
|
72
|
-
|
68
|
+
$~.captures.each_with_index do |value, index|
|
73
69
|
param = route[:extra_params][index]
|
74
70
|
request.params[param] = value
|
75
71
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "pg"
|
4
|
+
|
5
|
+
class Database
|
6
|
+
def self.connect(db_name)
|
7
|
+
pg_conn = PG.connect(dbname: db_name)
|
8
|
+
new(pg_conn)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(pg_conn)
|
12
|
+
@pg_conn = pg_conn
|
13
|
+
end
|
14
|
+
|
15
|
+
def exec(sql)
|
16
|
+
@pg_conn.exec(sql).to_a
|
17
|
+
end
|
18
|
+
end
|
@@ -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
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaframework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maxbarsukov
|
@@ -93,6 +93,9 @@ files:
|
|
93
93
|
- examples/hello-world/config.ru
|
94
94
|
- lib/yaframework.rb
|
95
95
|
- lib/yaframework/base.rb
|
96
|
+
- lib/yaframework/database.rb
|
97
|
+
- lib/yaframework/request.rb
|
98
|
+
- lib/yaframework/response.rb
|
96
99
|
- lib/yaframework/version.rb
|
97
100
|
- test/test_helper.rb
|
98
101
|
- test/yaframework_test.rb
|