yaframework 0.2.1 → 0.4.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: 1023ffdf696fe0cca466c59e707504d1087fd4433d1cfaf44d17a0e7056d4196
4
- data.tar.gz: 93dd4318d8a209a8dd40fc5a1b2d9dc8e3d233a1fa92f4e3883f0ca74067e71e
3
+ metadata.gz: 23a8af14273443627664b65feb652bc03336d5fbe829f672cae4ef9c8ca4604e
4
+ data.tar.gz: 848d0e46585695fe4756bf91b6c286b9cacb4ec684a535491e7a348f4a2c081a
5
5
  SHA512:
6
- metadata.gz: d3eab3431ab7170cfe6367a258b33b32404abcb97ac8f8853bf9a4959cd6528b92edfbe014ee9a8e14afe6b7a445fe76dd198787ff7b927162dc6217a4192886
7
- data.tar.gz: 0b5701763d06e37cb4f71ec494e38280998110ca698dd00ce9c1ea31373204a561028f9ee14dcdb6e7f604c28dbe76b17447c3306f025eeb2c56ee1e8d61e29c
6
+ metadata.gz: 35fa5d015af06f258f7b7fff290a14a3f60329aeb2e713b4cad172f86b55bde28a01d371a114ed89b77a48862146d78c53721fa21bcd86beaa155c0cec7a13f8
7
+ data.tar.gz: 47a8a7be5c253fb3a1bf788c6256613f85c9132725a328e456b8e7e27f82da322f5f2ff5691e8267dd3fce9139d0db13fcab6ca71e424436bdf1a1b165a88b3b
data/.gitignore CHANGED
@@ -12,4 +12,4 @@
12
12
  /tmp/
13
13
  *.gem
14
14
  *.rbc
15
- Gemfile.lock
15
+ /examples/**/Gemfile.lock
data/.rubocop.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.4
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,26 @@
1
- ## [Unreleased]
1
+ ## [0.4.1] - 2021-07-24
2
+
3
+ - A little fix. Fixed an error with the wrong order of declaring variables (lol)
4
+
5
+ ## [0.4.0] - 2021-07-24
6
+
7
+ - Global routing update and large-scale code base changes
8
+
9
+ ## [0.3.0] - 2021-07-24
10
+
11
+ - Added tools for interacting with the database
12
+
13
+ ## [0.2.2] - 2021-07-23
14
+
15
+ - Params fixed
16
+
17
+ ## [0.2.1] - 2021-07-23
18
+
19
+ - Response/env/params variables added
20
+
21
+ ## [0.2.0] - 2021-07-23
22
+
23
+ - Main functionality implemented
2
24
 
3
25
  ## [0.1.0] - 2021-07-23
4
26
 
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
9
  gem "rack", "~> 2.2", ">= 2.2.3"
9
- gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yaframework (0.4.0)
5
+ rack (~> 2.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ minitest (5.14.4)
12
+ parallel (1.20.1)
13
+ parser (3.0.2.0)
14
+ ast (~> 2.4.1)
15
+ pg (1.2.3)
16
+ rack (2.2.3)
17
+ rainbow (3.0.0)
18
+ rake (13.0.6)
19
+ regexp_parser (2.1.1)
20
+ rexml (3.2.5)
21
+ rubocop (1.18.4)
22
+ parallel (~> 1.10)
23
+ parser (>= 3.0.0.0)
24
+ rainbow (>= 2.2.2, < 4.0)
25
+ regexp_parser (>= 1.8, < 3.0)
26
+ rexml
27
+ rubocop-ast (>= 1.8.0, < 2.0)
28
+ ruby-progressbar (~> 1.7)
29
+ unicode-display_width (>= 1.4.0, < 3.0)
30
+ rubocop-ast (1.8.0)
31
+ parser (>= 3.0.1.1)
32
+ ruby-progressbar (1.11.0)
33
+ unicode-display_width (2.0.0)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 2.1.4)
40
+ minitest (~> 5.0)
41
+ pg (~> 1.2, >= 1.2.3)
42
+ rack (~> 2.2, >= 2.2.3)
43
+ rake (~> 13.0)
44
+ rubocop (~> 1.7)
45
+ yaframework!
46
+
47
+ BUNDLED WITH
48
+ 2.1.4
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
 
