diff --git a/README.md b/README.md index 76510ab..ba2db51 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,7 @@ Lightweight, fast and easy to use small ruby web framework. Add this line to your application's Gemfile: -```ruby -gem 'lydia' -``` + gem 'lydia' And then execute: @@ -27,7 +25,7 @@ Or install it yourself as: ## Another ruby web framework? WTF? -This project is not intended to become a top notch framework or the new rails, it's just an experiment. +This project is not intended to become a top notch framework or the new rails, it's just an experiment. The goals of this project are: * [Rack](https://github.com/rack/rack/) based. @@ -44,35 +42,35 @@ The goals of this project are: ## Usage ### First example -Create a ruby file, fior example hello_world.rb, require 'lydia' and using the routing functions without creating an application object. +Create a ruby file, for example hello_world.rb, require 'lydia' and using the routing functions without creating an application object. require 'lydia' - + get '/' do 'Hello world!' end Just run it to start a webrick server that responds hello world to root. - - $ ruby hello_world.rb + + $ ruby hello_world.rb ### Application If preferred it's possible to create an application object and run using rackup command, in this case don't require lydia but lydia/application to avoid the server auto start. For example a minimal config.ru file can be: require 'lydia/application' - + class App < Lydia::Application get '/' do 'Hello world!' end end - + run App.new - + Start the server using rackup command: $ rackup - + ### Router #### Stand alone router @@ -80,10 +78,10 @@ If needed the router can be used stand alone, for example if best performances a Stand alone example, note that the return type must be in rack standard format, an array of three that is status, header, body (as array): require 'lydia/router' - + class App < Lydia::Router get '/' do - body = 'Hellow world!' + body = 'Hello world!' [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]] end end @@ -91,26 +89,101 @@ Stand alone example, note that the return type must be in rack standard format, #### HTTP verbs Supports standard HTTP verbs: HEAD GET PATCH PUT POST DELETE OPTIONS. -### DOCUMENTATION IN PROGRESS... +#### Query parameters -#### Parameters + # matches /querystring&name=mirko + get '/querystring' do + # do something + # request.params[:name] contains 'mirko' + end -#### Wildcards +#### Wildcard + + # matches /wildcard/everything + get '/wildcard/* ' do + # do something + end #### Named route parameters + # matches /users/1/comments/3/edit + get '/users/:id/comments/:comment_id' do + # do something + # request.params[:id] contains 1 + # request.params[:comment_id] contains 3 + end + +Automatically add to response.params every route params. + #### Regular expressions -#### Not found routes and errors + # matches /regexp + get %r{/regexp$}i do + # do something + end #### Skip to next route +To skip to the next matching route use next_route method. + + get '/next_route' do + next_route + end + + get '/next_route' do + 'Hello this is the next route' + end #### Halting +To halt the execution raising an Halt error use halt method, by default the standard halt page is displayed but it's possible to pass a custom response as halt parameter. + + get '/halt' do + halt + end + + get '/custom_halt' do + halt 'Custom halt' + end ### Return types +Lydia supports various returns types other that the standard rack response object. The supported type are: + +#### Rack::Response or Lydia::Response +Using the standard rack response the framework does nothing other than pass the response to rack. If response finish method was not called the framework will. + +#### String +Returning a string is intended as the response body, the headers and a 200 status are automatically added. + +#### Array of 2 or 3 elements +Returning an array of 2 elements means that the first is the status and the second the body. +Returning an array of 3 elements means that the first is the status, the second the headers, and the third the body. + +#### Fixnum +Returning a fixnum is intended as the response code. Useful to return a response code without a body. + +#### Hash +An hash is intended as a json, json content type is automatically added. + +#### Object that responds to :each +Returning a generic object is admitted accorind rack specifications if responds to :each method. ### Filters +#### Before and after Filters +Before and after filters are available as in the following example: + + before do + # do something + end + + after do + # do something + end + +#### Redirects +To define a redirect use the following syntax: + + redirect '/from_route', to: '/to_route' + ### Templates Extensive templates support using [tilt](https://github.com/rtomayko/tilt/) @@ -118,8 +191,7 @@ To render a template simply use the render function: get '/render_erb' do render 'template.erb', nil, message: 'template' - end - + end ### Helpers @@ -135,7 +207,7 @@ It's possible to read request parameters using params helper: get '/test' do params['my_param'] - end + end #### Content type It's possible to force the response return type using content_type helper: @@ -157,4 +229,3 @@ It's possible to force the response return type using content_type helper: ## License The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). - diff --git a/lib/lydia/response.rb b/lib/lydia/response.rb index d052d9b..73948c3 100644 --- a/lib/lydia/response.rb +++ b/lib/lydia/response.rb @@ -37,8 +37,8 @@ def build_string(input) end def build_array(input) - @status = input.first - write(input.last.is_a?(Array) ? input.last[0] : input.last) + @status, *, body = input + write(body.is_a?(Array) ? body[0] : body) headers.merge!(input[1]) if input.count == 3 end diff --git a/spec/response_spec.rb b/spec/response_spec.rb index 70773e8..e615cd1 100644 --- a/spec/response_spec.rb +++ b/spec/response_spec.rb @@ -67,7 +67,7 @@ expect(result[2][0]).to eq(body[1][0]) end - it 'builds using an array of two (body is noy an array)' do + it 'builds using an array of two (body is not an array)' do body = [201, 'Body'] result = response.build(body) expect(result).not_to be_nil