yaframework 0.2.2 → 0.4.2

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: 2788a5ff60e8e0bc920bdc79f77a7b72173ea9e931796eca9c6133f052e94128
4
- data.tar.gz: c407ba38d42abb056bef8adb8e237e46f6b536f93153646bf65f45aa4665c38d
3
+ metadata.gz: e8009597e2e41965f0ad078918d629c4b88eaf2536d9c001053079cdfd24705a
4
+ data.tar.gz: 44386071847fd8a44299d967a8d8605b615a3fc865afd21a4f555410c10b3cba
5
5
  SHA512:
6
- metadata.gz: d8279ffce672f404395f175dc98a41caab4658e44b7eb6feb80b208d217cf923f35eed7fcde3910620465ee1cb0b1d0c4e8e0d44f4cee8339551ea5932d8e762
7
- data.tar.gz: 48fcd44ddaa1db8dbda9da2817241627ab44264c946e9c9dfac68ab409f6b31d708bfea4ab39d50031531cca8548ba9a8836da7c7bbd85ffa69e6998ade94767
6
+ metadata.gz: 505e79eee033484f4ecd2eb08daca91521e2d93f5b780051fe82968d9a5510b0ac5152d4cc61aee77827e85d1e40d053451881e623e9f0f713fc1e0f0e03b1fa
7
+ data.tar.gz: 842210c79e38f9c984018c38377f704d235bd4f795ce3753cd89fdcddceb9c2d93db654c94d68dec23a4de818ade9719a08d382f5b6abb4fed5d29b5259cf8a1
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  /tmp/
13
13
  *.gem
14
14
  *.rbc
15
+ /examples/**/Gemfile.lock
data/.rubocop.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.5
3
+ SuggestExtensions: false
3
4
 
4
5
  Style/StringLiterals:
5
6
  Enabled: true
@@ -9,5 +10,17 @@ Style/StringLiteralsInInterpolation:
9
10
  Enabled: true
10
11
  EnforcedStyle: double_quotes
11
12
 
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Metrics/MethodLength:
17
+ Enabled: false
18
+
19
+ Metrics/AbcSize:
20
+ Max: 25
21
+
12
22
  Layout/LineLength:
13
23
  Max: 120
24
+
25
+ Style/SpecialGlobalVars:
26
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,4 +1,30 @@
1
- ## [Unreleased]
1
+ ## [0.4.2] - 2021-07-24
2
+
3
+ - DB module removed
4
+
5
+ ## [0.4.1] - 2021-07-24
6
+
7
+ - A little fix. Fixed an error with the wrong order of declaring variables (lol)
8
+
9
+ ## [0.4.0] - 2021-07-24
10
+
11
+ - Global routing update and large-scale code base changes
12
+
13
+ ## [0.3.0] - 2021-07-24
14
+
15
+ - Added tools for interacting with the database
16
+
17
+ ## [0.2.2] - 2021-07-23
18
+
19
+ - Params fixed
20
+
21
+ ## [0.2.1] - 2021-07-23
22
+
23
+ - Response/env/params variables added
24
+
25
+ ## [0.2.0] - 2021-07-23
26
+
27
+ - Main functionality implemented
2
28
 
3
29
  ## [0.1.0] - 2021-07-23
4
30
 
data/Gemfile CHANGED
@@ -5,4 +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
9
  gem "rack", "~> 2.2", ">= 2.2.3"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yaframework (0.2.1)
4
+ yaframework (0.4.1)
5
5
  rack (~> 2.2)
6
6
 
7
7
  GEM
@@ -12,6 +12,7 @@ GEM
12
12
  parallel (1.20.1)
13
13
  parser (3.0.2.0)
14
14
  ast (~> 2.4.1)
15
+ pg (1.2.3)
15
16
  rack (2.2.3)
16
17
  rainbow (3.0.0)
17
18
  rake (13.0.6)
@@ -37,6 +38,7 @@ PLATFORMS
37
38
  DEPENDENCIES
