User:Hongyi23/sandbox

Rack is an abstraction layer and specification for interfaces between web servers and web frameworks for Ruby programming language. It provides a minimal, modular and adaptable API for construct web applications. Rack uses simple way, wrapping HTTP requests and responses, to combine web servers, web frameworks and web applications into a single method call. The latest version of Rack is v1.6.4, published on June 18, 2015.

Origin
Rack is a standardized dominant interface in Ruby web development world, which was released by Christian Neukirchen in 2007. The aim of Rack is to meet the demand of mix-and-match web servers and web applications, and to provide unified interface for web frameworks developer. Just like Unix pipes, to make it easy to mix and match web framework elements. Before that, there were many other methods to support Ruby and Ruby frameworks on server. CGI was first widely used method of running Ruby scripts on web server. Apache and FastCGI were most commonly methods of deploying Rails applications.

Specification overview
Rack has two sides: the web server side and web application side. Web server side refer to server or gateway, it provides an API to connect web servers and executes the application and a call function to the application side with environment information, which must be an instance of Hash that includes CGI-like headers. The application side process the HTTP request and return a response to call function which contain an array of status, headers and a body. There may be a Rack middleware between the server side and the application side. For example:
 * # Without argument runs first TARGET
 * # Route to multiple applications inside the same process.
 * # Create Apache-style logfiles.
 * # Serve static files.

Web frameworks with Rack support:
 * Camping
 * Coset
 * espresso
 * Halcyon
 * Mack
 * Maveric
 * Merb
 * Racktools::SimpleApplication
 * Ramaze
 * Ruby on Rails
 * Rum
 * Sinatra (software)
 * Sin
 * Vintage
 * Waves
 * Wee

Receive request from a HTTP client, the server invokes the web application, then send to the application. Currently wrappers are available for WEBrick, FCGI, CGI, SCGI, Thin.

A Gem of Rack is available at rubygems.org. You can install it with: gem install rack

Rack middleware
A way to filter a request and response coming into the web application, and it implements the pipeline design pattern using Rack. The principle of the Rack middleware is to separately dispatch the HTTP requests, handle the sessions and parse the parameters. The server forwards a request to the middleware. The middleware processes the request and sends a request to another web application whose response is forwarded to the client. It is common that there are multiple middleware components as a stack. Large sum of middleware components exist in the Rack library.

Specify directory
Build tools are very relevant with dependencies: files or file directories depend on each other to get complied and linked before moving on to next tasks. Things are the same in rake files, dependencies should be specified before working on tasks. Simply use  to clarify directories in rake files.

The task aims at output a string  to the file whose directory is. When the rake file is run, a directory named  is created first before output the string.

Clarify tasks
Multiple tasks can be defined in a rake file, and tasks can depend on other tasks. is used to defined tasks in a rake file.

The code above defines three tasks using : task1, task2, and final task. When run the rake file, these tasks would be handled in order. Notice that the task1 and task2 are all prerequisites of the final task.

Default tasks
Also, default task can be defined in a rake file using. A default task will be run if you type rake without any other arguments. The code above is modified and the task1 is set to be the default task.

Task description
Task description, which acts like annotation, can be added to tasks as a reminder. A task description can be defined using.

When the rake file is run with argument, the output will be like the following:

Redefine tasks
More things can be added on to an existing task. In the example below, more details are expected to be output within task one, so a task with the same name would be created. Within the new task1, more items can be added.

here is the result when this rake file is run:

Namespace
Namespace is supported in rake so that it is easy to group together similar tasks inside of one namespace.

When a task within a namespace is called, the namespace should be specified.

Invoking tasks
Rake allows to invoke a task from inside another task. In the following example, task one is invoked within the task.

which outputs:

File list
is a powerful tool for file management. In the following example, files of the same extension are found and filtered. This is the tree of the directory:

The goal is to read all the txt and doc file from the directory. The rake file could be edited as the following:

which outputs:

To add the left two txt files in folder1 and folder2, as well as filter the file start with, the rake file can be modified as:

the correct output will be shown:

Path map
In file management, it's crucial to provide correct filenames and the relative information, such as extension, and path. Pathmap takes as its argument a specification, which is a string containing codes corresponding to different parts of the original filenames.

%p: the whole original path. %f: filename without any directory portion. %n: base file name without extension or directory portions. %d: directory without file name %x: yields only the file extensions.

Add tasks to rails project
There might already be some tasks in a given rails project, and they can be run:.

If more tasks are needed to be added or refined, a rake file which contains new tasks should be put into. The rake file will automatically detacted and add to the rails tasks list. The following example is a new task aim to create three new folders defined in a rake file named.

the new task will be added to task list when run.