yaframework 0.2.0 → 0.4.0

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: e7a479f2bf2f12810b9b16491e1e23977b71231de949e482f85ab8347a46a75e
4
- data.tar.gz: 95547e19dc23c7afee15973f2423fe51145b24a04fba435b6efdbe51d0ce3531
3
+ metadata.gz: 33b8bd38ee7fbf3d16bdef94f830384f979ad465ff674d085378caa6a46b6971
4
+ data.tar.gz: 1da887439ec28a18fc4ee8a4812d9bf3d9f70be894636f181a6af98a965d0dd2
5
5
  SHA512:
6
- metadata.gz: 484558b99bd73c24686ed9b46b5ed90209e4cee5dd85337068768f7634b60f8f05174f35f2ccc5b5ebfb832159007461d1209f7156dd52ee1718f8652187493e
7
- data.tar.gz: e88e02703af13cf829d97e47bcde0023a69865a778f4a071622499ff3227f60f83047b6de2da513591ff466510f8ae704d32f890eac2375311eb61d19cfdfc09
6
+ metadata.gz: 6ff977d266bccebbc67aa17c7ff2116be8f51c057be070807afa4d2c6e6a090c1053f146434e3a9ed78defa9ee840dfaa08b760f4807b0976b7bef9a09b181db
7
+ data.tar.gz: a62b06535d579d4b552aec2c1a0856514d5b27628aec77e267cfa4d98bc9b4b6185d00d6c919cae5fdf198a295870a7d0a8233eb4b3df08e39c967663791887f
data/.gitignore CHANGED
@@ -12,4 +12,3 @@
12
12
  /tmp/
13
13
  *.gem
14
14
  *.rbc
15
- 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,11 @@ 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
+
12
19
  Layout/LineLength:
13
20
  Max: 120
data/CHANGELOG.md CHANGED
@@ -1,4 +1,14 @@
1
- ## [Unreleased]
1
+ ## [0.2.2] - 2021-07-23
2
+
3
+ - Params fixed
4
+
5
+ ## [0.2.1] - 2021-07-23
6
+
7
+ - Response/env/params variables added
8
+
9
+ ## [0.2.0] - 2021-07-23
10
+
11
+ - Main functionality implemented
2
12
 
3
13
  ## [0.1.0] - 2021-07-23
4
14
 
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.2.2)
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,10 @@
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. Be soon...
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "yaframework"
@@ -0,0 +1,15 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ rack (2.2.3)
5
+ yaframework (0.2.2)
6
+ rack (~> 2.2)
7
+
8
+ PLATFORMS
9
+ ruby
10
+
11
+ DEPENDENCIES
12
+ yaframework
13
+
14
+ BUNDLED WITH
15
+ 2.1.4
@@ -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,14 @@
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.get "/hi/:name" do
11
+ "Hello #{request.params[:name]}"
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,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,35 +4,27 @@ require "rack"
4
4
 
5
5
  module Yaframework
6
6
  class Base
7
- attr_reader :routes,
8
- :request
7
+ attr_reader :routes, :request, :response, :env
9
8
 
10
9
  def initialize
11
- @routes = {}
10
+ @routes = Hash.new([])
12
11
  end
13
12
 
14
13
  %w[GET POST PATCH PUT DELETE HEAD OPTIONS].each do |verb|
15
14
  define_method :"#{verb.downcase}" do |path, &handler|
16
- route(verb, path, &handler)
15
+ add_route(verb, path, &handler)
17
16
  end
18
17
  end
19
18
 
20
19
  def call(env)
21
- @request = Rack::Request.new(env)
22
- verb = @request.request_method
23
- path = @request.path_info
24
-
25
- handler = @routes.fetch(verb, {}).fetch(path, nil)
26
-
27
- if handler
28
- result = instance_eval(&handler)
29
- return result.instance_of?(String) ? [200, {}, [result]] : result
30
- end
31
- [404, {}, ["Route for #{verb} #{path} not found"]]
20
+ @request = Yaframework::Request.new @env
21
+ @response = Yaframework::Response.new
22
+ @env = env
23
+ catch(:halt) { route_eval }
32
24
  end
33
25
 
34
- def params
35
- @request.params
26
+ def halt(response)
27
+ throw :halt, response
36
28
  end
37
29
 
38
30
  def listen(port = 5000)
@@ -41,11 +33,44 @@ module Yaframework
41
33
 
42
34
  private
43
35
 
44
- def route(verb, path, &handler)
36
+ def add_route(verb, path, &handler)
45
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 }
43
+
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
46
61
 
47
- @routes[verb] ||= {}
48
- @routes[verb][path] = handler
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
49
74
  end
50
75
  end
51
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.0"
4
+ VERSION = "0.4.0"
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.0
4
+ version: 0.4.0
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,23 @@ 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/Gemfile.lock
91
+ - examples/hello-world/README.md
92
+ - examples/hello-world/app.rb
93
+ - examples/hello-world/config.ru
87
94
  - lib/yaframework.rb
88
95
  - lib/yaframework/base.rb
96
+ - lib/yaframework/database.rb
97
+ - lib/yaframework/request.rb
98
+ - lib/yaframework/response.rb
89
99
  - lib/yaframework/version.rb
90
100
  - test/test_helper.rb
91
101
  - test/yaframework_test.rb
@@ -105,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
115
  requirements:
106
116
  - - ">="
107
117
  - !ruby/object:Gem::Version
108
- version: 2.4.0
118
+ version: 2.5.0
109
119
  required_rubygems_version: !ruby/object:Gem::Requirement
110
120
  requirements:
111
121
  - - ">="