rackstep 0.0.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 +7 -0
- data/lib/controller.rb +42 -0
- data/lib/rackstep.rb +58 -0
- data/lib/router.rb +61 -0
- metadata +46 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: d43164a0ecd96e90a43c8c343e5b5736473e3cbd
|
|
4
|
+
data.tar.gz: 3591804ceda2a7619911120c1ee8f08c26d97a18
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d6658db5f112bb9073706866abaad6392d5a1197adb290b63b30133c7b22dd8aa6b9eab716c938d8dc31e00a05f99aa6ac2a6c5e17f19c9520ef88b1cc4b1c67
|
|
7
|
+
data.tar.gz: 2bfb9aab2733304ce1d645c3e6765951a3968d61aaad2ee45b7637420a8c62afaff01e54b9c28cc0b68a655f6e4a48517d35a3522271e30ce85d184c0d1806cd
|
data/lib/controller.rb
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Abstract controller class with some helper methods. ALL your controllers
|
|
2
|
+
# MUST use this one as a superclass.
|
|
3
|
+
|
|
4
|
+
module RackStep
|
|
5
|
+
|
|
6
|
+
class Controller
|
|
7
|
+
|
|
8
|
+
# The request will be injected here.
|
|
9
|
+
attr_accessor :request
|
|
10
|
+
# Represents the response information that will be delivered to the user
|
|
11
|
+
# (a Hash with contentType, content and httpStatus).
|
|
12
|
+
# By default httpStatus is 200 and contentType is application/json.
|
|
13
|
+
attr_accessor :response
|
|
14
|
+
|
|
15
|
+
def initialize
|
|
16
|
+
@response = Hash.new
|
|
17
|
+
@response[:contentType] = 'application/json'
|
|
18
|
+
@response[:httpStatus] = 200
|
|
19
|
+
@response[:content] = ''
|
|
20
|
+
@pagesDirectory = 'app/public/pages'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# RackStep will always execute this method before delegating the request
|
|
24
|
+
# processing to the specified method. The user may overwrite this method.
|
|
25
|
+
# This can be used to check for access authorization or any piece of code
|
|
26
|
+
# that must be executed before every request for this controller.
|
|
27
|
+
def before
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# This is not the best way to serve static content. In production, consider
|
|
31
|
+
# using Nginx or Apache. Using ruby/rack to serve static content is a waste
|
|
32
|
+
# of resources and should be only used for low traffic web pages. This
|
|
33
|
+
# method is provided so that in this circumstances you may use it to keep a
|
|
34
|
+
# simpler architecture.
|
|
35
|
+
def render_page(pageName)
|
|
36
|
+
response[:contentType] = 'text/html'
|
|
37
|
+
File.read("#{@pagesDirectory}/#{pageName}.html")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
data/lib/rackstep.rb
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require_relative 'controller'
|
|
2
|
+
require_relative 'router'
|
|
3
|
+
|
|
4
|
+
module RackStep
|
|
5
|
+
|
|
6
|
+
class App
|
|
7
|
+
|
|
8
|
+
# We will store the request and create a router in this class initializer.
|
|
9
|
+
attr_reader :request, :router
|
|
10
|
+
|
|
11
|
+
# Static method called from config.ru ("run App").
|
|
12
|
+
def self.call(env)
|
|
13
|
+
new(env).process_request
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(env)
|
|
17
|
+
@request = Rack::Request.new(env)
|
|
18
|
+
@router = RackStep::Router.new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def process_request
|
|
22
|
+
# Trying to find what controller should process this request.
|
|
23
|
+
# This will return a hash with the name of the controller, the
|
|
24
|
+
# method (action), etc.
|
|
25
|
+
route = router.find_route_for(request)
|
|
26
|
+
# If no valid route is found, will break the request and return http 404
|
|
27
|
+
# (page not found).
|
|
28
|
+
if (route == nil)
|
|
29
|
+
return page_not_found_response
|
|
30
|
+
end
|
|
31
|
+
# Initialize the correspondent controller
|
|
32
|
+
controller = Object.const_get(route.controller).new
|
|
33
|
+
# Inject the request into the Controller
|
|
34
|
+
controller.request = request
|
|
35
|
+
# Execute the before method of this controller
|
|
36
|
+
controller.send(:before)
|
|
37
|
+
# Execute the apropriate method/action
|
|
38
|
+
controller.send(route.method)
|
|
39
|
+
response = controller.response
|
|
40
|
+
# Generate a rack response that will be returned to the user
|
|
41
|
+
Rack::Response.new( response[:content],
|
|
42
|
+
response[:httpStatus],
|
|
43
|
+
{'Content-Type' => response[:contentType]} )
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Will use this as response when no route is found.
|
|
47
|
+
def page_not_found_response
|
|
48
|
+
Rack::Response.new("404 - Page not found", 404)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Adds a new route to the application.
|
|
52
|
+
def add_route(verb, path, controller, method)
|
|
53
|
+
@router.add_route(verb, path, controller, method)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
data/lib/router.rb
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
|
|
2
|
+
module RackStep
|
|
3
|
+
|
|
4
|
+
class Router
|
|
5
|
+
|
|
6
|
+
# Will store all the possible routes. By default it's empty and should be
|
|
7
|
+
# populated by the application.
|
|
8
|
+
attr_accessor :routes
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@routes = Hash.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def add_route(verb, path, controller, method)
|
|
15
|
+
route = Route.new(verb, path, controller, method)
|
|
16
|
+
routes[route.id] = route
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Given a request, will parse it's path to find what it the apropriate
|
|
20
|
+
# controller and mehod/action to respond it.
|
|
21
|
+
def find_route_for(request)
|
|
22
|
+
path = request.path
|
|
23
|
+
# Ignoring the first char if path starts with '/'. This way the path of
|
|
24
|
+
# 'http//localhost/' will be the same of 'http://localhost' (both will
|
|
25
|
+
# be empty strings).
|
|
26
|
+
path = path[1..-1] if path[0] == '/'
|
|
27
|
+
# Extracting the type of request (GET, POST, PUT, DELETE).
|
|
28
|
+
verb = request.request_method
|
|
29
|
+
# Re-creating the route id (verb + path).
|
|
30
|
+
route_id = verb + path
|
|
31
|
+
# Getting the correspondent route or nil if route is invalid.
|
|
32
|
+
route = routes[route_id]
|
|
33
|
+
|
|
34
|
+
return route
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Represents a single route. The verb can be 'GET', 'PUT', 'POST' or 'DELETE'.
|
|
40
|
+
# The path is a string with something like 'users', the controller is the
|
|
41
|
+
# name of the class that will process this type of request and the method
|
|
42
|
+
# parameter is the name of the method that will be executed.
|
|
43
|
+
class Route
|
|
44
|
+
|
|
45
|
+
attr_accessor :verb, :path, :controller, :method
|
|
46
|
+
|
|
47
|
+
def initialize(verb, path, controller, method)
|
|
48
|
+
@verb = verb
|
|
49
|
+
@path = path
|
|
50
|
+
@controller = controller
|
|
51
|
+
@method = method
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Unique id of the route (verb + path). Eg: 'GETuser'
|
|
55
|
+
def id
|
|
56
|
+
verb + path
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rackstep
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Marcio Frayze
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-10-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: RackStep is (yet another) micro ruby framework for web development.
|
|
14
|
+
email: mfdavid@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/controller.rb
|
|
20
|
+
- lib/rackstep.rb
|
|
21
|
+
- lib/router.rb
|
|
22
|
+
homepage: https://github.com/mfdavid/RackStep
|
|
23
|
+
licenses:
|
|
24
|
+
- MIT
|
|
25
|
+
metadata: {}
|
|
26
|
+
post_install_message:
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubyforge_project:
|
|
42
|
+
rubygems_version: 2.4.5.1
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 4
|
|
45
|
+
summary: RackStep micro web framework
|
|
46
|
+
test_files: []
|