yaframework 0.4.1 → 0.4.6

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: 23a8af14273443627664b65feb652bc03336d5fbe829f672cae4ef9c8ca4604e
4
- data.tar.gz: 848d0e46585695fe4756bf91b6c286b9cacb4ec684a535491e7a348f4a2c081a
3
+ metadata.gz: 4b91dd4a4391f3f01d24edba73230b3831874bb2449c321e7ae795b47334ef68
4
+ data.tar.gz: 8e3f1e8c24751c1591aad3c9d6602a51d270ab21ec574621dedc3a8a1a9c6df4
5
5
  SHA512:
6
- metadata.gz: 35fa5d015af06f258f7b7fff290a14a3f60329aeb2e713b4cad172f86b55bde28a01d371a114ed89b77a48862146d78c53721fa21bcd86beaa155c0cec7a13f8
7
- data.tar.gz: 47a8a7be5c253fb3a1bf788c6256613f85c9132725a328e456b8e7e27f82da322f5f2ff5691e8267dd3fce9139d0db13fcab6ca71e424436bdf1a1b165a88b3b
6
+ metadata.gz: adfc2c9f9cc37361d5be76853ff5d42435748fbe3e2816f47d4163303c013f428f793a26dd080a7e7fd65119e1f5910ab50f49aee735fdb57a5d3fed49f7fdd5
7
+ data.tar.gz: 77677d979203ecda92b147618823084b0e054f2aec87feeb322624529415ac6bad573e739eaa09a1b2946c8903fe27fa205f6ac4521e7c5d86778cff32155457
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,3 +1,24 @@
1
+ ## [0.4.6] - 2021-07-25
2
+
3
+ - Content duplication fixed
4
+
5
+ ## [0.4.5] - 2021-07-25
6
+
7
+ - Error handling added
8
+
9
+ ## [0.4.4] - 2021-07-25
10
+
11
+ - Interaction with cookies has been updated
12
+
13
+ ## [0.4.3] - 2021-07-25
14
+
15
+ - Updated the method for changing the Content-Type
16
+ - Interaction with headers has been improved
17
+
18
+ ## [0.4.2] - 2021-07-24
19
+
20
+ - DB module removed
21
+
1
22
  ## [0.4.1] - 2021-07-24
2
23
 
3
24
  - A little fix. Fixed an error with the wrong order of declaring variables (lol)
