Skip to content

Commit

Permalink
Code refactored to use Rack::Response instead of a hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
marciofrayze committed Oct 26, 2015
1 parent 830f44f commit fe7043a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 47 deletions.
27 changes: 9 additions & 18 deletions lib/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class Controller
# The request will be injected here.
attr_accessor :request

# Represents the response information that will be delivered to the user
# (a Hash with type, content and httpStatus).
# By default httpStatus is 200 and type is application/json.
# The Rack::Response object that will be delivered to the user.
attr_accessor :response

# The 'global' app settings will be injected here. This may contain
Expand All @@ -19,12 +17,10 @@ class Controller
attr_accessor :settings

def initialize
# TODO: Maybe create a RackStep::Response class?
@response = Hash.new
@response[:type] = 'application/json'
@response[:httpStatus] = 200
@response[:content] = ''
@response[:headers] = Hash.new
@response = Rack::Response.new
@response.body = ''
@response.header['Content-Type'] = 'application/json'
@response.status = 200
end

# RackStep will always execute this method before delegating the request
Expand All @@ -34,22 +30,17 @@ def initialize
def before
end

# Since headers is a hash, when using it directly the syntax is a little
# wierd, so let's create an alias for it
def headers
@response[:headers]
end

end


# This controller will handle error the error "page not found". The user may
# overwrite this by creating new router to 'notfound'.
class ErrorController < RackStep::Controller

def not_found
@response[:type] = 'text/plain'
@response[:httpStatus] = 404
@response[:content] = '404 - Page not found'
@response.body = '404 - Page not found'
@response.header['Content-Type'] = 'text/plain'
@response.status = 404
end

end
Expand Down
19 changes: 10 additions & 9 deletions lib/rackstep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ def process_request
controller.send(route.method)
# Get from the controller what is the response for this request.
response = controller.response
# Adding the content type to the header (other things may have been
# inserted by the user).
response[:headers]['Content-Type'] = response[:type]
# Generate a rack response that will be returned to the user.
rackResponse = Rack::Response.new( response[:content],
response[:httpStatus],
response[:headers] )
# { 'Content-Type' => response[:type],
# 'WWW-Authenticate' => 'Basic realm="Restricted Area"' } )
# TODO: Review this.
# The response body must be an array. If it was set with an String,
# transform it to an array.
if response.body.is_a?(String)
body = Array.new
body << response.body
response.body = body
end

return response
end

# Adds new routes to the application, one for each possible http verb (GET,
Expand Down
44 changes: 24 additions & 20 deletions test/util/sample_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ def initialize(env)
# Creating the controller that will process the request.
class Root < RackStep::Controller

include RackStep::Controller::HtmlRendering
include RackStep::Controller::ErbRendering
include RackStep::Controller::BasicHttpAuthentication
include RackStep::Controller::HtmlRendering

def index
# RackStep was created mainly to be used for microservices and single page
# applications, so by default it will set the content type of the response
# as JSON, but for this example, let's chance that to plain txt.
response[:type] = 'text/plain'
# to JSON, but for this example, let's chance that to plain txt.
response.header['Content-Type'] = 'text/plain'

# Anything that is returned by the controller will be the body of the
# request response back to the user. Let's return a simple string of text.
response[:content] = "Welcome to the RackStep Sample App."
response.body = 'Welcome to the RackStep Sample App.'
end

def my_json_service
Expand All @@ -64,47 +64,51 @@ def my_json_service
user['age'] = '27'
user['job'] = 'Developer'

response[:content] = user.to_json
response.body = user.to_json
end

def html_page
# Overwriting default directory (don't wanna create the whole default
# folders structure).
pages_directory = 'test/util/pages'

response[:content] = render_page('justatestpage', pages_directory)
response[:type] = 'text/html'
response.body = render_page('justatestpage', pages_directory)
response.header['Content-Type'] = 'text/html'
end

def settings_test_service
# At the sample_app.rb we set a :config. Lets retrieve it and send back as
# a response.
# At the initialize of sampleApp we set a :config. Lets retrieve it and
# send back as the body of our.

response[:content] = settings[:test]
response.body = settings[:test]
end

def render_erb_test
# Let's render an ERB template to test the RackStep::ErbRendering module.
pages_directory = 'test/util/pages'

@templateAttributeTest = "This is the content of the attribute."
response[:content] = render_erb('justatesttemplate', pages_directory)
response[:type] = 'text/html'
# Every attribute should be available for the template. In our case, it is
# expecting to find the following attribute:
@templateAttributeTest = 'This is the content of the attribute.'
response.body = render_erb('justatesttemplate', pages_directory)
response.header['Content-Type'] = 'text/html'
end

def protected_page
response[:type] = 'text/html'
response.header['Content-Type'] = 'text/html'

credentials = basic_access_authentication_credentials
if credentials != ['myBoringUsername', 'myBoringPassword']
# TODO: Make life easier for the app developer.
response[:httpStatus] = 401
response[:content] = "Access Denied"
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
return
response.status = 401
response.body = 'Access Denied'
# In a real life application you must set this header so the browser
# knows that it should ask for the username and password.
response.headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
else
# The credentials are fine! Let's show the page content.
response.body = 'Welcome! You are now logged in.'
end

response[:content] = "Welcome! You are now logged in."
end

end

0 comments on commit fe7043a

Please sign in to comment.