Class: MailGrabber::Web::Application

Inherits:
Object
  • Object
show all
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

DatabaseHelper::DATABASE

Instance Attribute Summary collapse

Attributes included from ApplicationRouter

#routes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationRouter

route, set_route

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

#root_path

Constructor Details

#initialize(env) ⇒ Application

Initialize web application request and response, then process the given request.

Parameters:

  • env (Hash)

    the environment variables



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

#requestObject (readonly)

Returns the value of attribute request.



13
14
15
# File 'lib/mail_grabber/web/application.rb', line 13

def request
  @request
end

#responseObject (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.

Parameters:

  • env (Hash)

    the environment variables

Returns:

  • (Array)

    the response of the web application e.g. [200, {}, [‘Hello World’]]



22
23
24
# File 'lib/mail_grabber/web/application.rb', line 22

def self.call(env)
  new(env).response.finish
end

Instance Method Details

#paramsHash

This method returns with extracted request parameters.

Returns:

  • (Hash)

    params



49
50
51
# File 'lib/mail_grabber/web/application.rb', line 49

def params
  request.params
end

#pathString

Extract env value. If the path info is empty, then it will return with root path.

Returns:

  • (String)

    path the requested path or the root path if this value is empty



42
43
44
# File 'lib/mail_grabber/web/application.rb', line 42

def path
  @path ||= request.path_info.empty? ? '/' : request.path_info
end

#process_requestObject

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.

Parameters:

  • template (String)

Returns:

  • (String)

    template with the result



129
130
131
132
# File 'lib/mail_grabber/web/application.rb', line 129

def render(template)
  path = File.expand_path("views/#{template}", __dir__)
  ERB.new(File.read(path)).result(binding)
end

#request_methodString

Extract env value.

Returns:

  • (String)

    request_method with e.g. GET, POST or DELETE etc.



56
57
58
# File 'lib/mail_grabber/web/application.rb', line 56

def request_method
  request.request_method
end

#script_nameString

Extract env value.

Returns:

  • (String)

    script_name the initial portion of the request ‘path’



63
64
65
# File 'lib/mail_grabber/web/application.rb', line 63

def script_name
  request.script_name
end