yaframework 0.4.7 → 0.5.1

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: 4aec9734b89fbd8a9c87fb63506f49f53df83c0f141efd612d5cdceb9d4d8c64
4
- data.tar.gz: 8d43e4889c14798d20400ae36bfa9c6b76797bf7348eac539d85c690dcf250a3
3
+ metadata.gz: b01d1643e6921880fd3630c0b5898c502a0b3ff54c602f63ac54f9d32e263b36
4
+ data.tar.gz: 602742bc864f51d7151d946710d47583e2b3a7abfe257ffb74c2f2d48418ff89
5
5
  SHA512:
6
- metadata.gz: 00af9a420fd2edffe78d77d6a2ecfc81973cbadb9543424153a6a5e689409a06d4c98a5ff54e7ddbda2a122cbe3bcd2d5b563968f8820738c6f21747269201d7
7
- data.tar.gz: ba55916ef3b9776ab8711e82af51406b5d4e00b6991acf04418fc1526f6a0aa803757b9744296180843cf201534f4ed14292ac2f738a92d00bb8d5ad11bf6925
6
+ metadata.gz: 80d9262cc6a0206ecbd058c3bb6d65c78b4abe0192924dd27685d61fd91ec90c60e85d3a3f7792e8ce6f45d4c6d1b6e30309fa6d66f3988debd0a8fa450bd1b6
7
+ data.tar.gz: 9d95222a443494ca862a16499f42662053515765c3cd2dbb06050b469b333f852c7097c1de16904704bc731a18c653a3980cd199b2dfe8738e5a1424148ce6ce
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## [0.5.1] - 2021-07-26
2
+
3
+ - `helpers` and `configure` methods added
4
+
5
+ ## [0.5.0] - 2021-07-25
6
+
7
+ - Templates rendering added
8
+
9
+ ## [0.4.9] - 2021-07-25
10
+
11
+ - Some useful aliases added
12
+
13
+ ## [0.4.8] - 2021-07-25
14
+
15
+ - Before/after hooks added
16
+
1
17
  ## [0.4.7] - 2021-07-25
2
18
 
3
19
  - Handling 404 error fixed
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaframework (0.4.7)
4
+ yaframework (0.5.1)
5
5
  rack (~> 2.2, >= 2.2.3)
6
+ tilt (~> 2.0)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
@@ -29,6 +30,7 @@ GEM
29
30
  rubocop-ast (1.7.0)
30
31
  parser (>= 3.0.1.1)
31
32
  ruby-progressbar (1.11.0)
33
+ tilt (2.0.10)
32
34
  unicode-display_width (2.0.0)
33
35
 
34
36
  PLATFORMS
data/examples/README.md CHANGED
@@ -2,21 +2,60 @@
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.rb)
5
+ 1. [**Hello World**](https://github.com/maxbarsukov/yaframework/blob/master/examples/hello_world.rb)
6
6
 
7
- A simple application that displays "Hello world" to you. Go to `http://localhost:4567/` and see `Hello World`!
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.rb)
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.rb)
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
18
 