@@ -0,0 +1,17 @@
1
+ # Examples
2
+
3
+ Here are some examples of applications created with Yaframework:
4
+
5
+ 1. ###[Hello World](https://github.com/maxbarsukov/yaframework/tree/master/examples/hello-world)
6
+
7
+ A simple application that displays "Hello world" to you.
8
+
9
+
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
+ # 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
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../lib/yaframework"
4
+ app = Yaframework::Application
5
+
6
+ app.get "/" do
7
+ "Hello world!"
8
+ end
9
+
10
+ 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,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'yaframework/base'
3
+ require_relative "yaframework/base"
4
+ require_relative "yaframework/request"
5
+ require_relative "yaframework/response"
6
+ require_relative "yaframework/database"
4
7
 
5
8
  module Yaframework
6
9
  end
@@ -4,38 +4,27 @@ require "rack"
4
4
 
5
5
  module Yaframework
6
6
  class Base
7
- attr_reader :routes, :request, :response, :params, :env
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,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.2.1"
4
+ VERSION = "0.4.1"
5
5
  end
@@ -6,8 +6,4 @@ class YaframeworkTest < Minitest::Test
6
6
  def test_that_it_has_a_version_number
7
7
  refute_nil ::Yaframework::VERSION
8
8
  end
9
-
10
- def test_it_does_something_useful
11
- assert false
12
- end
13
9
  end
data/yaframework.gemspec CHANGED
@@ -13,25 +13,24 @@ Gem::Specification.new do |spec|
13
13
 
14
14
  spec.homepage = "https://rubygems.org/gems/yaframework"
15
15
  spec.license = "MIT"
16
- spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
17
17
 
18
18
  spec.metadata["homepage_uri"] = spec.homepage
19
19
  spec.metadata["source_code_uri"] = "https://github.com/maxbarsukov/yaframework"
20
20
  spec.metadata["changelog_uri"] = "https://github.com/maxbarsukov/yaframework/blob/master/CHANGELOG.md"
21
21
 
22
- spec.test_files = Dir['test/**/*.rb']
23
- spec.require_paths = ['lib']
22
+ spec.test_files = Dir["test/**/*.rb"]
23
+ spec.require_paths = ["lib"]
24
24
 
25
25
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
26
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
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
31
  spec.add_development_dependency "bundler", "~> 2.1.4"
33
32
  spec.add_development_dependency "rake", "~> 13.0"
34
33
  spec.add_development_dependency "rubocop", "~> 1.7"
35
34
 
36
- spec.add_runtime_dependency "rack", "~> 2.2"
35
+ spec.add_runtime_dependency "rack", "~> 2.2"
37
36
  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.2.1
4
+ version: 0.4.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-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
@@ -79,13 +79,22 @@ files:
79
79
  - CHANGELOG.md
80
80
  - CODE_OF_CONDUCT.md
81
81
  - Gemfile
82
+ - Gemfile.lock
82
83
  - LICENSE.txt
83
84
  - README.md
84
85
  - Rakefile
85
86
  - bin/console
86
87
  - bin/setup
88
+ - 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
87
93
  - lib/yaframework.rb
88
94
  - lib/yaframework/base.rb
95
+ - lib/yaframework/database.rb
96
+ - lib/yaframework/request.rb
97
+ - lib/yaframework/response.rb
89
98
  - lib/yaframework/version.rb
90
99
  - test/test_helper.rb
91
100
  - test/yaframework_test.rb
@@ -105,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
114
  requirements:
106
115
  - - ">="
107
116
  - !ruby/object:Gem::Version
108
- version: 2.4.0
117
+ version: 2.5.0
109
118
  required_rubygems_version: !ruby/object:Gem::Requirement
110
119
  requirements:
111
120
  - - ">="