Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds simple getting of comments for a clip

It always loads the data from the server even when
the data is embedded in the clip.
  • Loading branch information...
commit b1552c443024d73b93e58f5ecee89c5b64c33f89 1 parent fd9d67a
Vesa Vänskä authored
7 TODO.md
View
@@ -1,5 +1,6 @@
* [DONE] Adding support for unauthenticated requests
* Better text representation in IRB
+* Timestamps as time
# Clip
@@ -8,8 +9,10 @@
* Support for including list & via data
* [DONE] Getting the list of the clip
* [DONE] Getting the original clip the clip was created via
-* Getting the comments, likes & saves of a clip
-* Getting and setting is favorited (How does is starred work with it?)
+* [DONE] Getting the comments of a clip
+* Getting the likes of a clip
+* Getting the saves of a clip
+* [DONE] Getting and setting is favorited (How does is starred work with it?)
* [DONE] Getting the media of the clip (experimental)
* [DONE] Getting the type of the clip
* [DONE] Getting the favicon url of the clip
4 lib/kippt/client.rb
View
@@ -40,8 +40,8 @@ def users
Kippt::Users.new(self)
end
- def collection_resource_for(resource_class)
- resource_class.new(self)
+ def collection_resource_for(resource_class, *options)
+ resource_class.new(*([self] + options))
end
def resource_from_url(resource_class, url)
9 lib/kippt/clip.rb
View
@@ -1,6 +1,7 @@
require "kippt/resource"
require "kippt/user"
require "kippt/list"
+require "kippt/comments"
class Kippt::Clip
include Kippt::Resource
@@ -8,7 +9,7 @@ class Kippt::Clip
attributes :url_domain, :updated, :is_starred, :title,
:url, :notes, :created, :id, :resource_uri,
:type, :favicon_url, :app_url, :media,
- :user => Kippt::User, :via => Kippt::Clip
+ :user => "Kippt::User", :via => "Kippt::Clip"
writable_attributes :is_favorite, :title, :url, :notes, :list
@@ -18,7 +19,7 @@ class Kippt::Clip
alias :is_starred= :is_favorite=
alias :starred? :favorite?
- embedded_attributes :list => Kippt::List
+ embedded_attributes :list => "Kippt::List"
def collection_resource_class
Kippt::Clips
@@ -31,4 +32,8 @@ def list_uri
list.resource_uri
end
end
+
+ def comments
+ Kippt::Comments.new(client, self)
+ end
end
25 lib/kippt/comment.rb
View
@@ -0,0 +1,25 @@
+require "kippt/resource"
+require "kippt/user"
+require "kippt/clip"
+
+class Kippt::Comment
+ include Kippt::Resource
+
+ attributes :body, :created, :id, :resource_uri,
+ :user => "Kippt::User"
+
+ writable_attributes :body
+
+ attr_reader :clip
+
+ def initialize(attributes = {}, client = nil, clip)
+ super(attributes, client)
+ @clip = clip
+ end
+
+ private
+
+ def collection_resource
+ @collection_resource ||= client.collection_resource_for(Kippt::Comments, clip)
+ end
+end
10 lib/kippt/comment_collection.rb
View
@@ -0,0 +1,10 @@
+require "kippt/collection"
+require "kippt/comment"
+
+class Kippt::CommentCollection
+ include Kippt::Collection
+
+ def object_class
+ Kippt::Comment
+ end
+end
34 lib/kippt/comments.rb
View
@@ -0,0 +1,34 @@
+require "kippt/comment_collection"
+require "kippt/comment"
+require "kippt/comment_collection"
+
+class Kippt::Comments
+ include Kippt::CollectionResource
+
+ attr_reader :clip
+
+ def initialize(client, clip)
+ @client = client
+ @clip = clip
+ end
+
+ def self.valid_filter_parameters
+ [:limit, :offset]
+ end
+
+ def object_class
+ Kippt::Comment
+ end
+
+ def collection_class
+ Kippt::CommentCollection
+ end
+
+ def base_uri
+ "clips/#{clip.id}/comments"
+ end
+
+ def build(attributes = {})
+ object_class.new(attributes, client, clip)
+ end
+end
18 lib/kippt/resource.rb
View
@@ -30,7 +30,7 @@ def attributes(*attribs)
mappings = hashes.reduce({}, :update)
mappings.each do |attrib, object_class|
define_method(attrib) do
- object_class.new(attributes.send(attrib))
+ _get_class(object_class).new(attributes.send(attrib))
end
end
@attribute_names += convert_to_symbols(mappings.keys)
@@ -66,9 +66,9 @@ def embedded_attributes(*attribs)
define_method(attrib) do
value = attributes.send(attrib)
if value.is_a? String
- client.resource_from_url(attribute_class, value)
+ client.resource_from_url(_get_class(attribute_class), value)
else
- attribute_class.new(value)
+ _get_class(attribute_class).new(value)
end
end
end
@@ -79,6 +79,7 @@ def embedded_attributes(*attribs)
def convert_to_symbols(list)
list.map {|item| item.to_sym }
end
+
end
def initialize(attributes = {}, client = nil)
@@ -129,4 +130,15 @@ def collection_resource
def client
@client
end
+
+ def _get_class(camel_cased_word)
+ names = camel_cased_word.split('::')
+ names.shift if names.empty? || names.first.empty?
+
+ constant = Object
+ names.each do |name|
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
+ end
+ constant
+ end
end
1  spec/fixtures/comment.json
View
@@ -0,0 +1 @@
+{"body": "Test comment!", "created": 1364752365, "id": 9735, "resource_uri": "/api/clips/10400202/comments/9735/", "user": {"username": "vesan", "bio": "Ruby developer at Kisko Labs. Dangerous, but not foolish.", "app_url": "/vesan", "avatar_url": "https://d19weqihs4yh5u.cloudfront.net/avatars/3b2958fd-db1d-4b80-acd1-d7e344fe5124/160x160", "bio": "Ruby developer at Kisko Labs. Dangerous, but not foolish.", "counts": {"follows": 21, "followed_by": 26}, "dribbble": "vesan", "full_name": "Vesa Vänskä", "github": "vesan", "id": 33, "is_pro": true, "resource_uri": "/api/users/33/", "twitter": "vesan", "username": "vesan", "website_url": "http://vesavanska.com/"}}
22 spec/kippt/comment_spec.rb
View
@@ -0,0 +1,22 @@
+require "spec_helper"
+require "kippt/comment"
+
+describe Kippt::Comment do
+ subject { Kippt::Comment.new(data, client, clip) }
+ let(:client) { Kippt::Client.new(valid_user_credentials) }
+ let(:clip) { stub(:clip) }
+ let(:collection_resource_class) { Kippt::Comments }
+
+ let(:data) { MultiJson.load(fixture("comment.json").read) }
+ let(:attributes) {
+ [:body, :created, :id, :resource_uri]
+ }
+
+ it_behaves_like "resource"
+
+ describe "#clip" do
+ it "returns the clip passed to it" do
+ subject.clip.should eq clip
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.