-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_side.rb
More file actions
74 lines (61 loc) · 1.77 KB
/
server_side.rb
File metadata and controls
74 lines (61 loc) · 1.77 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
66
67
68
69
70
71
72
73
74
require 'rack'
require 'grooveshark'
class GrooveServer
@song_queue = []
def call env
request = Rack::Request.new env
case request.request_method
when 'POST'
# Given a search to input into the grooveshark client
puts request.params
search_term = request.params['search_term']
searched_songs = @client.search_songs(search_term)
# Need to determine how many songs to send back here
# For now I will just send the first song back to the client
[200, {"Content-Type" => "text/json"}, searched_songs.first.to_json]
when 'PUT'
# Not sure how this will work yet, but potentially change song index here
when 'GET'
# Send out the current song list
[200, {"Content-Type" => "text/plain"}, nil]
end
end
def init_client
@client = Grooveshark::Client.new
# This session can be stored for 7 days if desired
@session = @client.session
end
def change_queue_to_params params
end
def get_song_from_search song_info
song_potentials = @client.search_songs song_info
song = song_potentials.first
return song
end
def get_correct_song song
return get_song_from_search(song.title + " " + song.artist)
end
def insert_song_to_queue song
if client.get_song_url(song).is_nil?
song = get_correct_song song
end
@song_queue << song
end
def move_song_to_index song new_index
# Check if the song is currently in the song queue first
current_index = @song_queue.index(song)
if not current_index.is_nil?
@song_queue.delete_at(current_index)
end
@song_queue.insert(new_index, song)
end
def parse_songs_to_json
# Take the @song_queue and turn it into a json object that the
# view will understand with [{title:x, artist:y, album:z, url:w}, ...]
end
end
map '/groove_server' do
groove = GrooveServer.new
groove.init_client
run groove
end