-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
67 lines (57 loc) · 1.24 KB
/
app.rb
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
66
67
require 'sinatra'
require 'active_record'
# modelo de datos
class Post < ActiveRecord::Base
end
# inicializacion conector activerecord
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'blog.db'
)
ActiveRecord::Base.include_root_in_json = false
# Configurar sinatra
after do
ActiveRecord::Base.connection.close
end
set :public_folder, File.dirname(__FILE__)
set :views, File.dirname(__FILE__)
# API REST
get '/' do
erb :index
end
get '/posts' do
{ :posts => Post.all }.to_json
end
get '/posts/:id' do
Post.where(:id => params['id']).first.to_json
end
post '/posts' do
data = JSON.parse(request.body.read)['post']
nuevo = Post.new(
:titulo => data['titulo'],
:texto => data['texto'],
:autor => data['autor']
)
nuevo.save
end
put '/posts/:id' do
_post = Post.where(:id => params['id']).first
data = JSON.parse(request.body.read)['post']
if _post
if data.has_key?('titulo')
_post.titulo = data['titulo']
end
if data.has_key?('texto')
_post.texto = data['texto']
end
if data.has_key?('autor')
_post.autor = data['autor']
end
_post.save
end
_post
end
delete '/posts/:id' do
Post.where(:id => params['id']).destroy_all
"item eliminado"
end