Skip to content

Commit

Permalink
Fixed typo and small code improv
Browse files Browse the repository at this point in the history
  • Loading branch information
MirkoMignini committed Oct 17, 2016
1 parent ce7fbaf commit 68f500b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 25 deletions.
115 changes: 93 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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.
Expand All @@ -44,82 +42,156 @@ 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
If needed the router can be used stand alone, for example if best performances are needed, or used via the application class, slower but with a lot of more features.
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

#### 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/)
To render a template simply use the render function:

get '/render_erb' do
render 'template.erb', nil, message: 'template'
end

end

### Helpers

Expand All @@ -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:
Expand All @@ -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).

4 changes: 2 additions & 2 deletions lib/lydia/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 68f500b

Please sign in to comment.