38
39
  bundler (~> 2.1.4)
39
40
  minitest (~> 5.0)
41
+ pg (~> 1.2, >= 1.2.3)
40
42
  rack (~> 2.2, >= 2.2.3)
41
43
  rake (~> 13.0)
42
44
  rubocop (~> 1.7)
data/README.md CHANGED
@@ -1,8 +1,41 @@
1
1
  # Yaframework
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/yaframework`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/yaframework.svg)](https://badge.fury.io/rb/sinatra)
4
+ [![Build Status](https://app.travis-ci.com/maxbarsukov/yaframework.svg?token=T4CL2EqKG6FY816F3W3F&branch=master)](https://app.travis-ci.com/maxbarsukov/yaframework)
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ Yaframework is a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for
7
+ quickly creating web applications in Ruby with minimal effort:
8
+
9
+ > _Wait, I've seen this somewhere..._
10
+
11
+ ```ruby
12
+ # app.rb
13
+ require 'yaframework'
14
+ app = Yaframework::Application
15
+
16
+ app.get "/" do
17
+ "Hello world!"
18
+ end
19
+
20
+ app.listen(4567)
21
+ ```
22
+
23
+ Install the gem:
24
+
25
+ ```shell
26
+ gem install yaframework
27
+ ```
28
+
29
+ And run with:
30
+
31
+ ```shell
32
+ ruby app.rb
33
+ ```
34
+
35
+ View at [http://localhost:4567](http://localhost:4567)
36
+
37
+ The code you changed will not take effect until you restart the server.
38
+ Please restart the server every time you change.
6
39
 
7
40
  ## Installation
8
41
 
@@ -22,7 +55,7 @@ Or install it yourself as:
22
55
 
23
56
  ## Usage
24
57
 
25
- TODO: Write usage instructions here
58
+ You can see usage examples in the [**examples**](https://github.com/maxbarsukov/yaframework/tree/master/examples) folder
26
59
 
27
60
  ## Development
28
61
 
data/examples/README.md CHANGED
@@ -2,9 +2,16 @@
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)
6
6
 
7
7
  A simple application that displays "Hello world" to you.
8
8
 
9
9
 
10
- 2. Be soon...
10
+ 2. ###[Params](https://github.com/maxbarsukov/yaframework/tree/master/examples/params)
11
+
12
+ Shows how the application parameters can be used
13
+
14
+
15
+ 3. ###[Halts and Redirects](https://github.com/maxbarsukov/yaframework/tree/master/examples/halts-and-redirects)
16
+
17
+ Shows how to use halts and redirects
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "yaframework"
@@ -0,0 +1,12 @@
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
@@ -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,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ require "./app"
5
+
6
+ run app
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "yaframework"
@@ -0,0 +1,12 @@
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
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.get "/:name" do
7
+ "Hello #{request.params[:name]}!"
8
+ end
9
+
10
+ app.get "/:name/foo/:bar" do
11
+ "Hello #{request.params[:name]} from #{request.params[:bar]}!"
12
+ end
13
+
14
+ app.listen(4567)
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaframework"
4
+ require "./app"
5
+
6
+ run app
data/lib/yaframework.rb CHANGED
@@ -1,6 +1,8 @@
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
 
5
7
  module Yaframework
6
8
  end
@@ -7,35 +7,24 @@ module Yaframework
7
7
  attr_reader :routes, :request, :response, :env
8
8
 
9
9
  def initialize
10
- @routes = {}
10
+ @routes = Hash.new([])
11
11
  end
12
12
 
13
13
  %w[GET POST PATCH PUT DELETE HEAD OPTIONS].each do |verb|
14
14
  define_method :"#{verb.downcase}" do |path, &handler|
15
- route(verb, path, &handler)
15
+ add_route(verb, path, &handler)
16
16
  end
17
17
  end
18
18
 
19
19
  def call(env)
20
- @request = Rack::Request.new(env)
21
- @response = Rack::Response.new
22
- @params = request.params
23
20
  @env = env
24
-
25
- verb = @request.request_method
26
- path = @request.path_info
27
-
28
- handler = @routes.fetch(verb, {}).fetch(path, nil)
29
-
30
- if handler
31
- result = instance_eval(&handler)
32
- return result.instance_of?(String) ? [200, {}, [result]] : result
33
- end
34
- [404, {}, ["Route for #{verb} #{path} not found"]]
21
+ @request = Yaframework::Request.new @env
22
+ @response = Yaframework::Response.new
23
+ catch(:halt) { route_eval }
35
24
  end
36
25
 
37
- def params
38
- @request.params
26
+ def halt(response)
27
+ throw :halt, response
39
28
  end
40
29
 
41
30
  def listen(port = 5000)
@@ -44,11 +33,44 @@ module Yaframework
44
33
 
45
34
  private
46
35
 
47
- def route(verb, path, &handler)
36
+ def add_route(verb, path, &handler)
48
37
  path = "/#{path}" unless path[0] == "/"
38
+ @routes[verb] << compile(path, &handler)
39
+ end
40
+
41
+ def compile(path, &handler)
42
+ route = { handler: handler, compiled_path: nil, extra_params: [], path: path }
49
43
 
50
- @routes[verb] ||= {}
51
- @routes[verb][path] = handler
44
+ compiled_path = path.gsub(/:\w+/) do |match|
45
+ route[:extra_params] << match.gsub(":", "").to_sym
46
+ "([^/?#]+)"
47
+ end
48
+ route[:compiled_path] = /^#{compiled_path}$/
49
+ route
50
+ end
51
+
52
+ def route_eval
53
+ route = find_route
54
+ if route
55
+ response.write instance_eval(&route[:handler])
56
+ else
57
+ response.status = 404
58
+ end
59
+ response.finish
60
+ end
61
+
62
+ def find_route
63
+ route = routes[request.request_method].detect do |r|
64
+ r[:compiled_path] =~ request.path_info
65
+ end
66
+
67
+ if route
68
+ $~.captures.each_with_index do |value, index|
69
+ param = route[:extra_params][index]
70
+ request.params[param] = value
71
+ end
72
+ end
73
+ route
52
74
  end
53
75
  end
54
76
 
@@ -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.2.2"
4
+ VERSION = "0.4.2"
5
5
  end
data/yaframework.gemspec CHANGED
@@ -27,9 +27,8 @@ Gem::Specification.new do |spec|
27
27
  end
28
28
  spec.bindir = "exe"
29
29
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
31
30
 
32
- spec.add_development_dependency "bundler", "~> 2.1.4"
31
+ spec.add_development_dependency "bundler", ">= 2.2.10"
33
32
  spec.add_development_dependency "rake", "~> 13.0"
34
33
  spec.add_development_dependency "rubocop", "~> 1.7"
35
34
 
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaframework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.2
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-23 00:00:00.000000000 Z
11
+ date: 2021-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.1.4
19
+ version: 2.2.10
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 2.1.4
26
+ version: 2.2.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -86,13 +86,22 @@ files:
86
86
  - bin/console
87
87
  - bin/setup
88
88
  - 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
89
93
  - examples/hello-world/Gemfile
90
- - examples/hello-world/Gemfile.lock
91
94
  - examples/hello-world/README.md
92
95
  - examples/hello-world/app.rb
93
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
94
101
  - lib/yaframework.rb
95
102
  - lib/yaframework/base.rb
103
+ - lib/yaframework/request.rb
104
+ - lib/yaframework/response.rb
96
105
  - lib/yaframework/version.rb
97
106
  - test/test_helper.rb
98
107
  - test/yaframework_test.rb
@@ -1,15 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- rack (2.2.3)
5
- yaframework (0.2.0)
6
- rack (~> 2.2)
7
-
8
- PLATFORMS
9
- ruby
10
-
11
- DEPENDENCIES
12
- yaframework
13
-
14
- BUNDLED WITH
15
- 2.1.4