Permalink
Browse files

Adds proper API for adding and removing likes

  • Loading branch information...
vesan committed Apr 1, 2013
1 parent 455b160 commit 44e17839b9b4ac0c580189f99bcf6630657d63f9
Showing with 83 additions and 2 deletions.
  1. +8 −0 lib/kippt/clip.rb
  2. +48 −0 lib/kippt/like.rb
  3. +7 −2 lib/kippt/likes.rb
  4. +20 −0 spec/kippt/clip_spec.rb
View
@@ -78,4 +78,12 @@ def saves_count
def saves_data
attributes.saves["data"]
end
+
+ def like
+ Kippt::Like.new(client, self).save
+ end
+
+ def unlike
+ Kippt::Like.new(client, self).destroy
+ end
end
View
@@ -0,0 +1,48 @@
+require "kippt/users"
+
+class Kippt::Like
+ def initialize(client, clip)
+ @client = client
+ @errors = []
+ @clip = clip
+ end
+
+ def id
+ nil
+ end
+
+ def collection_resource_class
+ Kippt::Likes
+ end
+
+ def writable_attributes_hash
+ nil
+ end
+
+ def save
+ @errors = []
+ response = collection_resource.save_resource(self)
+ if response[:error_message]
+ errors << response[:error_message]
+ end
+ response[:success]
+ end
+
+ def destroy
+ collection_resource.destroy_resource(self)
+ end
+
+ private
+
+ def collection_resource
+ @collection_resource ||= client.collection_resource_for(collection_resource_class, clip)
+ end
+
+ def client
+ @client
+ end
+
+ def clip
+ @clip
+ end
+end
View
@@ -1,5 +1,6 @@
require "kippt/user_collection"
require "kippt/user"
+require "kippt/like"
class Kippt::Likes
include Kippt::CollectionResource
@@ -37,7 +38,11 @@ def all(options = {})
end
end
- def build(attributes = {})
- object_class.new(attributes, client, clip)
+ def build
+ Kippt::Like.new(client, clip)
+ end
+
+ def destroy_resource(resource)
+ client.delete(base_uri).success?
end
end
View
@@ -84,4 +84,24 @@
end
end
end
+
+ describe "#like" do
+ let(:like) { stub :like }
+
+ it "instantiates a Kippt::Like and saves it" do
+ Kippt::Like.should_receive(:new).with(client, subject).and_return(like)
+ like.should_receive(:save)
+ subject.like
+ end
+ end
+
+ describe "#unlike" do
+ let(:like) { stub :like }
+
+ it "instantiates a Kippt::Like and destroys it" do
+ Kippt::Like.should_receive(:new).with(client, subject).and_return(like)
+ like.should_receive(:destroy)
+ subject.unlike
+ end
+ end
end

0 comments on commit 44e1783

Please sign in to comment.