-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rb
More file actions
65 lines (49 loc) · 1.04 KB
/
Copy pathserver.rb
File metadata and controls
65 lines (49 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'sinatra'
require 'sinatra/reloader'
require 'pry'
require 'csv'
require_relative 'app/models/article.rb'
set :bind,'0.0.0.0'
get "/" do
redirect '/articles'
end
get "/articles" do
@article_objects = []
CSV.foreach('articles.csv', headers: true) do |row|
new_article = Article.new(
row["title"],
row["url"],
row["description"]
)
@article_objects << new_article
end
erb :index
end
get "/articles/new" do
erb :new
end
def retreive_articles
article_objects = []
CSV.foreach('articles.csv', headers: true) do |row|
new_article = Article.new(
row["title"],
row["url"],
row["description"]
)
article_objects << new_article
end
article_objects
end
post "/articles" do
title = params["title"]
url = params["url"]
description = params["description"]
if description == "" || title == "" || url == ""
redirect '/articles/new'
else
CSV.open("articles.csv", "a") do |csv_file|
csv_file << [title, url, description]
end
end
redirect "/articles"
end