data/Gemfile CHANGED
@@ -3,7 +3,3 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
-
7
- gem "minitest", "~> 5.0"
8
- gem "pg", "~> 1.2", ">= 1.2.3"
9
- 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.0)
5
- rack (~> 2.2)
4
+ yaframework (0.4.6)
5
+ rack (~> 2.2, >= 2.2.3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -10,39 +10,36 @@ 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
- bundler (~> 2.1.4)
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!
46
43
 
47
44
  BUNDLED WITH
48
- 2.1.4
45
+ 2.2.16
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Yaframework
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/yaframework.svg)](https://badge.fury.io/rb/sinatra)
3
+ [![Gem Version](https://badge.fury.io/rb/yaframework.svg)](https://badge.fury.io/rb/yaframework)
4
4
  [![Build Status](https://app.travis-ci.com/maxbarsukov/yaframework.svg?token=T4CL2EqKG6FY816F3W3F&branch=master)](https://app.travis-ci.com/maxbarsukov/yaframework)
5
5
 
6
6
  Yaframework is a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for
File without changes
data/examples/README.md CHANGED
@@ -2,16 +2,27 @@
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/tree/master/examples/hello_world.rb)
6
6
 
7
- A simple application that displays "Hello world" to you.
7
+ A simple application that displays "Hello world" to you. Go to `http://localhost:4567/` and see `Hello World`!
8
8
 
9
9
 
10
- 2. ###[Params](https://github.com/maxbarsukov/yaframework/tree/master/examples/params)
10
+ 2. [**Params**](https://github.com/maxbarsukov/yaframework/tree/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/tree/master/examples/halts_and_redirects.rb)
16
16
 
17
17
  Shows how to use halts and redirects
18
+
19
+
20
+ ## Installation
21
+
22
+ Clone this repo and go to this folder.
23
+ Then, run `bundle install` to install this gem.
24
+
25
+ ## Run
26
+
27
+ Run with `ruby hello-world.rb` or any other file and view at http://localhost:4567
28
+
@@ -0,0 +1,23 @@
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>.<br/>JSON <a href=\"/json\">here</a>,
12
+ <br/>Plain text <a href=\"/text\">here</a>"
13
+ end
14
+
15
+ app.get "/text" do
16
+ response.text "Just plain text.<br/>Boring, even tags don't work..."
17
+ end
18
+
19
+ app.get "/json" do
20
+ response.json "{ \"The awesomeness of this framework\": \"100/100\" }"
21
+ end
22
+
23
+ app.listen(4567)
@@ -0,0 +1,12 @@
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
+
9
+ end
10
+
11
+
12
+ 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"
8
+ end
9
+
10
+ app.handle 404 do
11
+ "Hey, I just handled a 404 error!"
12
+ end
13
+
14
+ app.handle 500 do
15
+ "Error 500, this shouldn't have happened..."
16
+ end
17
+
18
+ app.listen(4567)
@@ -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,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)
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../../lib/yaframework"
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
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ # Any route, like /max or /ivan
7
+ app.get "/:name" do
8
+ "Hello #{request.params[:name]}!"
9
+ end
10
+
11
+ # You can try /max/from/rnd and see it with your own eyes
12
+ app.get "/:name/from/:bar" do
13
+ "Hello #{request.params[:name]} from #{request.params[:bar]}!"
14
+ end
15
+
16
+ app.listen(4567)
data/lib/yaframework.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  require_relative "yaframework/base"
4
4
  require_relative "yaframework/request"
5
5
  require_relative "yaframework/response"
6
- require_relative "yaframework/database"
7
6
 
8
7
  module Yaframework
9
8
  end
@@ -8,6 +8,7 @@ module Yaframework
8
8
 
9
9
  def initialize
10
10
  @routes = Hash.new([])
11
+ @inbox = {}
11
12
  end
12
13
 
13
14
  %w[GET POST PATCH PUT DELETE HEAD OPTIONS].each do |verb|
@@ -27,6 +28,10 @@ module Yaframework
27
28
  throw :halt, response
28
29
  end
29
30
 
31
+ def handle(status, &block)
32
+ @inbox[status] = block
33
+ end
34
+
30
35
  def listen(port = 5000)
31
36
  Rack::Handler::WEBrick.run self, Port: port
32
37
  end
@@ -50,6 +55,11 @@ module Yaframework
50
55
  end
51
56
 
52
57
  def route_eval
58
+ if @inbox[response.status]
59
+ response.write instance_eval(&@inbox[response.status])
60
+ return response.finish
61
+ end
62
+
53
63
  route = find_route
54
64
  if route
55
65
  response.write instance_eval(&route[:handler])
@@ -2,6 +2,14 @@
2
2
 
3
3
  module Yaframework
4
4
  class Response
5
+ LOCATION = "Location"
6
+
7
+ module ContentType
8
+ HTML = "text/html"
9
+ TEXT = "text/plain"
10
+ JSON = "application/json"
11
+ end
12
+
5
13
  attr_accessor :status
6
14
  attr_reader :headers, :body
7
15
 
@@ -21,19 +29,51 @@ module Yaframework
21
29
  end
22
30
 
23
31
  def finish
24
- headers["Content-Length"] = @length.to_s unless (100..199).include?(status) || status == 204
32
+ @headers[Rack::CONTENT_LENGTH] = @length.to_s unless (100..199).include?(status) || status == 204
25
33
  [status, headers, body]
26
34
  end
27
35
 
28
36
  def redirect(target, status = 302)
29
- self.status = status
30
- headers["Location"] = target
37
+ @status = status
38
+ @headers[LOCATION] = target
31
39
  end
32
40
 
33
41
  def write(string)
34
42
  s = string.to_s
35
43
  @length += s.bytesize
36
- body << s
44
+ @body << s
45
+ nil
46
+ end
47
+
48
+ def [](key)
49
+ @headers[key]
50
+ end
51
+
52
+ def []=(key, value)
53
+ @headers[key] = value
54
+ end
55
+
56
+ def html(str)
57
+ @headers[Rack::CONTENT_TYPE] = ContentType::HTML
58
+ write(str)
59
+ end
60
+
61
+ def text(str)
62
+ @headers[Rack::CONTENT_TYPE] = ContentType::TEXT
63
+ write(str)
64
+ end
65
+
66
+ def json(str)
67
+ @headers[Rack::CONTENT_TYPE] = ContentType::JSON
68
+ write(str)
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)
37
77
  end
38
78
  end
39
79
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yaframework
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.6"
5
5
  end
data/yaframework.gemspec CHANGED
@@ -28,9 +28,10 @@ Gem::Specification.new do |spec|
28
28
  spec.bindir = "exe"
29
29
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
30
 
31
- spec.add_development_dependency "bundler", "~> 2.1.4"
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,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaframework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.6
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
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.10
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: 2.1.4
33
+ version: '5.0'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: 2.1.4
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,14 +105,17 @@ files:
85
105
  - Rakefile
86
106
  - bin/console
87
107
  - bin/setup
108
+ - examples/Gemfile
88
109
  - examples/README.md
89
- - examples/hello-world/Gemfile
90
- - examples/hello-world/README.md
91
- - examples/hello-world/app.rb
92
- - examples/hello-world/config.ru
110
+ - examples/content_types.rb
111
+ - examples/cookies.rb
112
+ - examples/error_handling.rb
113
+ - examples/halts_and_redirects.rb
114
+ - examples/headers.rb
115
+ - examples/hello_world.rb
116
+ - examples/params.rb
93
117
  - lib/yaframework.rb
94
118
  - lib/yaframework/base.rb
95
- - lib/yaframework/database.rb
96
119
  - lib/yaframework/request.rb
97
120
  - lib/yaframework/response.rb
98
121
  - lib/yaframework/version.rb
@@ -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,18 +0,0 @@
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