Skip to content

Commit

Permalink
Examples updated
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbarsukov committed Jul 25, 2021
1 parent 7aec87a commit c73b290
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 69 deletions.
File renamed without changes.
37 changes: 34 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,47 @@

Here are some examples of applications created with Yaframework:

1. ###[Hello World](https://github.com/maxbarsukov/yaframework/tree/master/examples/hello-world)
1. [**Hello World**](https://github.com/maxbarsukov/yaframework/raw/master/examples/hello_world.rb)

A simple application that displays "Hello world" to you.


2. ###[Params](https://github.com/maxbarsukov/yaframework/tree/master/examples/params)
2. [**Params**](https://github.com/maxbarsukov/yaframework/raw/master/examples/params.rb)

Shows how the application parameters can be used


3. ###[Halts and Redirects](https://github.com/maxbarsukov/yaframework/tree/master/examples/halts-and-redirects)
3. [**Halts and Redirects**](https://github.com/maxbarsukov/yaframework/raw/master/examples/halts_and_redirects.rb)

Shows how to use halts and redirects


4. [**Headers**](https://github.com/maxbarsukov/yaframework/raw/master/examples/headers.rb)

How to add and view headers.


5. [**Error handling**](https://github.com/maxbarsukov/yaframework/raw/master/examples/error_handling.rb)

How to handle errors


6. [**Content Types**](https://github.com/maxbarsukov/yaframework/raw/master/examples/content_types.rb)

Using json, html, or plain text


7. [**Cookies**](https://github.com/maxbarsukov/yaframework/raw/master/examples/cookies.rb)

Adding and deleting cookies


## Installation

Clone this repo and go to this folder.
Then, run `bundle install` to install this gem.

## Run

Run with `ruby hello-world.rb` or any other file and view at http://localhost:4567

24 changes: 24 additions & 0 deletions examples/content_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "yaframework"
app = Yaframework::Application

app.get "/" do
response.redirect "/html"
end

app.get "/html" do
response.html "This is <b>HTML</b>, where you cat use some <small>tags</small>.
<br/>JSON <a href=\"/json\">here</a>,
<br/>Plain text <a href=\"/text\">here</a>"
end

app.get "/text" do
response.text "Just plain text.<br/>Boring, even tags don't work..."
end

app.get "/json" do
response.json "{ \"The awesomeness of this framework\": \"100/100\" }"
end

app.listen(4567)
16 changes: 16 additions & 0 deletions examples/cookies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "yaframework"
app = Yaframework::Application

app.get "/" do
response.set_cookie("foo", "bar")
"Your cookies, sir: #{response["Set-Cookie"]}.<br/>Go <a href=\"/delete\">here</a> to delete them"
end

app.get "/delete" do
response.delete_cookie("foo")
"Your cookies, sir: #{response["Set-Cookie"]}"
end

app.listen(4567)
18 changes: 18 additions & 0 deletions examples/error_handling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "yaframework"
app = Yaframework::Application

app.get "/" do
"This is the root, go somewhere else..<br/>Maybe <a href=\"/asdfg\">here</a>, idk"
end

app.handle 404 do
"Hey, I just handled a 404 error!<br/>The dude from the previous page deceived you!"
end

app.handle 500 do
"Error 500, this shouldn't have happened..."
end

app.listen(4567)
12 changes: 0 additions & 12 deletions examples/halts-and-redirects/README.md

This file was deleted.

6 changes: 0 additions & 6 deletions examples/halts-and-redirects/config.ru

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end

app.get "/hello" do
"Hello world!"
"Hi, you were redirected here from the root page. You can go <a href=\"/error\"></a> to get a 401 error"
end

app.get "/error" do
Expand Down
11 changes: 11 additions & 0 deletions examples/headers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "yaframework"
app = Yaframework::Application

app.get "/" do
request[:username] = "IVAN!"
"Now 'request[:username]': #{request[:username]} contains in 'request.params': #{request.params}"
end

app.listen(4567)
5 changes: 0 additions & 5 deletions examples/hello-world/Gemfile

This file was deleted.

12 changes: 0 additions & 12 deletions examples/hello-world/README.md

This file was deleted.

6 changes: 0 additions & 6 deletions examples/hello-world/config.ru

This file was deleted.

1 change: 1 addition & 0 deletions examples/hello-world/app.rb → examples/hello_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "yaframework"
app = Yaframework::Application

# Just go to the root page and say hello
app.get "/" do
"Hello world!"
end
Expand Down
4 changes: 3 additions & 1 deletion examples/params/app.rb → examples/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
require "yaframework"
app = Yaframework::Application

# Any route, like /max or /ivan
app.get "/:name" do
"Hello #{request.params[:name]}!"
end

app.get "/:name/foo/:bar" do
# You can try /max/from/rnd and see it with your own eyes
app.get "/:name/from/:bar" do
"Hello #{request.params[:name]} from #{request.params[:bar]}!"
end

Expand Down
5 changes: 0 additions & 5 deletions examples/params/Gemfile

This file was deleted.

12 changes: 0 additions & 12 deletions examples/params/README.md

This file was deleted.

6 changes: 0 additions & 6 deletions examples/params/config.ru

This file was deleted.

0 comments on commit c73b290

Please sign in to comment.