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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f292d6a4e199c7cbd9d9375649f311bfc3889c86212d8a30316a37d65685b90d
4
- data.tar.gz: a3715a27f85ef444a996edd039323dc15e6621f518c33c278d06fceeb6cf75c1
3
+ metadata.gz: 33b8bd38ee7fbf3d16bdef94f830384f979ad465ff674d085378caa6a46b6971
4
+ data.tar.gz: 1da887439ec28a18fc4ee8a4812d9bf3d9f70be894636f181a6af98a965d0dd2
5
5
  SHA512:
6
- metadata.gz: 2f152b5b3f1198fa48e2eb434ce38a9254970c3f4a82571d6ea9359ee531c2c39c663a65fa674c512c897d3bed269d6aa3d7858937047c22f911d5c5cfd8af73
7
- data.tar.gz: ad9ddf07e7ce153c3b55cbb48389628dcbc055f3dd23101eede3e28040961a47cd7c83a26fe46de824c96995cf35502ab2cb49856e50fba0aa2a04f48a5d9318
6
+ metadata.gz: 6ff977d266bccebbc67aa17c7ff2116be8f51c057be070807afa4d2c6e6a090c1053f146434e3a9ed78defa9ee840dfaa08b760f4807b0976b7bef9a09b181db
7
+ data.tar.gz: a62b06535d579d4b552aec2c1a0856514d5b27628aec77e267cfa4d98bc9b4b6185d00d6c919cae5fdf198a295870a7d0a8233eb4b3df08e39c967663791887f
data/Gemfile CHANGED
@@ -5,5 +5,5 @@ source "https://rubygems.org"
5
5
  gemspec
6
6
 
7
7
  gem "minitest", "~> 5.0"
8
- gem 'pg', '~> 1.2', '>= 1.2.3'
8
+ gem "pg", "~> 1.2", ">= 1.2.3"
9
9
  gem "rack", "~> 2.2", ">= 2.2.3"
@@ -2,4 +2,4 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "yaframework", path: "/home/max/prog/ruby/yaframework"
5
+ gem "yaframework"
@@ -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
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "yaframework"
3
+ require_relative "../../lib/yaframework"
4
4
  app = Yaframework::Application
5
5
 
6
6
  app.get "/" do
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
@@ -4,11 +4,10 @@ require "rack"
4
4
 
5
5
  module Yaframework
6
6
  class Base
7
- attr_reader :routes, :request, :response, :env, :hello
7
+ attr_reader :routes, :request, :response, :env
8
8
 
9
9
  def initialize
10
- @routes = Hash.new { |h, k| h[k] = [] }
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 compile_route(path, &handler)
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 = @routes[request.request_method].detect do |r|
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
- $LAST_MATCH_INFO.captures.each_with_index do |value, index|
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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack/request"
4
+
5
+ module Yaframework
6
+ class Request < Rack::Request
7
+ def initialize(env)
8
+ env["PATH_INFO"] = "/" if env["PATH_INFO"].empty?
9
+ super
10
+ end
11
+ end
12
+ 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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yaframework
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
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.3.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