Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mxrnx/woah
Unobtrusive web framework
https://github.com/mxrnx/woah
framework gem rack ruby ruby-gem web web-framework
Last synced: 6 days ago
JSON representation
Unobtrusive web framework
- Host: GitHub
- URL: https://github.com/mxrnx/woah
- Owner: mxrnx
- License: gpl-3.0
- Created: 2018-06-23T17:11:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-02-28T23:24:06.000Z (9 months ago)
- Last Synced: 2024-03-15T14:42:49.987Z (8 months ago)
- Topics: framework, gem, rack, ruby, ruby-gem, web, web-framework
- Language: Ruby
- Size: 68.4 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Woah!
[![Build Status](https://travis-ci.org/knarka/woah.svg?branch=master)](https://travis-ci.org/knarka/woah)
[![Coverage Status](https://coveralls.io/repos/github/knarka/woah/badge.svg?branch=master&service=github)](https://coveralls.io/github/knarka/woah?branch=master)
[![Code Climate](https://codeclimate.com/github/knarka/woah/badges/gpa.svg)](https://codeclimate.com/github/knarka/woah)
[![Gem Version](https://badge.fury.io/rb/woah.svg)](https://badge.fury.io/rb/woah)Woah! is an unobtrusive, (extremely) minimal web framework built on Rack.
## Installation
`gem install woah`## What do I do with it???
You're gonna want to extend Woah::Base, which will be your app's, er, base.```ruby
require 'woah'class MyApp < Woah::Base
endMyApp.run!
```You now have an app that serves 404 errors on every route. Not very useful, so let's extend that.
By the way, the lines `require 'woah'` and `MyApp.run!` will always be necessary, but to keep this file a little neater, they'll be omitted in the next examples. Just pretend they're there.
```ruby
class MyApp < Woah::Base
on '/hello' do
"hey, what's up"
end
end
```When someone stumbles upon `/hello` now, they'll be greeted properly. Nice. These blocks of code are called **routes**. There's more types of blocks than just routes though. Check this out:
```ruby
class MyApp < Woah::Base
before do
@@num ||= 1
endon '/' do
"this is the root of this app, and page hit nr. #{@@num}"
endon '/hello' do
"hey, what's up. this is page hit nr. #{@@num}"
endafter do
@@num += 1
end
end
```There's two new blocks here: `before` and `after`. They do things before and after the relevant route gets executed. This example will increment a counter everytime a page is hit, regardless of what page it is.
Of course, getting pages isn't everything you can do on the Internet. There's other HTTP verbs as well, like POST. Behold:
```ruby
class MyApp < Woah::Base
before do
@@content ||=
''\
''\
''
endon '/' do
@@content
endon '/', 'POST' do
@@content = 'thanks for clicking!'
end
end
```As soon as you click the button on `/`, the message on the page will transform.
Of course, sometimes you want routes to be flexible, and to catch more than one expression. For this, you can use regular expressions instead of strings as your routes, just like this:
```ruby
class MyApp < Woah::Base
on %r{^/greet/(\w+)$} do
"oh, hello, I didn't see you there #{match[1]}"
end
end
```Now, visiting `/greet/Socrates` will greet you with your own name (assuming your name is Socrates). Wonderful. By the way, you may have noticed the regex here is delimited by `%r{}`, instead of the more common `//`. This is because of how common slashes are in routes, so it's recommended to use this syntax. You can use slashes to delimit your regex though, if you like. I won't judge you.
Redirects are possible as well:
```ruby
class MyApp < Woah::Base
on '/' do
redirect_to '/landing'
endon '/landing' do
'welcome'
end
end
```So are cookies:
```ruby
class MyApp < Woah::Base
on '/' do
cookie 'chunky' || 'no cookie set'
endon '/set' do
cookie 'chunky', 'bacon'
endon '/del' do
cookie 'chunky', :delete
end
end
```Upon first visiting, this page will tell you there's no cookie set. After visiting `/set` however, it'll display `bacon`, as that is now the content of the `chunky` cookie. Visiting `/del` will delete the cookie again.
We're nearing the end of this little guide already, I'm afraid. However, there's still one more trick you need to see. Look, sometimes, you might disagree with the things Woah! thinks up for you. That's why you can override everything Woah! is about to send, if you so please. Por exemplo:
```ruby
class MyApp < Woah::Base
on '/' do
'(insert super secret information)'
endon %r{^/pass/(\w+)$} do
@password = match[1]
'logged in! back to root'
endafter do
unless @password && @password == 'penguin'
set :status, 403
set :body, 'log in first!'
end
end
end
```That's all. Have fun!