yaframework 0.2.2 → 0.3.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: 2788a5ff60e8e0bc920bdc79f77a7b72173ea9e931796eca9c6133f052e94128
4
- data.tar.gz: c407ba38d42abb056bef8adb8e237e46f6b536f93153646bf65f45aa4665c38d
3
+ metadata.gz: f292d6a4e199c7cbd9d9375649f311bfc3889c86212d8a30316a37d65685b90d
4
+ data.tar.gz: a3715a27f85ef444a996edd039323dc15e6621f518c33c278d06fceeb6cf75c1
5
5
  SHA512:
6
- metadata.gz: d8279ffce672f404395f175dc98a41caab4658e44b7eb6feb80b208d217cf923f35eed7fcde3910620465ee1cb0b1d0c4e8e0d44f4cee8339551ea5932d8e762
7
- data.tar.gz: 48fcd44ddaa1db8dbda9da2817241627ab44264c946e9c9dfac68ab409f6b31d708bfea4ab39d50031531cca8548ba9a8836da7c7bbd85ffa69e6998ade94767
6
+ metadata.gz: 2f152b5b3f1198fa48e2eb434ce38a9254970c3f4a82571d6ea9359ee531c2c39c663a65fa674c512c897d3bed269d6aa3d7858937047c22f911d5c5cfd8af73
7
+ data.tar.gz: ad9ddf07e7ce153c3b55cbb48389628dcbc055f3dd23101eede3e28040961a47cd7c83a26fe46de824c96995cf35502ab2cb49856e50fba0aa2a04f48a5d9318
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,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.2.2)
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
 
@@ -2,4 +2,4 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "yaframework"
5
+ gem "yaframework", path: "/home/max/prog/ruby/yaframework"
@@ -1,15 +1,19 @@
1
+ PATH
2
+ remote: /home/max/prog/ruby/yaframework
3
+ specs:
4
+ yaframework (0.3.0)
5
+ rack (~> 2.2)
6
+
1
7
  GEM
2
8
  remote: https://rubygems.org/
3
9
  specs:
4
10
  rack (2.2.3)
5
- yaframework (0.2.0)
6
- rack (~> 2.2)
7
11
 
8
12
  PLATFORMS
9
13
  ruby
10
14
 
11
15
  DEPENDENCIES
12
- yaframework
16
+ yaframework!
13
17
 
14
18
  BUNDLED WITH
15
19
  2.1.4
@@ -7,4 +7,8 @@ app.get "/" do
7
7
  "Hello world!"
8
8
  end
9
9
 
10
+ app.get "/hi/:name" do
11
+ "Hello #{request.params[:name]}"
12
+ end
13
+
10
14
  app.listen(4567)
data/lib/yaframework.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "yaframework/base"
4
+ require_relative "yaframework/database"
5
+ require_relative 'yaframework/request'
6
+ require_relative 'yaframework/response'
4
7
 
5
8
  module Yaframework
6
9
  end
@@ -4,38 +4,28 @@ require "rack"
4
4
 
5
5
  module Yaframework
6
6
  class Base
7
- attr_reader :routes, :request, :response, :env
7
+ attr_reader :routes, :request, :response, :env, :hello
8
8
 
9
9
  def initialize
10
- @routes = {}
10
+ @routes = Hash.new { |h, k| h[k] = [] }
11
+ # @routes = Hash.new([])
11
12
  end
12
13
 
13
14
  %w[GET POST PATCH PUT DELETE HEAD OPTIONS].each do |verb|
14
15
  define_method :"#{verb.downcase}" do |path, &handler|
15
- route(verb, path, &handler)
16
+ add_route(verb, path, &handler)
16
17
  end
17
18
  end
18
19
 
19
20
  def call(env)
20
- @request = Rack::Request.new(env)
21
- @response = Rack::Response.new
22
- @params = request.params
23
21
  @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"]]
22
+ @request = Yaframework::Request.new @env
23
+ @response = Yaframework::Response.new
24
+ catch(:halt) { route_eval }
35
25
  end
36
26
 
37
- def params
38
- @request.params
27
+ def halt(response)
28
+ throw :halt, response
39
29
  end
40
30
 
41
31
  def listen(port = 5000)
@@ -44,11 +34,47 @@ module Yaframework
44
34
 
45
35
  private
46
36
 
47
- def route(verb, path, &handler)
37
+ def add_route(verb, path, &handler)
48
38
  path = "/#{path}" unless path[0] == "/"
49
39
 
50
- @routes[verb] ||= {}
51
- @routes[verb][path] = handler
40
+ @routes[verb] ||= []
41
+ @routes[verb] << compile_route(path, &handler)
42
+ end
43
+
44
+ def compile_route(path, &handler)
45
+ route = { handler: handler, compiled_path: nil, extra_params: [], path: path }
46
+
47
+ compiled_path = path.gsub(/:\w+/) do |match|
48
+ route[:extra_params] << match.gsub(":", "").to_sym
49
+ "([^/?#]+)"
50
+ end
51
+ route[:compiled_path] = /^#{compiled_path}$/
52
+ route
53
+ end
54
+
55
+ def route_eval
56
+ route = find_route
57
+ if route
58
+ response.write instance_eval(&route[:handler])
59
+ else
60
+ response.status = 404
61
+ end
62
+
63
+ response.finish
64
+ end
65
+
66
+ def find_route
67
+ route = @routes[request.request_method].detect do |r|
68
+ r[:compiled_path] =~ request.path_info
69
+ end
70
+
71
+ if route
72
+ $LAST_MATCH_INFO.captures.each_with_index do |value, index|
73
+ param = route[:extra_params][index]
74
+ request.params[param] = value
75
+ end
76
+ end
77
+ route
52
78
  end
53
79
  end
54
80
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yaframework
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.0"
5
5
  end
data/yaframework.gemspec CHANGED
@@ -27,7 +27,6 @@ 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
31
  spec.add_development_dependency "bundler", "~> 2.1.4"
33
32
  spec.add_development_dependency "rake", "~> 13.0"
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.2
4
+ version: 0.3.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