rackstep 0.0.2 → 0.0.3

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/controller.rb +77 -16
  3. data/lib/rackstep.rb +61 -20
  4. data/lib/router.rb +10 -7
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bcdfa5d5dd2b3e0861787e21c39e6483ff74dac
4
- data.tar.gz: 0fb53c222f6489e3cce137c87e5379178ca2840a
3
+ metadata.gz: c61f80d81dce85a8edaf0aed8f3c9a6191a335a6
4
+ data.tar.gz: 45afba957a83cb16b42daeb03edfd1e35507a642
5
5
  SHA512:
6
- metadata.gz: 6c8b46758537e23e28251a61f65f6c5e20f37f1191e2e7b8f0305d630af3e5d8fc1544646e820453818d2fc477020c38af258071413a9247c01ebfa7234f7045
7
- data.tar.gz: 569bd479abffb8c79b51de1a5f4c758ab7c6949d54f4b8fc3bb8e6b5e8ee05c8292482e5204a99a0ca4552f561d8b60b1bf5640bdbef368b888fbf010c3b34ae
6
+ metadata.gz: 55ddab5bb82ae17d6821fb471e015f57f7cfa35991efdbef1d74932199918b33a6f3bd9f46947905248483864f328306be47d9645ee887782ddd6f65ab7f9ca4
7
+ data.tar.gz: 3c939a6c913167405185302027236d64ee889a245785a394d3ca16afe126231cdad192c7dc9a22aae350cbd3824a39403bf9a0863401a05b1bc88bbcd841ae12
data/lib/controller.rb CHANGED
@@ -7,17 +7,20 @@ module RackStep
7
7
 
8
8
  # The request will be injected here.
9
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.
10
+
11
+ # The Rack::Response object that will be delivered to the user.
13
12
  attr_accessor :response
14
13
 
14
+ # The 'global' app settings will be injected here. This may contain
15
+ # references to things that should be initialize only once during the app
16
+ # start (eg: database connection).
17
+ attr_accessor :settings
18
+
15
19
  def initialize
16
- @response = Hash.new
17
- @response[:contentType] = 'application/json'
18
- @response[:httpStatus] = 200
19
- @response[:content] = ''
20
- @pagesDirectory = 'app/public/pages'
20
+ @response = RackStep::Response.new
21
+ @response.body = ''
22
+ @response.content_type = 'application/json'
23
+ @response.status = 200
21
24
  end
22
25
 
23
26
  # RackStep will always execute this method before delegating the request
@@ -27,16 +30,74 @@ module RackStep
27
30
  def before
28
31
  end
29
32
 
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")
33
+ # RackStep will always execute this method after processing the request
34
+ # of to the specified method. The user may overwrite this method.
35
+ # This can be used to check for logging or any piece of code
36
+ # that must be executed after every request for this controller.
37
+ def after
38
+ end
39
+
40
+ end
41
+
42
+
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
+
47
+ def not_found
48
+ @response.body = '404 - Page not found'
49
+ @response.content_type = 'text/plain'
50
+ @response.status = 404
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ # A module for controllers to add static html pages rendering.
58
+ # 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 is a waste
60
+ # of resources and should be only used for low traffic web pages. This
61
+ # method is provided so that in this circumstances you may use it to keep a
62
+ # simpler architecture.
63
+ module RackStep::Controller::HtmlRendering
64
+ def render_page(page_name, pages_directory = 'app/public/pages')
65
+ File.read("#{pages_directory}/#{page_name}.html")
66
+ end
67
+ end
68
+
69
+ # A module for controllers to add ERB template rendering. RackStep is not meant
70
+ # to be used for template rendering. We recommend you to use a SPA (Single Page
71
+ # Application) approach. But if you want to, you may include this module into
72
+ # your controller and render ERB templates, following the old ruby web way.
73
+ # TODO: Add layout support.
74
+ module RackStep::Controller::ErbRendering
75
+
76
+ require 'erb'
77
+
78
+ def render_erb(template_name, erb_template_directory = 'app/public/pages')
79
+ template_path = "#{erb_template_directory}/#{template_name}.erb"
80
+ erb = ERB.new(File.open(template_path).read)
81
+ return erb.result(binding)
82
+ end
83
+
84
+ end
85
+
86
+ # A module for controllers to add basic http authentication helper method.
87
+ module RackStep::Controller::BasicHttpAuthentication
88
+
89
+ def basic_access_authentication_credentials
90
+ credentials = nil
91
+ # Trying to get the basic http authentication credentials from the request.
92
+ auth = Rack::Auth::Basic::Request.new(request.env)
93
+ if auth.provided? and auth.basic? and auth.credentials
94
+ credentials = auth.credentials
95
+ else
96
+ # No credentials found. Will return empty to avoid returning nil.
97
+ credentials = ['', '']
38
98
  end
39
99
 
100
+ return credentials
40
101
  end
41
102
 
42
103
  end
