-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicsme.rb
executable file
·142 lines (111 loc) · 2.39 KB
/
dicsme.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# dicsme.rb
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-validations'
require 'dm-migrations/adapters/dm-sqlite-adapter'
require "dm-migrations"
require "dm-serializer"
#
### CONFIGURATION
configure :development do
DataMapper.setup(:default, 'sqlite3:db/dicsme.db')
set :logging, true
set :raise_errors, true
DataMapper::Logger.new(STDOUT, :debug)
end
configure :production do
DataMapper.setup(:default, ENV['DATABASE_URL'])
set :raise_errors, true
end
#
# #
# ### MODELS
# #
#
class LP
include DataMapper::Resource
property :id, Serial
property :UID, String, :required => true, :key => true, :unique => true
property :identifier, String, :required => true, :key => true, :unique => true
property :created_at, DateTime
property :updated_at, DateTime
end
class Player
include DataMapper::Resource
property :id, Serial
property :playing, String
property :updated_at, DateTime
end
DataMapper.finalize
#
# # Actions for the Daemon side
#
# Apparition du disc
get '/app/:uid' do
lp = LP.first( :UID => params[:uid])
if !lp.nil?
play(lp.id)
end
end
# Disparition du disc
get '/dis/:uid' do
lp = LP.first( :UID => params[:uid])
if !lp.nil?
stop(id)
end
end
get '/' do
"<h1>Dicsme</h1><p>/playing - currenty playing song</p><p>/list - list of tracks in db</p><p>/track/:id - track infos</p><p>POST /add - add track [:uid,:id]</p><p>delete /:id - delete track</p>"
end
#
# # Actions for XBMC
#
get '/playing' do
LP.all( :UID => Player.first.playing ).to_json( :only => [:UID, :identifier])
end
get '/list' do
LP.all.to_json( :only => [:UID, :identifier])
end
get '/track/:id' do
LP.all( :identifier => params[:id] ).to_json( :only => [:UID, :identifier])
end
post '/add' do
lp = LP.new(
:UID => params[:uid],
:identifier => params[:id],
:created_at => Time.now,
:updated_at => Time.now
)
if lp.save
"[{ 'success' : 'Album saved' }]"
else
lp.errors
end
end
delete '/:id' do
lp = LP.first( :identifier => params[:id] )
if !lp.nil?
lp.destroy
"[{ 'success' : 'Album deleted' }]"
else
"[{ 'error' : 'Paire Doesn\'t exist' }]"
end
end
#
# # misc functions
#
def play(id)
pl = Player.first
pl.playing = id
pl.updated_at = Time.now
pl.save
end
def stop(id)
pl = Player.first
pl.playing = ""
pl.updated_at = Time.now
pl.save
end
def toAdd(uid)
end