rackstep 0.0.3 → 0.0.4
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/lib/controller.rb +51 -41
- data/lib/global_configuration.rb +24 -0
- data/lib/rackstep.rb +30 -52
- data/lib/response.rb +32 -0
- data/lib/route.rb +24 -0
- data/lib/router.rb +16 -26
- metadata +6 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a96502606655874e6a4ff8f9d617a49a6be5b74c
|
|
4
|
+
data.tar.gz: aaed008abdfc950a191dbaacad82d25a0d5d3228
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d022ecba03a538210e8f92c125b291caa458dbf153ca207e90dc28c97acc0c5875ab1a8766072e4c0e0d1322ecbd5c57766ce46ef990f0bdb6a8670ed4028735
|
|
7
|
+
data.tar.gz: 9663bf5ea208d782f58d2ff6d97b8d4168ecdb6f2abed35b2cf0e0e1282326f10af5335325f923701637a58febde0e6514f6fc0dd68befae00ea3cebb7b0a230
|
data/lib/controller.rb
CHANGED
|
@@ -1,69 +1,79 @@
|
|
|
1
1
|
# Abstract controller class with some helper methods. ALL your controllers
|
|
2
2
|
# MUST use this one as a superclass.
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
class RackStep::Controller
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
# The request will be injected here.
|
|
7
|
+
attr_accessor :request
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
# The Rack::Response object that will be delivered to the user.
|
|
10
|
+
attr_accessor :response
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
# The 'global' app settings will be injected here. This hash variable is
|
|
13
|
+
# initialized only once (singleton) and may contain references to things
|
|
14
|
+
# that should be initialize only once during the app start (eg: database
|
|
15
|
+
# connection).
|
|
16
|
+
attr_accessor :settings
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
@response = RackStep::Response.new
|
|
21
|
-
@response.body = ''
|
|
22
|
-
@response.content_type = 'application/json'
|
|
23
|
-
@response.status = 200
|
|
24
|
-
end
|
|
18
|
+
def initialize
|
|
19
|
+
@response = RackStep::Response.new
|
|
20
|
+
@response.body = ''
|
|
21
|
+
@response.content_type = 'application/json'
|
|
22
|
+
@response.status = 200
|
|
23
|
+
end
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
# Once the application receives a new request, the router will decide wich
|
|
26
|
+
# controller should process that request and will execute this method for
|
|
27
|
+
# the chosen controller. So this is the most important method of this class
|
|
28
|
+
# and every controller should overwrite it to implement it's business
|
|
29
|
+
# logic.
|
|
30
|
+
def process_request
|
|
31
|
+
end
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
# RackStep will always execute this method before delegating the request
|
|
34
|
+
# processing to the specified controller. The user may overwrite this method.
|
|
35
|
+
# This may be usefull if the user wants to create an abstract controllers.
|
|
36
|
+
# TODO: Is this really necessary?
|
|
37
|
+
def before
|
|
38
|
+
end
|
|
39
39
|
|
|
40
|
+
# RackStep will always execute this method after processing the request
|
|
41
|
+
# of to the specified controller. The user may overwrite this method.
|
|
42
|
+
# This can be used to check for logging or any piece of code
|
|
43
|
+
# that must be executed after every request for this controller.
|
|
44
|
+
# This may be usefull if the user wants to create an abstract controllers.
|
|
45
|
+
# TODO: Is this really necessary?
|
|
46
|
+
def after
|
|
40
47
|
end
|
|
41
48
|
|
|
49
|
+
end
|
|
42
50
|
|
|
43
|
-
# This controller will handle error the error "page not found". The user may
|
|
44
|
-
# overwrite this by creating new router to 'notfound'.
|
|
45
|
-
class ErrorController < RackStep::Controller
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
@response.status = 404
|
|
51
|
-
end
|
|
52
|
+
# This is the default controller that will handle the "page not found" (404).
|
|
53
|
+
# The user may overwrite this by creating new route to 'notfound'.
|
|
54
|
+
class RackStep::NotFoundController < RackStep::Controller
|
|
52
55
|
|
|
56
|
+
def process_request
|
|
57
|
+
@response.body = '404 - Page not found'
|
|
58
|
+
@response.content_type = 'text/plain'
|
|
59
|
+
@response.status = 404
|
|
53
60
|
end
|
|
54
61
|
|
|
55
62
|
end
|
|
56
63
|
|
|
57
64
|
# A module for controllers to add static html pages rendering.
|
|
58
65
|
# This is not the best way to serve static content. In production, consider
|
|
59
|
-
# using Nginx or Apache. Using ruby/rack to serve static content
|
|
60
|
-
# of resources and should be only used for low traffic web pages.
|
|
61
|
-
# method is provided so that in this circumstances you may use it to
|
|
62
|
-
# simpler architecture.
|
|
66
|
+
# using Nginx or Apache or Amazon S3. Using ruby/rack to serve static content
|
|
67
|
+
# is a waste of resources and should be only used for low traffic web pages.
|
|
68
|
+
# This method is provided so that in this circumstances you may use it to
|
|
69
|
+
# keep a simpler architecture.
|
|
70
|
+
# TODO: Add layout support.
|
|
63
71
|
module RackStep::Controller::HtmlRendering
|
|
72
|
+
|
|
64
73
|
def render_page(page_name, pages_directory = 'app/public/pages')
|
|
65
74
|
File.read("#{pages_directory}/#{page_name}.html")
|
|
66
75
|
end
|
|
76
|
+
|
|
67
77
|
end
|
|
68
78
|
|
|
69
79
|
# A module for controllers to add ERB template rendering. RackStep is not meant
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# A singleton class with a settings hash attribute which may be used to
|
|
2
|
+
# to store all 'global' settings (eg: database connections, etc).
|
|
3
|
+
# This settings variable will be injected into every controller
|
|
4
|
+
# by RackStep.
|
|
5
|
+
|
|
6
|
+
class RackStep::GlobalConfiguration
|
|
7
|
+
|
|
8
|
+
# One and only one instance of this class will exist. To retrieve it,
|
|
9
|
+
# we use RackStep::GlobalConfiguration.instance, but please DO NOT
|
|
10
|
+
# get it this way. The settings hash will be available to you in the
|
|
11
|
+
# controller and you should access it with the "settings" object that
|
|
12
|
+
# is inject into your controller.
|
|
13
|
+
# To see how to use it, please take a look at sample_app.rb initialize
|
|
14
|
+
# method and SimpleSettingsRetrieveService controller class.
|
|
15
|
+
include Singleton
|
|
16
|
+
|
|
17
|
+
attr_accessor :settings
|
|
18
|
+
|
|
19
|
+
# Defines a Hash where the settings shall be stored.
|
|
20
|
+
def initialize
|
|
21
|
+
@settings = Hash.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
data/lib/rackstep.rb
CHANGED
|
@@ -1,40 +1,51 @@
|
|
|
1
|
-
|
|
1
|
+
# This is where we define an abstract class with the base of
|
|
2
|
+
# a RackStep app. This class MUST be extended by the user.
|
|
3
|
+
|
|
4
|
+
require 'rack'
|
|
5
|
+
require_relative 'response'
|
|
6
|
+
require_relative 'route'
|
|
2
7
|
require_relative 'router'
|
|
8
|
+
require_relative 'global_configuration'
|
|
9
|
+
require_relative 'controller'
|
|
3
10
|
|
|
4
11
|
module RackStep
|
|
5
12
|
|
|
6
13
|
class App
|
|
7
14
|
|
|
8
|
-
#
|
|
9
|
-
attr_reader :request
|
|
15
|
+
# Will store the received request which will be injected into the user controllers.
|
|
16
|
+
attr_reader :request
|
|
10
17
|
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
18
|
+
# A hash that will be injected into the controller. This hash may contain
|
|
19
|
+
# "global" settings, like a connection to database and other things that
|
|
20
|
+
# should be initiaized only once while the app is starting.
|
|
14
21
|
attr_accessor :settings
|
|
15
22
|
|
|
23
|
+
# Router is a singleton that will store all the registred routes.
|
|
24
|
+
def router
|
|
25
|
+
Router.instance
|
|
26
|
+
end
|
|
27
|
+
|
|
16
28
|
# Static method called from config.ru ("run App").
|
|
17
29
|
def self.call(env)
|
|
18
30
|
new(env).process_request
|
|
19
31
|
end
|
|
20
32
|
|
|
21
33
|
def initialize(env)
|
|
22
|
-
# TODO: Is it ok to leave request as an attribute?
|
|
23
34
|
@request = Rack::Request.new(env)
|
|
24
|
-
@
|
|
25
|
-
@settings = Hash.new
|
|
35
|
+
@settings = RackStep::GlobalConfiguration.instance.settings
|
|
26
36
|
|
|
27
37
|
# Adding default routes to handle page not found (404).
|
|
28
|
-
|
|
38
|
+
router.add_route_for_all_verbs('notfound', 'RackStep::NotFoundController')
|
|
29
39
|
end
|
|
30
40
|
|
|
41
|
+
# TODO: Code Climate says this method is too big.
|
|
31
42
|
def process_request
|
|
32
43
|
verb = request.request_method
|
|
33
44
|
path = request.path
|
|
34
45
|
|
|
35
|
-
# In RackStep, each request is processed by a
|
|
36
|
-
#
|
|
37
|
-
# the apropriate controller
|
|
46
|
+
# In RackStep, each request is processed by a controller. The router
|
|
47
|
+
# is responsable to find, based on the given path and http verb,
|
|
48
|
+
# the apropriate controller to handle the request.
|
|
38
49
|
route = router.find_route_for(path, verb)
|
|
39
50
|
# Initialize the correspondent controller.
|
|
40
51
|
controller = Object.const_get(route.controller).new
|
|
@@ -45,7 +56,7 @@ module RackStep
|
|
|
45
56
|
# Execute the before method of this controller.
|
|
46
57
|
controller.send(:before)
|
|
47
58
|
# Execute the apropriate method/action.
|
|
48
|
-
controller.send(
|
|
59
|
+
controller.send(:process_request)
|
|
49
60
|
# Execute the after method of this controller.
|
|
50
61
|
controller.send(:after)
|
|
51
62
|
# Get from the controller what is the response for this request.
|
|
@@ -54,44 +65,11 @@ module RackStep
|
|
|
54
65
|
return response
|
|
55
66
|
end
|
|
56
67
|
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
def
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
@router.add_route('DELETE', path, controller, method)
|
|
63
|
-
@router.add_route('PUT', path, controller, method)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# Adds a new route to the application.
|
|
67
|
-
def add_route(verb, path, controller, method)
|
|
68
|
-
@router.add_route(verb, path, controller, method)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
# Let's extend the Rack Response class to add a few methods to make the life
|
|
74
|
-
# of the developer a little easier.
|
|
75
|
-
class Response < Rack::Response
|
|
76
|
-
|
|
77
|
-
# The original body= method of Rack::Response expects an array. In RackStep
|
|
78
|
-
# the user may set it as a String and we will convert it to array if
|
|
79
|
-
# necessary.
|
|
80
|
-
def body=(value)
|
|
81
|
-
if value.is_a?(String)
|
|
82
|
-
# Convert it to an array.
|
|
83
|
-
value = [value]
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
super(value)
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def content_type
|
|
90
|
-
header['Content-Type']
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def content_type=(value)
|
|
94
|
-
header['Content-Type'] = value
|
|
68
|
+
# This method was created to make it easier for the user to add routes, but it
|
|
69
|
+
# will delegate to the router singleton class.
|
|
70
|
+
def self.add_route(verb, path, controller)
|
|
71
|
+
router = Router.instance
|
|
72
|
+
router.add_route(verb, path, controller)
|
|
95
73
|
end
|
|
96
74
|
|
|
97
75
|
end
|
data/lib/response.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Extending the Rack::Response class to add a few methods to make the life
|
|
2
|
+
# of the developer a little easier.
|
|
3
|
+
|
|
4
|
+
module RackStep
|
|
5
|
+
|
|
6
|
+
class Response < Rack::Response
|
|
7
|
+
|
|
8
|
+
# The original body= method of Rack::Response expects an array. In RackStep
|
|
9
|
+
# the user may set it as a String and we will convert it to array if
|
|
10
|
+
# necessary.
|
|
11
|
+
def body=(value)
|
|
12
|
+
if value.is_a?(String)
|
|
13
|
+
# Convert it to an array.
|
|
14
|
+
value = [value]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
super(value)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Just a helper to get the content type from the response header.
|
|
21
|
+
def content_type
|
|
22
|
+
header['Content-Type']
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Just a helper to set the content type on the response header.
|
|
26
|
+
def content_type=(value)
|
|
27
|
+
header['Content-Type'] = value
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
data/lib/route.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Represents a single route. The verb can be 'GET', 'PUT', 'POST' or 'DELETE'.
|
|
2
|
+
# The path is a string with something like 'users', the controller is the
|
|
3
|
+
# name of the class that will process this type of request.
|
|
4
|
+
|
|
5
|
+
module RackStep
|
|
6
|
+
|
|
7
|
+
class Route
|
|
8
|
+
|
|
9
|
+
attr_accessor :verb, :path, :controller
|
|
10
|
+
|
|
11
|
+
def initialize(verb, path, controller)
|
|
12
|
+
@verb = verb
|
|
13
|
+
@path = path
|
|
14
|
+
@controller = controller
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Unique id (String) of the route (verb + path). Eg: 'GETuser'.
|
|
18
|
+
def id
|
|
19
|
+
verb + path
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
data/lib/router.rb
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
# Router is one of the fundamental parts of RackStep. It's the router that tell
|
|
2
|
-
# us, given a path and a http verb, what controller
|
|
3
|
-
#
|
|
2
|
+
# us, given a path and a http verb, what controller should handle the business
|
|
3
|
+
# logic of the request.
|
|
4
|
+
|
|
4
5
|
module RackStep
|
|
5
6
|
|
|
6
7
|
class Router
|
|
8
|
+
|
|
9
|
+
include Singleton
|
|
7
10
|
|
|
8
11
|
# Will store all the possible routes. By default it's empty and should be
|
|
9
12
|
# populated by the application (RackStep will add a 404 route, the others
|
|
@@ -14,13 +17,13 @@ module RackStep
|
|
|
14
17
|
@routes = Hash.new
|
|
15
18
|
end
|
|
16
19
|
|
|
17
|
-
def add_route(verb, path, controller
|
|
18
|
-
route = Route.new(verb, path, controller
|
|
20
|
+
def add_route(verb, path, controller)
|
|
21
|
+
route = Route.new(verb, path, controller)
|
|
19
22
|
routes[route.id] = route
|
|
20
23
|
end
|
|
21
24
|
|
|
22
25
|
# Given a request, will parse it's path to find what it the apropriate
|
|
23
|
-
# controller
|
|
26
|
+
# controller to respond it.
|
|
24
27
|
def find_route_for(path, verb)
|
|
25
28
|
# Ignoring the first char if path starts with '/'. This way the path of
|
|
26
29
|
# 'http//localhost/' will be the same of 'http://localhost' (both will
|
|
@@ -37,28 +40,15 @@ module RackStep
|
|
|
37
40
|
return route
|
|
38
41
|
end
|
|
39
42
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
attr_accessor :verb, :path, :controller, :method
|
|
49
|
-
|
|
50
|
-
def initialize(verb, path, controller, method)
|
|
51
|
-
@verb = verb
|
|
52
|
-
@path = path
|
|
53
|
-
@controller = controller
|
|
54
|
-
@method = method
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# Unique id of the route (verb + path). Eg: 'GETuser'.
|
|
58
|
-
def id
|
|
59
|
-
verb + path
|
|
43
|
+
# Adds new routes to the application, one for each possible http verb (GET,
|
|
44
|
+
# POST, DELETE and PUT).
|
|
45
|
+
def add_route_for_all_verbs(path, controller)
|
|
46
|
+
add_route('GET', path, controller)
|
|
47
|
+
add_route('POST', path, controller)
|
|
48
|
+
add_route('DELETE', path, controller)
|
|
49
|
+
add_route('PUT', path, controller)
|
|
60
50
|
end
|
|
61
51
|
|
|
62
52
|
end
|
|
63
53
|
|
|
64
|
-
end
|
|
54
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rackstep
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Marcio Frayze David
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-02-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: RackStep is (yet another) micro ruby framework for microservices and
|
|
14
14
|
web development.
|
|
@@ -18,7 +18,10 @@ extensions: []
|
|
|
18
18
|
extra_rdoc_files: []
|
|
19
19
|
files:
|
|
20
20
|
- lib/controller.rb
|
|
21
|
+
- lib/global_configuration.rb
|
|
21
22
|
- lib/rackstep.rb
|
|
23
|
+
- lib/response.rb
|
|
24
|
+
- lib/route.rb
|
|
22
25
|
- lib/router.rb
|
|
23
26
|
homepage: https://github.com/mfdavid/rackstep
|
|
24
27
|
licenses:
|
|
@@ -40,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
40
43
|
version: '0'
|
|
41
44
|
requirements: []
|
|
42
45
|
rubyforge_project:
|
|
43
|
-
rubygems_version: 2.
|
|
46
|
+
rubygems_version: 2.5.1
|
|
44
47
|
signing_key:
|
|
45
48
|
specification_version: 4
|
|
46
49
|
summary: RackStep micro web framework
|