yaframework 0.4.3 → 0.4.8

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: ef685ddc4ab38b92f223ce779b2e57b27473556cdbdcb8a93bdf8269f7cb27ff
4
- data.tar.gz: f89e2a1d07968d32b50ac56fe1aa7669d7f02c31ede5d7e7d0ccb09deb48c36d
3
+ metadata.gz: 63212eea1f540904cec647bd269e65ed74c5f17569d211c5e2df1cacf5e906eb
4
+ data.tar.gz: c0d422d3fbcf9f05fb3110062730cfbfa6e943aff65112c2c5262188c79189bf
5
5
  SHA512:
6
- metadata.gz: 8bad4c17c6e830f8c2897112375da39947326a8f204db2a52dd41d99ed7c963a31f8f0b2ad05228bd50226cb4d82e995e5752c2be73f0ede1306e24b79f48ead
7
- data.tar.gz: 32ee2f926d51b6324427f295bcebfd46958da7f364d7dbbdf80270777a486e5513ce75f50725fbe1c466ac304271fb2edbef0e864e3b5441a7af8b225bca30bf
6
+ metadata.gz: 4c03d0a6d9a2e8a0b59403f4814a79f32169f06984f40a0f62f41d1ed03e3cea55ee7cb53e677d557de3763dbf1e3846152429026983d59b3871e3d813fad60f
7
+ data.tar.gz: 9f07579ed44407f75a5431f5c65d33c3f7824c133e672a0ed2278f8033307c37c2b8456eff10a10c8864ad128f69f289a4450bc0143e2d4fb9b34a7c5916953d
data/.gitignore CHANGED
@@ -13,3 +13,4 @@
13
13
  *.gem
14
14
  *.rbc
15
15
  /examples/**/Gemfile.lock
16
+ /lib/app.rb
data/CHANGELOG.md CHANGED
@@ -1,4 +1,24 @@
1
- ## [0.4.3] - 2021-07-24
1
+ ## [0.4.8] - 2021-07-25
2
+
3
+ - Before/after hooks added
4
+
5
+ ## [0.4.7] - 2021-07-25
6
+
7
+ - Handling 404 error fixed
8
+
9
+ ## [0.4.6] - 2021-07-25
10
+
11
+ - Content duplication fixed
12
+
13
+ ## [0.4.5] - 2021-07-25
14
+
15
+ - Error handling added
16
+
17
+ ## [0.4.4] - 2021-07-25
18
+
19
+ - Interaction with cookies has been updated
20
+
21
+ ## [0.4.3] - 2021-07-25
2
22
 
3
23
  - Updated the method for changing the Content-Type
4
24
  - Interaction with headers has been improved
