-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
187 lines (141 loc) · 3.11 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require './song'
require './sinatra/auth'
require 'sinatra'
require 'slim'
require 'sass'
require 'sinatra/flash'
require 'pony'
require 'v8'
require 'coffee-script'
require 'sinatra/reloader' if development?
configure :development do
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
set :email_address => 'smtp.gmail.com',
:email_user_name => 'Mentessi',
:email_password => 'secret',
:email_domain => 'localhost.localdomain'
end
configure :production do
DataMapper.setup(:default, ENV['DATABASE_URL'])
set :email_address => 'smtp.sendgrid.net',
:email_user_name => ENV['SENDGRID_USERNAME'],
:email_password => ENV['SENDGRID_PASSWORD'],
:email_domain => 'heroku.com'
end
before do
set_title
end
helpers do
def css(*stylesheets)
stylesheets.map do |stylesheet|
"<link href=\"/#{stylesheet}.css\" media=\"screen, projection\"rel=\"stylesheet\" />"
end.join
end
def current?(path='/')
(request.path==path || request.path==path+'/') ? "current" :nil
end
def set_title
@title ||= "Songs by Sinatra"
end
end
get '/styles.css' do
scss :styles
end
get('/javascripts/application.js') do
coffee :application
end
get '/' do
slim :home
end
get '/about' do
@title = "all about this website"
slim :about
end
get '/contact' do
@title = "conact shizzle"
slim :contact
end
not_found do
slim :not_found
end
#Contact stuff----------
post '/contact' do
send_message
flash[:notice] = "Thank you for your message. We'll be in touch soon."
redirect to('/')
end
def send_message
Pony.mail(
:from => params[:name] + "<" + params[:email] + ">",
:to => '[email protected]',
:subject => params[:name] + " has contacted you",
:body => params[:message] + params[:email],
:port => '587',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'mentessi',
:password => 'secret',
:authentication => :plain,
:domain => 'localhost.localdomain'
})
end
#song methods -----------------------#
module SongHelpers
def find_songs
@songs = Song.all
end
def find_song
Song.get(params[:id])
end
def create_song
@song = Song.create(params[:song])
end
end
helpers SongHelpers
get '/songs' do
@songs = find_songs
slim :songs
end
get '/songs/new' do
protected!
@song = Song.new
slim :new_song
end
get '/songs/:id' do
@song = find_song
slim :show_song
end
post '/songs' do
flash[:notice] = "Song successfully added" if create_song
redirect to("/songs/#{@song.id}")
end
get '/songs/:id/edit' do
protected!
@song = find_song
slim :edit_song
end
put '/songs/:id' do
protected!
song = find_song
if song.update(params[:song])
flash[:notice] = "Song sucessfully updated!"
end
redirect to("/songs/#{song.id}")
end
delete '/songs/:id' do
protected!
if find_song.destroy
flash[:notice] = "Song deleted"
end
redirect to('/songs')
end
post '/songs/:id/like' do
@song = find_song
@song.likes = @song.likes.next
@song.save
redirect to"/songs/#{@song.id}" unless request.xhr?
slim :like, :layout => false
end