Class: MailGrabber::Web::Application
- Inherits:
-
Object
- Object
- MailGrabber::Web::Application
- Extended by:
- ApplicationRouter
- Includes:
- DatabaseHelper, ApplicationHelper
- Defined in:
- lib/mail_grabber/web/application.rb
Constant Summary
Constants included from ApplicationRouter
MailGrabber::Web::ApplicationRouter::NAMED_SEGMENTS_PATTERN
Constants included from DatabaseHelper
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Attributes included from ApplicationRouter
Class Method Summary collapse
-
.call(env) ⇒ Array
Method to call MailGrabber::Web::Application.
Instance Method Summary collapse
-
#initialize(env) ⇒ Application
constructor
Initialize web application request and response, then process the given request.
-
#params ⇒ Hash
This method returns with extracted request parameters.
-
#path ⇒ String
Extract env value.
-
#process_request ⇒ Object
Parse the routes of the ApplicationRouter and tries to find a matching route for the request method, which was defined in the get, post, put, patch or delete blocks.
-
#render(template) ⇒ String
Render erb template from the views folder.
-
#request_method ⇒ String
Extract env value.
-
#script_name ⇒ String
Extract env value.
Methods included from ApplicationRouter
Methods included from DatabaseHelper
#connection, #connection_execute, #connection_execute_transaction, #delete_all_messages, #delete_message_by, #select_all_messages, #select_message_by, #select_message_parts_by, #select_messages_by, #store_mail
Methods included from DatabaseQueries
#create_mail_part_table, #create_mail_table, #insert_into_mail_part_query, #insert_into_mail_query, #select_messages_with_pagination_query
Methods included from ApplicationHelper
Constructor Details
#initialize(env) ⇒ Application
Initialize web application request and response, then process the given request.
30 31 32 33 34 35 |
# File 'lib/mail_grabber/web/application.rb', line 30 def initialize(env) @request = Rack::Request.new(env) @response = Rack::Response.new process_request end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
13 14 15 |
# File 'lib/mail_grabber/web/application.rb', line 13 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
13 14 15 |
# File 'lib/mail_grabber/web/application.rb', line 13 def response @response end |
Class Method Details
.call(env) ⇒ Array
Method to call MailGrabber::Web::Application. This method will call the initialize method and returns with response of the application.
22 23 24 |
# File 'lib/mail_grabber/web/application.rb', line 22 def self.call(env) new(env).response.finish end |
Instance Method Details
#params ⇒ Hash
This method returns with extracted request parameters.
49 50 51 |
# File 'lib/mail_grabber/web/application.rb', line 49 def params request.params end |
#path ⇒ String
Extract env value. If the path info is empty, then it will return with root path.
42 43 44 |
# File 'lib/mail_grabber/web/application.rb', line 42 def path @path ||= request.path_info.empty? ? '/' : request.path_info end |
#process_request ⇒ Object
Parse the routes of the ApplicationRouter and tries to find a matching route for the request method, which was defined in the get, post, put, patch or delete blocks. If the ‘extracted_params’ is nil, then it could not find any defined routes. If it can find a defined route, then it saves the params and call the given block. If it cannot find anything, then it will set the response with 404 Not Found.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mail_grabber/web/application.rb', line 73 def process_request self.class.routes[request_method].each do |route| extracted_params = route.extract_params(path) next unless extracted_params request.update_param('request_params', extracted_params) return instance_exec(&route.block) end response.status = 404 response.write('Not Found') end |
#render(template) ⇒ String
Render erb template from the views folder.
129 130 131 132 |
# File 'lib/mail_grabber/web/application.rb', line 129 def render(template) path = File.("views/#{template}", __dir__) ERB.new(File.read(path)).result(binding) end |
#request_method ⇒ String
Extract env value.
56 57 58 |
# File 'lib/mail_grabber/web/application.rb', line 56 def request_method request.request_method end |
#script_name ⇒ String
Extract env value.
63 64 65 |
# File 'lib/mail_grabber/web/application.rb', line 63 def script_name request.script_name end |