19
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
+
40
+ 8. [**Hooks**](https://github.com/maxbarsukov/yaframework/blob/master/examples/before_after_hooks.rb)
41
+
42
+ How to use `before` and `after` hooks
43
+
44
+
45
+ 9. [**Rendering**](https://github.com/maxbarsukov/yaframework/tree/master/examples/rendering)
46
+
47
+ Rendering your `.erb` (or any else) files and partials using tilt.
48
+
49
+
50
+ 10. [**Helpers**](https://github.com/maxbarsukov/yaframework/blob/master/examples/helpers.rb)
51
+
52
+ Example of using helpers
53
+
54
+
55
+ 11. [**Configure**](https://github.com/maxbarsukov/yaframework/blob/master/examples/configure.rb)
56
+
57
+ How to configure your application
58
+
20
59
  ## Installation
21
60
 
22
61
  Clone this repo and go to this folder.
@@ -25,4 +64,3 @@ Then, run `bundle install` to install this gem.
25
64
  ## Run
26
65
 
27
66
  Run with `ruby hello-world.rb` or any other file and view at http://localhost:4567
28
-
@@ -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,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.configure do
7
+ @app_name
8
+ end
9
+
10
+ app.get "/" do
11
+ "Hey, that's #{@app_name}"
12
+ end
13
+
14
+ app.listen(9292)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../lib/yaframework"
3
+ require "yaframework"
4
4
  app = Yaframework::Application
5
5
 
6
6
  app.get "/" do
@@ -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,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.helpers do
7
+ def hello
8
+ "Hello"
9
+ end
10
+
11
+ def colon
12
+ ", "
13
+ end
14
+ end
15
+
16
+ app.helpers do
17
+ def world
18
+ "world!"
19
+ end
20
+ end
21
+
22
+ app.get "/" do
23
+ hello + colon + world
24
+ end
25
+
26
+ app.listen(9292)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.before do
7
+ @number = Random.rand
8
+ puts @number
9
+ end
10
+
11
+ app.get "/" do
12
+ render "index"
13
+ end
14
+
15
+ app.listen(4567)
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ require "./app"
5
+
6
+ run app
@@ -0,0 +1,4 @@
1
+ <h1>Look me in index.erb file</h1>
2
+
3
+ <p>It works!</p>
4
+ <%= partial "partial", number: @number %>
@@ -0,0 +1,10 @@
1
+ <html lang="en">
2
+ <head>
3
+ <meta charset="utf-8">
4
+ <title>Example app</title>
5
+ </head>
6
+
7
+ <body>
8
+ <%= content %>
9
+ </body>
10
+ </html>
@@ -0,0 +1 @@
1
+ <p>Yout number was <b><%= number %></b></p>
data/lib/yaframework.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "yaframework/base"
4
4
  require_relative "yaframework/request"
5
5
  require_relative "yaframework/response"
6
+ require_relative "yaframework/render"
6
7
 
7
8
  module Yaframework
8
9
  end
@@ -1,13 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rack"
4
+ require_relative "../yaframework/render"
4
5
 
5
6
  module Yaframework
6
7
  class Base
7
- attr_reader :routes, :request, :response, :env
8
+ include Yaframework::Render
9
+
10
+ attr_reader :routes, :request, :response, :env, :settings
11
+ alias req request
12
+ alias res response
8
13
 
9
14
  def initialize
10
15
  @routes = Hash.new([])
16
+ @before_hooks = []
17
+ @after_hooks = []
18
+ @settings = {}
11
19
  @inbox = {}
12
20
  end
13
21
 
@@ -17,6 +25,14 @@ module Yaframework
17
25
  end
18
26
  end
19
27
 
28
+ def helpers(&block)
29
+ exec(block) if block_given?
30
+ end
31
+
32
+ def configure(&block)
33
+ exec(block) if block_given?
34
+ end
35
+
20
36
  def call(env)
21
37
  @env = env
22
38
  @request = Yaframework::Request.new @env
@@ -28,6 +44,18 @@ module Yaframework
28
44
  throw :halt, response
29
45
  end
30
46
 
47
+ def before(&block)
48
+ @before_hooks << block
49
+ end
50
+
51
+ def after(&block)
52
+ @after_hooks << block
53
+ end
54
+
55
+ def params
56
+ request.params
57
+ end
58
+
31
59
  def handle(status, &block)
32
60
  @inbox[status] = block
33
61
  end
@@ -59,11 +87,14 @@ module Yaframework
59
87
  response.status = 404 unless route
60
88
 
61
89
  if @inbox[response.status]
62
- response.write instance_eval(&@inbox[response.status])
90
+ response.write exec(@inbox[response.status])
63
91
  return response.finish
64
92
  end
65
93
 
66
- response.write instance_eval(&route[:handler]) if route
94
+ exec_before_hooks
95
+ response.write exec(route[:handler]) if route
96
+ exec_after_hooks
97
+
67
98
  response.finish
68
99
  end
69
100
 
@@ -80,7 +111,32 @@ module Yaframework
80
111
  end
81
112
  route
82
113
  end
114
+
115
+ def exec(action)
116
+ if action.respond_to? :to_sym
117
+ send(action)
118
+ else
119
+ instance_exec(&action)
120
+ end
121
+ end
122
+
123
+ def exec_before_hooks
124
+ exec_hooks @before_hooks
125
+ end
126
+
127
+ def exec_after_hooks
128
+ exec_hooks @after_hooks
129
+ end
130
+
131
+ def exec_hooks(hooks)
132
+ return true if hooks.nil?
133
+
134
+ hooks.each do |hook|
135
+ return false if exec(hook) == false
136
+ end
137
+ end
83
138
  end
84
139
 
85
140
  Application = Base.new
141
+ Application.setup
86
142
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tilt"
4
+
5
+ module Yaframework
6
+ module Render
7
+ def setup(settings = {})
8
+ @settings = settings
9
+ @settings[:template_engine] ||= "erb"
10
+ @settings[:layout] ||= "layout"
11
+ @settings[:views] ||= File.expand_path("views", File.dirname($PROGRAM_NAME))
12
+ @settings[:options] ||= {
13
+ default_encoding: Encoding.default_external
14
+ }
15
+ end
16
+
17
+ def render(template, locals = {}, layout = @settings[:layout])
18
+ res.headers["Content-Type"] ||= "text/html; charset=utf-8"
19
+ res.write(view(template, locals, layout))
20
+ end
21
+
22
+ def view(template, locals = {}, layout = @settings[:layout])
23
+ partial(layout, locals.merge(content: partial(template, locals)))
24
+ end
25
+
26
+ def partial(template, locals = {})
27
+ _render(template_path(template), locals, @settings[:options])
28
+ end
29
+
30
+ def template_path(template)
31
+ dir = @settings[:views]
32
+ ext = @settings[:template_engine]
33
+ File.join(dir, "#{template}.#{ext}")
34
+ end
35
+
36
+ private
37
+
38
+ def _render(template, locals = {}, options = {}, &block)
39
+ _cache.fetch(template) { Tilt.new(template, 1, options.merge(outvar: "@_output")) }.render(self, locals, &block)
40
+ end
41
+
42
+ def _cache
43
+ Thread.current[:_cache] ||= Tilt::Cache.new
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yaframework
4
- VERSION = "0.4.7"
4
+ VERSION = "0.5.1"
5
5
  end
data/yaframework.gemspec CHANGED
@@ -33,5 +33,6 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency "rake", "~> 13.0"
34
34
  spec.add_development_dependency "rubocop", "~> 1.7"
35
35
 
36
- spec.add_runtime_dependency "rack", "~> 2.2", ">= 2.2.3"
36
+ spec.add_dependency "rack", "~> 2.2", ">= 2.2.3"
37
+ spec.add_dependency "tilt", "~> 2.0"
37
38
  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.7
4
+ version: 0.5.1
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-25 00:00:00.000000000 Z
11
+ date: 2021-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,6 +86,20 @@ dependencies:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
88
  version: 2.2.3
89
+ - !ruby/object:Gem::Dependency
90
+ name: tilt
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.0'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '2.0'
89
103
  description: What could be better than one more Sinatra?
90
104
  email:
91
105
  - maximbarsukov@bk.ru
@@ -107,15 +121,24 @@ files:
107
121
  - bin/setup
108
122
  - examples/Gemfile
109
123
  - examples/README.md
124
+ - examples/before_after_hooks.rb
125
+ - examples/configure.rb
110
126
  - examples/content_types.rb
111
127
  - examples/cookies.rb
112
128
  - examples/error_handling.rb
113
129
  - examples/halts_and_redirects.rb
114
130
  - examples/headers.rb
115
131
  - examples/hello_world.rb
132
+ - examples/helpers.rb
116
133
  - examples/params.rb
134
+ - examples/rendering/app.rb
135
+ - examples/rendering/config.ru
136
+ - examples/rendering/views/index.erb
137
+ - examples/rendering/views/layout.erb
138
+ - examples/rendering/views/partial.erb
117
139
  - lib/yaframework.rb
118
140
  - lib/yaframework/base.rb
141
+ - lib/yaframework/render.rb
119
142
  - lib/yaframework/request.rb
120
143
  - lib/yaframework/response.rb
121
144
  - lib/yaframework/version.rb