jetski 0.2.8 → 0.3.1
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 +4 -4
- data/bin/jetski +9 -0
- data/lib/jetski/base_controller.rb +21 -0
- data/lib/jetski/router/parser.rb +93 -0
- data/lib/jetski/router.rb +19 -18
- data/lib/jetski/version.rb +1 -1
- data/lib/jetski.rb +3 -1
- data/templates/base/app/assets/images/jetski-logo.png +0 -0
- data/templates/base/app/controllers/pages_controller.rb +1 -0
- data/templates/base/app/views/layouts/application.html +1 -0
- metadata +3 -2
- data/templates/base/config/routes +0 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9695762a6c63b04e17d8477feff33bd1cf6e44a819a7112c17abed5c9411826b
|
|
4
|
+
data.tar.gz: 046a721b230e7fe80c5fe19a95a1a81d64d3e1956caff0163127a4752064a58a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3186f1f38ab5fd20ddbd24f6c3e9958f34e44161269333c83b1ae42208e64d4016ed03354458c4a7540c4e38784120a9ff8e971279465b2d66156d9909c0a7c3
|
|
7
|
+
data.tar.gz: 1b6674aaffba2a6054bd0d07e9923d5c0bf43df333572e4b81f9474c2d671d01c3ad63b987d0625a794012c227003b831c97ce3b956e7e3b655985f35677400c
|
data/bin/jetski
CHANGED
|
@@ -23,6 +23,15 @@ class JetskiCLI < Thor
|
|
|
23
23
|
say "Jetski v#{Jetski::VERSION}"
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
+
desc "routes", "shows the routes in your app."
|
|
27
|
+
def routes
|
|
28
|
+
compiled_routes = Jetski::Router::Parser.compile_routes
|
|
29
|
+
say "🌊 Jetski Routes 🏄♂️"
|
|
30
|
+
compiled_routes.each do |route_obj|
|
|
31
|
+
say "#{route_obj[:method]} #{route_obj[:url]} #{route_obj[:controller_name]}##{route_obj[:action_name]}"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
26
35
|
def self.source_root
|
|
27
36
|
File.join(File.dirname(__FILE__), '..', 'templates')
|
|
28
37
|
end
|
|
@@ -3,13 +3,33 @@ module Jetski
|
|
|
3
3
|
class BaseController
|
|
4
4
|
attr_accessor :action_name, :controller_name
|
|
5
5
|
attr_reader :res
|
|
6
|
+
attr_reader :performed_render
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
def request_method(method)
|
|
10
|
+
# Really just a shell method since Im using File.readlines to use the logic in routes.
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def root
|
|
14
|
+
# another shell method
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def path(location)
|
|
18
|
+
# Another shell method
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
6
22
|
def initialize(res)
|
|
7
23
|
@res = res
|
|
24
|
+
@performed_render = false
|
|
8
25
|
end
|
|
9
26
|
|
|
10
27
|
# Method to render matching view with controller_name/action_name
|
|
11
28
|
|
|
12
29
|
def render(**args)
|
|
30
|
+
@performed_render = true
|
|
31
|
+
request_status = args[:status] || 200
|
|
32
|
+
res.status = request_status
|
|
13
33
|
if args[:text]
|
|
14
34
|
res.content_type = "text/plain"
|
|
15
35
|
res.body = "#{args[:text]}\n"
|
|
@@ -21,6 +41,7 @@ module Jetski
|
|
|
21
41
|
res.body = args[:json].to_json
|
|
22
42
|
return
|
|
23
43
|
end
|
|
44
|
+
|
|
24
45
|
render_template_file
|
|
25
46
|
end
|
|
26
47
|
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
module Jetski
|
|
2
|
+
class Router
|
|
3
|
+
module Parser
|
|
4
|
+
extend self
|
|
5
|
+
def compile_routes
|
|
6
|
+
auto_found_routes = []
|
|
7
|
+
controller_file_paths = Dir.glob([File.join(Jetski.app_root, 'app', 'controllers', '**', '*_controller.rb')])
|
|
8
|
+
controller_file_paths.each do |file_path|
|
|
9
|
+
controller_file_name = file_path.split('app/controllers')[1]
|
|
10
|
+
controller_as_url = controller_file_name.gsub(/_controller.rb/, '')
|
|
11
|
+
controller_name = controller_as_url.split("/").last
|
|
12
|
+
controller_classname = controller_as_url.split("/").reject(&:empty?).map(&:capitalize).join("::") + "Controller"
|
|
13
|
+
controller_file_readlines = File.readlines(file_path)
|
|
14
|
+
controller_file_readlines.each.with_index do |line, idx|
|
|
15
|
+
strp_line = line.strip
|
|
16
|
+
if strp_line.start_with?('def')
|
|
17
|
+
action_name = strp_line.split("def").last.strip
|
|
18
|
+
base_opts = {
|
|
19
|
+
controller_classname: controller_classname,
|
|
20
|
+
controller_file_name: controller_file_name,
|
|
21
|
+
controller_name: controller_name
|
|
22
|
+
}
|
|
23
|
+
case action_name
|
|
24
|
+
when "new"
|
|
25
|
+
auto_found_routes << base_opts.merge({
|
|
26
|
+
url: controller_as_url + "/new",
|
|
27
|
+
method: "GET",
|
|
28
|
+
action_name: action_name,
|
|
29
|
+
})
|
|
30
|
+
when "create"
|
|
31
|
+
auto_found_routes << base_opts.merge({
|
|
32
|
+
url: controller_as_url,
|
|
33
|
+
method: "POST",
|
|
34
|
+
action_name: action_name,
|
|
35
|
+
})
|
|
36
|
+
when "show"
|
|
37
|
+
auto_found_routes << base_opts.merge({
|
|
38
|
+
url: controller_as_url + "/:id",
|
|
39
|
+
method: "GET",
|
|
40
|
+
action_name: action_name,
|
|
41
|
+
})
|
|
42
|
+
when "edit"
|
|
43
|
+
auto_found_routes << base_opts.merge({
|
|
44
|
+
url: controller_as_url + "/:id/edit",
|
|
45
|
+
method: "GET",
|
|
46
|
+
action_name: action_name,
|
|
47
|
+
})
|
|
48
|
+
when "update"
|
|
49
|
+
auto_found_routes << base_opts.merge({
|
|
50
|
+
url: controller_as_url + "/:id",
|
|
51
|
+
method: "PUT",
|
|
52
|
+
action_name: action_name,
|
|
53
|
+
})
|
|
54
|
+
when "destroy"
|
|
55
|
+
auto_found_routes << base_opts.merge({
|
|
56
|
+
url: controller_as_url + "/:id",
|
|
57
|
+
method: "DELETE",
|
|
58
|
+
action_name: action_name,
|
|
59
|
+
})
|
|
60
|
+
else
|
|
61
|
+
method_route_options = controller_file_readlines[(idx - 2)..(idx - 1)].map(&:strip)
|
|
62
|
+
custom_request_method = method_route_options.find { |line| line.start_with? "request_method" }
|
|
63
|
+
custom_request_method = if custom_request_method
|
|
64
|
+
custom_request_method.split(" ")[1].gsub('"', '').upcase
|
|
65
|
+
else
|
|
66
|
+
"GET"
|
|
67
|
+
end
|
|
68
|
+
custom_path_option = method_route_options.find { |line| line.start_with? "path" }
|
|
69
|
+
check_root = controller_file_readlines[idx - 1].strip
|
|
70
|
+
url_to_use = if check_root.include?("root")
|
|
71
|
+
"/"
|
|
72
|
+
else
|
|
73
|
+
if custom_path_option
|
|
74
|
+
custom_path_option.split(" ")[1].gsub('"', '')
|
|
75
|
+
else
|
|
76
|
+
url_friendly_action_name = action_name.split("_").join("-")
|
|
77
|
+
controller_as_url + "/#{url_friendly_action_name}"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
auto_found_routes << base_opts.merge({
|
|
81
|
+
url: url_to_use,
|
|
82
|
+
method: custom_request_method,
|
|
83
|
+
action_name: action_name,
|
|
84
|
+
})
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
auto_found_routes
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
data/lib/jetski/router.rb
CHANGED
|
@@ -1,47 +1,48 @@
|
|
|
1
1
|
module Jetski
|
|
2
2
|
class Router
|
|
3
|
+
include Parser
|
|
3
4
|
attr_reader :server
|
|
4
5
|
def initialize(server)
|
|
5
6
|
@server = server
|
|
6
7
|
end
|
|
7
8
|
|
|
8
9
|
def call
|
|
9
|
-
|
|
10
|
+
host_routes && host_assets
|
|
10
11
|
end
|
|
11
12
|
|
|
12
|
-
def
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
def host_routes
|
|
14
|
+
routes = compile_routes
|
|
15
|
+
routes.each do |af_route|
|
|
16
|
+
served_url = af_route[:url]
|
|
17
|
+
request_method = af_route[:method]
|
|
18
|
+
controller_classname = af_route[:controller_classname]
|
|
19
|
+
controller_name = af_route[:controller_name]
|
|
20
|
+
action_name = af_route[:action_name]
|
|
21
|
+
controller_file_name = af_route[:controller_file_name]
|
|
15
22
|
|
|
16
|
-
File.readlines(routes_file, chomp: true).each do |line|
|
|
17
|
-
route_action, served_url, controller_name, action_name = line.split(" ")
|
|
18
23
|
server.mount_proc served_url do |req, res|
|
|
19
24
|
errors = []
|
|
20
|
-
if (
|
|
25
|
+
if (request_method!= req.request_method)
|
|
21
26
|
errors << "Wrong request was performed"
|
|
22
27
|
end
|
|
23
|
-
# TODO: Fix the fact that we are always setting res.body to something here.
|
|
24
|
-
# Theres no way to return. We need to organize into case statement or if/else type
|
|
25
|
-
|
|
26
28
|
if errors.empty?
|
|
27
|
-
|
|
28
|
-
constantized_controller = "#{controller_name_formatted}Controller"
|
|
29
|
-
path_to_defined_controller = File.join(Jetski.app_root, "app/controllers/#{controller_name}_controller.rb")
|
|
29
|
+
path_to_defined_controller = File.join(Jetski.app_root, "app/controllers/#{controller_file_name}")
|
|
30
30
|
require_relative path_to_defined_controller
|
|
31
31
|
begin
|
|
32
|
-
controller_class = Object.const_get(
|
|
32
|
+
controller_class = Object.const_get(controller_classname)
|
|
33
33
|
rescue NameError
|
|
34
|
-
errors << "#{
|
|
34
|
+
errors << "#{controller_classname} is not defined. Please create a file app/controllers/#{controller_file_name}"
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
if errors.empty?
|
|
38
|
+
if errors.empty?
|
|
39
39
|
controller = controller_class.new(res)
|
|
40
40
|
controller.action_name = action_name
|
|
41
41
|
controller.controller_name = controller_name
|
|
42
42
|
controller.send(action_name)
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
if !controller.performed_render && (request_method.upcase == "GET")
|
|
44
|
+
controller.render
|
|
45
|
+
end
|
|
45
46
|
# TODO: Need to setup redirects for other request types. POST/PUT/DELETE
|
|
46
47
|
end
|
|
47
48
|
|
data/lib/jetski/version.rb
CHANGED
data/lib/jetski.rb
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
require_relative './jetski/version'
|
|
2
|
+
require_relative './jetski/base_controller'
|
|
2
3
|
require_relative './jetski/server'
|
|
4
|
+
require_relative './jetski/router/parser'
|
|
3
5
|
require_relative './jetski/router'
|
|
4
|
-
require_relative './jetski/base_controller'
|
|
5
6
|
require "webrick"
|
|
6
7
|
require "json"
|
|
8
|
+
require "json"
|
|
7
9
|
|
|
8
10
|
module Jetski
|
|
9
11
|
# Debug stage add constants here for debugging.
|
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jetski
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Indigo Tech Tutorials
|
|
@@ -49,14 +49,15 @@ files:
|
|
|
49
49
|
- lib/jetski.rb
|
|
50
50
|
- lib/jetski/base_controller.rb
|
|
51
51
|
- lib/jetski/router.rb
|
|
52
|
+
- lib/jetski/router/parser.rb
|
|
52
53
|
- lib/jetski/server.rb
|
|
53
54
|
- lib/jetski/version.rb
|
|
54
55
|
- templates/base/Gemfile
|
|
56
|
+
- templates/base/app/assets/images/jetski-logo.png
|
|
55
57
|
- templates/base/app/assets/stylesheets/application.css
|
|
56
58
|
- templates/base/app/controllers/pages_controller.rb
|
|
57
59
|
- templates/base/app/views/layouts/application.html
|
|
58
60
|
- templates/base/app/views/pages/home.html
|
|
59
|
-
- templates/base/config/routes
|
|
60
61
|
- templates/base/start.rb
|
|
61
62
|
homepage: https://rubygems.org/gems/jetski
|
|
62
63
|
licenses:
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
get / pages home
|