data/lib/rackstep.rb CHANGED
@@ -8,44 +8,59 @@ module RackStep
8
8
  # We will store the request and create a router in this class initializer.
9
9
  attr_reader :request, :router
10
10
 
11
+ # Settings is a hash that will be injected into the controller. This hash
12
+ # may contain "global" settings, like a connection to database, and other
13
+ # things that should be initiaized only once while the app is starting.
14
+ attr_accessor :settings
15
+
11
16
  # Static method called from config.ru ("run App").
12
17
  def self.call(env)
13
18
  new(env).process_request
14
19
  end
15
20
 
16
21
  def initialize(env)
22
+ # TODO: Is it ok to leave request as an attribute?
17
23
  @request = Rack::Request.new(env)
18
24
  @router = RackStep::Router.new
25
+ @settings = Hash.new
26
+
27
+ # Adding default routes to handle page not found (404).
28
+ for_all_verbs_add_route('notfound', 'RackStep::ErrorController', 'not_found')
19
29
  end
20
30
 
21
31
  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
+ verb = request.request_method
33
+ path = request.path
34
+
35
+ # In RackStep, each request is processed by a method of a controller. The
36
+ # router is responsable to find, based on the given path and http verb,
37
+ # the apropriate controller and method to handle the request.
38
+ route = router.find_route_for(path, verb)
39
+ # Initialize the correspondent controller.
32
40
  controller = Object.const_get(route.controller).new
33
- # Inject the request into the Controller
41
+ # Inject the request into the controller.
34
42
  controller.request = request
35
- # Execute the before method of this controller
43
+ # Inject the settings into the controller.
44
+ controller.settings = settings
45
+ # Execute the before method of this controller.
36
46
  controller.send(:before)
37
- # Execute the apropriate method/action
47
+ # Execute the apropriate method/action.
38
48
  controller.send(route.method)
49
+ # Execute the after method of this controller.
50
+ controller.send(:after)
51
+ # Get from the controller what is the response for this request.
39
52
  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]} )
53
+
54
+ return response
44
55
  end
45
56
 
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)
57
+ # Adds new routes to the application, one for each possible http verb (GET,
58
+ # POST, DELETE and PUT).
59
+ def for_all_verbs_add_route(path, controller, method)
60
+ @router.add_route('GET', path, controller, method)
61
+ @router.add_route('POST', path, controller, method)
62
+ @router.add_route('DELETE', path, controller, method)
63
+ @router.add_route('PUT', path, controller, method)
49
64
  end
50
65
 
51
66
  # Adds a new route to the application.
@@ -55,4 +70,30 @@ module RackStep
55
70
 
56
71
  end
57
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
95
+ end
96
+
97
+ end
98
+
58
99
  end
data/lib/router.rb CHANGED
@@ -1,10 +1,13 @@
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 and method should handle
3
+ # the business logic of the request.
2
4
  module RackStep
3
5
 
4
6
  class Router
5
7
 
6
8
  # Will store all the possible routes. By default it's empty and should be
7
- # populated by the application.
9
+ # populated by the application (RackStep will add a 404 route, the others
10
+ # should be added by the user).
8
11
  attr_accessor :routes
9
12
 
10
13
  def initialize
@@ -18,18 +21,18 @@ module RackStep
18
21
 
19
22
  # Given a request, will parse it's path to find what it the apropriate
20
23
  # controller and mehod/action to respond it.
21
- def find_route_for(request)
22
- path = request.path
24
+ def find_route_for(path, verb)
23
25
  # Ignoring the first char if path starts with '/'. This way the path of
24
26
  # 'http//localhost/' will be the same of 'http://localhost' (both will
25
27
  # be empty strings).
26
28
  path = path[1..-1] if path[0] == '/'
27
- # Extracting the type of request (GET, POST, PUT, DELETE).
28
- verb = request.request_method
29
29
  # Re-creating the route id (verb + path).
30
30
  route_id = verb + path
31
31
  # Getting the correspondent route or nil if route is invalid.
32
32
  route = routes[route_id]
33
+ # If no route was found, set it to 'notfound' route (maintaining the
34
+ # original verb).
35
+ route = routes["#{verb}notfound"] if route == nil
33
36
 
34
37
  return route
35
38
  end
@@ -51,7 +54,7 @@ module RackStep
51
54
  @method = method
52
55
  end
53
56
 
54
- # Unique id of the route (verb + path). Eg: 'GETuser'
57
+ # Unique id of the route (verb + path). Eg: 'GETuser'.
55
58
  def id
56
59
  verb + path
57
60
  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.2
4
+ version: 0.0.3
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: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2015-10-27 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.
@@ -20,7 +20,7 @@ files:
20
20
  - lib/controller.rb
21
21
  - lib/rackstep.rb
22
22
  - lib/router.rb
23
- homepage: https://github.com/mfdavid/RackStep
23
+ homepage: https://github.com/mfdavid/rackstep
24
24
  licenses:
25
25
  - MIT
26
26
  metadata: {}