Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add API for likes, stars, hearts, boobs (#23)

  • Loading branch information...
commit e52b2bfed66c70c1887bad6ff4e62096e4d4cca7 1 parent 6a84b3c
@holman holman authored
View
14 db/migrate/03_create_stars.rb
@@ -0,0 +1,14 @@
+class CreateStars < ActiveRecord::Migration
+ def self.up
+ create_table :stars do |t|
+ t.belongs_to :user
+ t.belongs_to :song
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :stars
+ end
+end
View
1  lib/play.rb
@@ -22,6 +22,7 @@
require 'play/history'
require 'play/library'
require 'play/office'
+require 'play/star'
require 'play/song'
require 'play/views/layout'
require 'play/user'
View
18 lib/play/app/api.rb
@@ -28,6 +28,24 @@ class App < Sinatra::Base
end
end
+ post "/api/star_now_playing" do
+ api_authenticate
+ song = Play.now_playing
+ song.star!(api_user)
+ music_response(song)
+ end
+
+ post "/api/play_stars" do
+ api_authenticate
+ star = api_user.stars.sort_by{ rand }.first
+ if song = star.song
+ song.enqueue!(api_user)
+ music_response(song)
+ else
+ error "You don't have any starred songs, you lazy bastard."
+ end
+ end
+
post "/api/add_song" do
api_authenticate
artist = Artist.find_by_name(params[:artist_name])
View
10 lib/play/song.rb
@@ -4,6 +4,7 @@ class Song < ActiveRecord::Base
belongs_to :album
has_many :votes
has_many :histories
+ has_many :stars
scope :queue, select("songs.*,(select count(song_id) from votes where song_id=songs.id and active=1) as song_count").
where(:queued => true).
@@ -40,6 +41,15 @@ def current_votes
votes.where(:active => true).all
end
+ # Stars a song.
+ #
+ # user - The User object to associate with the new Star.
+ #
+ # Returns the new Star object.
+ def star!(user)
+ stars.create(:user => user)
+ end
+
# Queue up a song.
#
# user - the User who is requesting the song to be queued
View
6 lib/play/star.rb
@@ -0,0 +1,6 @@
+module Play
+ class Star < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :song
+ end
+end
View
1  lib/play/user.rb
@@ -1,6 +1,7 @@
module Play
class User < ActiveRecord::Base
has_many :votes
+ has_many :stars
# Let the user vote for a particular song.
#
View
14 test/api_test.rb
@@ -45,6 +45,20 @@ def app
assert !user[:error].include?("must supply a valid `user_login`")
end
+ test "/api/star_now_playing" do
+ Play.stubs(:now_playing).returns(@song)
+ post "/api/star_now_playing", { :user_login => @user.login }
+ song = parse_json(last_response.body.strip)
+ assert_equal @song.title, song[:song_title]
+ end
+
+ test "/api/play_stars" do
+ @song.star!(@user)
+ post "/api/play_stars", { :user_login => @user.login }
+ song = parse_json(last_response.body.strip)
+ assert_equal @song.title, song[:song_title]
+ end
+
test "/api/add_song" do
post "/api/add_song", { :song_title => @song.title,
:artist_name => @artist.name,
View
5 test/song_test.rb
@@ -27,6 +27,11 @@
assert_equal "Played 0 times", @song.to_playcount
end
+ test "stars a song" do
+ @song.star! @user
+ assert @user.stars.size == 1
+ end
+
test "enqueue queues it up" do
@song.enqueue! @user
assert @song.queued
Please sign in to comment.
Something went wrong with that request. Please try again.