data/Gemfile CHANGED
@@ -3,6 +3,3 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
-
7
- gem "minitest", "~> 5.0"
8
- gem "rack", "~> 2.2", ">= 2.2.3"
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaframework (0.4.2)
5
- rack (~> 2.2)
4
+ yaframework (0.4.8)
5
+ rack (~> 2.2, >= 2.2.3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -10,36 +10,33 @@ GEM
10
10
  ast (2.4.2)
11
11
  minitest (5.14.4)
12
12
  parallel (1.20.1)
13
- parser (3.0.2.0)
13
+ parser (3.0.1.1)
14
14
  ast (~> 2.4.1)
15
- pg (1.2.3)
16
15
  rack (2.2.3)
17
16
  rainbow (3.0.0)
18
- rake (13.0.6)
17
+ rake (13.0.3)
19
18
  regexp_parser (2.1.1)
20
19
  rexml (3.2.5)
21
- rubocop (1.18.4)
20
+ rubocop (1.18.2)
22
21
  parallel (~> 1.10)
23
22
  parser (>= 3.0.0.0)
24
23
  rainbow (>= 2.2.2, < 4.0)
25
24
  regexp_parser (>= 1.8, < 3.0)
26
25
  rexml
27
- rubocop-ast (>= 1.8.0, < 2.0)
26
+ rubocop-ast (>= 1.7.0, < 2.0)
28
27
  ruby-progressbar (~> 1.7)
29
28
  unicode-display_width (>= 1.4.0, < 3.0)
30
- rubocop-ast (1.8.0)
29
+ rubocop-ast (1.7.0)
31
30
  parser (>= 3.0.1.1)
32
31
  ruby-progressbar (1.11.0)
33
32
  unicode-display_width (2.0.0)
34
33
 
35
34
  PLATFORMS
36
- ruby
35
+ x86_64-linux
37
36
 
38
37
  DEPENDENCIES
39
38
  bundler (>= 2.2.10)
40
39
  minitest (~> 5.0)
41
- pg (~> 1.2, >= 1.2.3)
42
- rack (~> 2.2, >= 2.2.3)
43
40
  rake (~> 13.0)
44
41
  rubocop (~> 1.7)
45
42
  yaframework!
File without changes
data/examples/README.md CHANGED
@@ -2,16 +2,51 @@
2
2
 
3
3
  Here are some examples of applications created with Yaframework:
4
4
 
5
- 1. ###[Hello World](https://github.com/maxbarsukov/yaframework/tree/master/examples/hello-world)
5
+ 1. [**Hello World**](https://github.com/maxbarsukov/yaframework/blob/master/examples/hello_world.rb)
6
6
 
7
7
  A simple application that displays "Hello world" to you.
8
8
 
9
9
 
10
- 2. ###[Params](https://github.com/maxbarsukov/yaframework/tree/master/examples/params)
10
+ 2. [**Params**](https://github.com/maxbarsukov/yaframework/blob/master/examples/params.rb)
11
11
 
12
12
  Shows how the application parameters can be used
13
13
 
14
14
 
15
- 3. ###[Halts and Redirects](https://github.com/maxbarsukov/yaframework/tree/master/examples/halts-and-redirects)
15
+ 3. [**Halts and Redirects**](https://github.com/maxbarsukov/yaframework/blob/master/examples/halts_and_redirects.rb)
16
16
 
17
17
  Shows how to use halts and redirects
18
+
19
+
20
+ 4. [**Headers**](https://github.com/maxbarsukov/yaframework/blob/master/examples/headers.rb)
21
+
22
+ How to add and view headers.
23
+
24
+
25
+ 5. [**Error handling**](https://github.com/maxbarsukov/yaframework/blob/master/examples/error_handling.rb)
26
+
27
+ How to handle errors
28
+
29
+
30
+ 6. [**Content Types**](https://github.com/maxbarsukov/yaframework/blob/master/examples/content_types.rb)
31
+
32
+ Using json, html, or plain text
33
+
34
+
35
+ 7. [**Cookies**](https://github.com/maxbarsukov/yaframework/blob/master/examples/cookies.rb)
36
+
37
+ Adding and deleting cookies
38
+
39
+ 8. [**Hooks**](https://github.com/maxbarsukov/yaframework/blob/master/examples/before_after_hooks.rb)
40
+
41
+ How to use `before` and `after` hooks
42
+
43
+
44
+ ## Installation
45
+
46
+ Clone this repo and go to this folder.
47
+ Then, run `bundle install` to install this gem.
48
+
49
+ ## Run
50
+
51
+ Run with `ruby hello-world.rb` or any other file and view at http://localhost:4567
52
+
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.before do
7
+ @page_updates ||= 0
8
+ @page_updates += 1
9
+ end
10
+
11
+ app.after do
12
+ puts "Some log info: #{response.body}"
13
+ response.write "<br>Refresh the page and your next number will be #{@page_updates + 1}!"
14
+ end
15
+
16
+ app.get "/" do
17
+ "Hello, your num is: #{@page_updates}"
18
+ end
19
+
20
+ app.listen(4567)
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.get "/" do
7
+ response.redirect "/html"
8
+ end
9
+
10
+ app.get "/html" do
11
+ response.html "This is <b>HTML</b>, where you cat use some <small>tags</small>.
12
+ <br/>JSON <a href=\"/json\">here</a>,
13
+ <br/>Plain text <a href=\"/text\">here</a>"
14
+ end
15
+
16
+ app.get "/text" do
17
+ response.text "Just plain text.<br/>Boring, even tags don't work..."
18
+ end
19
+
20
+ app.get "/json" do
21
+ response.json "{ \"The awesomeness of this framework\": \"100/100\" }"
22
+ end
23
+
24
+ app.listen(4567)
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.get "/" do
7
+ response.set_cookie("foo", "bar")
8
+ "Your cookies, sir: #{response["Set-Cookie"]}.<br/>Go <a href=\"/delete\">here</a> to delete them"
9
+ end
10
+
11
+ app.get "/delete" do
12
+ response.delete_cookie("foo")
13
+ "Your cookies, sir: #{response["Set-Cookie"]}"
14
+ end
15
+
16
+ app.listen(4567)
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.get "/" do
7
+ "This is the root, go somewhere else..<br/>Maybe <a href=\"/asdfg\">here</a>, idk"
8
+ end
9
+
10
+ app.handle 404 do
11
+ "Hey, I just handled a 404 error!<br/>The dude from the previous page deceived you!"
12
+ end
13
+
14
+ app.handle 500 do
15
+ "Error 500, this shouldn't have happened..."
16
+ end
17
+
18
+ app.listen(4567)
@@ -8,7 +8,7 @@ app.get "/" do
8
8
  end
9
9
 
10
10
  app.get "/hello" do
11
- "Hello world!"
11
+ "Hi, you were redirected here from the root page. You can go <a href=\"/error\"></a> to get a 401 error"
12
12
  end
13
13
 
14
14
  app.get "/error" do
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.get "/" do
7
+ request[:username] = "IVAN!"
8
+ "Now 'request[:username]': #{request[:username]} contains in 'request.params': #{request.params}"
9
+ end
10
+
11
+ app.listen(4567)
@@ -3,6 +3,7 @@
3
3
  require "yaframework"
4
4
  app = Yaframework::Application
5
5
 
6
+ # Just go to the root page and say hello
6
7
  app.get "/" do
7
8
  "Hello world!"
8
9
  end
@@ -3,11 +3,13 @@
3
3
  require "yaframework"
4
4
  app = Yaframework::Application
5
5
 
6
+ # Any route, like /max or /ivan
6
7
  app.get "/:name" do
7
8
  "Hello #{request.params[:name]}!"
8
9
  end
9
10
 
10
- app.get "/:name/foo/:bar" do
11
+ # You can try /max/from/rnd and see it with your own eyes
12
+ app.get "/:name/from/:bar" do
11
13
  "Hello #{request.params[:name]} from #{request.params[:bar]}!"
12
14
  end
13
15
 
@@ -8,6 +8,9 @@ module Yaframework
8
8
 
9
9
  def initialize
10
10
  @routes = Hash.new([])
11
+ @before_hooks = []
12
+ @after_hooks = []
13
+ @inbox = {}
11
14
  end
12
15
 
13
16
  %w[GET POST PATCH PUT DELETE HEAD OPTIONS].each do |verb|
@@ -27,6 +30,18 @@ module Yaframework
27
30
  throw :halt, response
28
31
  end
29
32
 
33
+ def before(&block)
34
+ @before_hooks << block
35
+ end
36
+
37
+ def after(&block)
38
+ @after_hooks << block
39
+ end
40
+
41
+ def handle(status, &block)
42
+ @inbox[status] = block
43
+ end
44
+
30
45
  def listen(port = 5000)
31
46
  Rack::Handler::WEBrick.run self, Port: port
32
47
  end
@@ -51,11 +66,17 @@ module Yaframework
51
66
 
52
67
  def route_eval
53
68
  route = find_route
54
- if route
55
- response.write instance_eval(&route[:handler])
56
- else
57
- response.status = 404
69
+ response.status = 404 unless route
70
+
71
+ if @inbox[response.status]
72
+ response.write exec(@inbox[response.status])
73
+ return response.finish
58
74
  end
75
+
76
+ exec_before_hooks
77
+ response.write exec(route[:handler]) if route
78
+ exec_after_hooks
79
+
59
80
  response.finish
60
81
  end
61
82
 
@@ -72,6 +93,30 @@ module Yaframework
72
93
  end
73
94
  route
74
95
  end
96
+
97
+ def exec(action)
98
+ if action.respond_to? :to_sym
99
+ send(action)
100
+ else
101
+ instance_exec(&action)
102
+ end
103
+ end
104
+
105
+ def exec_before_hooks
106
+ exec_hooks @before_hooks
107
+ end
108
+
109
+ def exec_after_hooks
110
+ exec_hooks @after_hooks
111
+ end
112
+
113
+ def exec_hooks(hooks)
114
+ return true if hooks.nil?
115
+
116
+ hooks.each do |hook|
117
+ return false if exec(hook) == false
118
+ end
119
+ end
75
120
  end
76
121
 
77
122
  Application = Base.new
@@ -42,6 +42,7 @@ module Yaframework
42
42
  s = string.to_s
43
43
  @length += s.bytesize
44
44
  @body << s
45
+ nil
45
46
  end
46
47
 
47
48
  def [](key)
@@ -66,5 +67,13 @@ module Yaframework
66
67
  @headers[Rack::CONTENT_TYPE] = ContentType::JSON
67
68
  write(str)
68
69
  end
70
+
71
+ def set_cookie(key, value)
72
+ Rack::Utils.set_cookie_header!(@headers, key, value)
73
+ end
74
+
75
+ def delete_cookie(key, value = {})
76
+ Rack::Utils.delete_cookie_header!(@headers, key, value)
77
+ end
69
78
  end
70
79
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yaframework
4
- VERSION = "0.4.3"
4
+ VERSION = "0.4.8"
5
5
  end
data/yaframework.gemspec CHANGED
@@ -29,8 +29,9 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
30
 
31
31
  spec.add_development_dependency "bundler", ">= 2.2.10"
32
+ spec.add_development_dependency "minitest", "~> 5.0"
32
33
  spec.add_development_dependency "rake", "~> 13.0"
33
34
  spec.add_development_dependency "rubocop", "~> 1.7"
34
35
 
35
- spec.add_runtime_dependency "rack", "~> 2.2"
36
+ spec.add_runtime_dependency "rack", "~> 2.2", ">= 2.2.3"
36
37
  end
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.3
4
+ version: 0.4.8
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-24 00:00:00.000000000 Z
11
+ date: 2021-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.2.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +73,9 @@ dependencies:
59
73
  - - "~>"
60
74
  - !ruby/object:Gem::Version
61
75
  version: '2.2'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.2.3
62
79
  type: :runtime
63
80
  prerelease: false
64
81
  version_requirements: !ruby/object:Gem::Requirement
@@ -66,6 +83,9 @@ dependencies:
66
83
  - - "~>"
67
84
  - !ruby/object:Gem::Version
68
85
  version: '2.2'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.2.3
69
89
  description: What could be better than one more Sinatra?
70
90
  email:
71
91
  - maximbarsukov@bk.ru
@@ -85,19 +105,16 @@ files:
85
105
  - Rakefile
86
106
  - bin/console
87
107
  - bin/setup
108
+ - examples/Gemfile
88
109
  - 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
93
- - examples/hello-world/Gemfile
94
- - examples/hello-world/README.md
95
- - examples/hello-world/app.rb
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
110
+ - examples/before_after_hooks.rb
111
+ - examples/content_types.rb
112
+ - examples/cookies.rb
113
+ - examples/error_handling.rb
114
+ - examples/halts_and_redirects.rb
115
+ - examples/headers.rb
116
+ - examples/hello_world.rb
117
+ - examples/params.rb
101
118
  - lib/yaframework.rb
102
119
  - lib/yaframework/base.rb
103
120
  - lib/yaframework/request.rb
@@ -1,12 +0,0 @@
1
- # Params
2
-
3
- Shows how to use halts and redirects
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
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "yaframework"
4
- require "./app"
5
-
6
- run app
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "yaframework"
@@ -1,12 +0,0 @@
1
- # Hello world
2
-
3
- A simple application that displays "Hello world" to you.
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
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "yaframework"
4
- require "./app"
5
-
6
- run app
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "yaframework"
@@ -1,12 +0,0 @@
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
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "yaframework"
4
- require "./app"
5
-
6
- run app