Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 5 commits
  • 17 files changed
  • 0 commit comments
  • 1 contributor
View
11 TODO.md
@@ -1,15 +1,18 @@
* [DONE] Adding support for unauthenticated requests
* Better text representation in IRB
+* Timestamps as time
# Clip
* [DONE] Getting the feed of authenticated user
* [DONE] Getting the user of the clip
* Support for including list & via data
-* Getting the list of the clip
-* 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 list of the clip
+* [DONE] Getting the original clip the clip was created via
+* [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
View
4 lib/kippt/client.rb
@@ -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)
View
22 lib/kippt/clip.rb
@@ -1,7 +1,7 @@
-require "ostruct"
require "kippt/resource"
require "kippt/user"
require "kippt/list"
+require "kippt/comments"
class Kippt::Clip
include Kippt::Resource
@@ -9,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
+ :user => "Kippt::User", :via => "Kippt::Clip"
writable_attributes :is_favorite, :title, :url, :notes, :list
@@ -19,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
@@ -32,4 +32,20 @@ def list_uri
list.resource_uri
end
end
+
+ def comments
+ Kippt::Comments.new(client, self)
+ end
+
+ def all_comments_embedded?
+ comments_count == comments_data.size
+ end
+
+ def comments_count
+ attributes.comments["count"]
+ end
+
+ def comments_data
+ attributes.comments["data"]
+ end
end
View
6 lib/kippt/collection.rb
@@ -4,9 +4,9 @@ module Kippt::Collection
attr_reader :total_count, :limit, :offset
def initialize(data, client = nil)
- meta = data.fetch("meta")
- @limit = meta.fetch("limit")
- @offset = meta.fetch("offset")
+ meta = data.fetch("meta") { {} }
+ @limit = meta.fetch("limit") { nil }
+ @offset = meta.fetch("offset") { nil }
@next = meta.fetch("next") { nil }
@previous = meta.fetch("previous") { nil }
@total_count = meta.fetch("total_count") { nil }
View
25 lib/kippt/comment.rb
@@ -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
View
10 lib/kippt/comment_collection.rb
@@ -0,0 +1,10 @@
+require "kippt/collection"
+require "kippt/comment"
+
+class Kippt::CommentCollection
+ include Kippt::Collection
+
+ def object_class
+ Kippt::Comment
+ end
+end
View
44 lib/kippt/comments.rb
@@ -0,0 +1,44 @@
+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 all(options = {})
+ validate_collection_options(options)
+
+ if options.empty? && @clip.all_comments_embedded?
+ collection_class.new({"objects" => @clip.comments_data}, client)
+ else
+ collection_class.new(client.get(base_uri, options).body, client)
+ end
+ end
+
+ def build(attributes = {})
+ object_class.new(attributes, client, clip)
+ end
+end
View
2 lib/kippt/connection.rb
@@ -35,7 +35,7 @@ def delete(url, options = {})
def connection
@connection ||= Faraday.new("https://#{HOST}/api") do |builder|
builder.use Kippt::Connection::ParseMultiJson
- # builder.use Faraday::Response::Logger
+ builder.use Faraday::Response::Logger if ENV['DEBUG']
builder.adapter Faraday.default_adapter
end
end
View
1 lib/kippt/list.rb
@@ -1,4 +1,3 @@
-require "ostruct"
require "kippt/user"
class Kippt::List
View
20 lib/kippt/resource.rb
@@ -1,3 +1,5 @@
+require "ostruct"
+
module Kippt::Resource
def self.included(base)
base.instance_eval do
@@ -28,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)
@@ -64,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
@@ -77,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)
@@ -127,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
View
2 lib/kippt/user.rb
@@ -1,4 +1,4 @@
-require "ostruct"
+require "kippt/users"
class Kippt::User
include Kippt::Resource
View
1 spec/fixtures/comment.json
@@ -0,0 +1 @@
+{"body": "Test comment!", "created": 1364752365, "id": 10, "resource_uri": "/api/clips/10400202/comments/10/", "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/"}}
View
1 spec/fixtures/comments.json
@@ -0,0 +1 @@
+{ "meta": { "next": null, "total_count": 1, "previous": null, "limit": 20, "offset": 0 }, "objects": [ {"body": "Test comment!", "created": 1364752365, "id": 10, "resource_uri": "/api/clips/10400202/comments/10/", "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/"}}]}
View
32 spec/kippt/clip_spec.rb
@@ -52,4 +52,36 @@
end
end
end
+
+ describe "#comments" do
+ it "returns Kippt::Comments" do
+ subject.comments.should be_a Kippt::Comments
+ end
+
+ it "returns object where clip is set" do
+ subject.comments.clip.should eq subject
+ end
+ end
+
+ describe "#all_comments_embedded?" do
+ context "when comment count and number of comment objects matches" do
+ let(:data) { {"comments" => {
+ "count" => 2, "data" => [{}, {}]
+ }} }
+
+ it "returns true" do
+ subject.all_comments_embedded?.should be_true
+ end
+ end
+
+ context "when comment count and number of comment objects doesn't match" do
+ let(:data) { {"comments" => {
+ "count" => 2, "data" => [{}]
+ }} }
+
+ it "returns true" do
+ subject.all_comments_embedded?.should be_false
+ end
+ end
+ end
end
View
22 spec/kippt/comment_spec.rb
@@ -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
View
103 spec/kippt/comments_spec.rb
@@ -0,0 +1,103 @@
+require "spec_helper"
+require "kippt/comments"
+
+describe Kippt::Comments do
+ let(:client) { Kippt::Client.new(valid_user_credentials) }
+ let(:clip) { stub :clip, id: 100, all_comments_embedded?: false }
+ subject { Kippt::Comments.new(client, clip) }
+ let(:base_uri) { "clips/#{clip.id}/comments" }
+ let(:singular_fixture) { "comment" }
+ let(:collection_class) { Kippt::CommentCollection }
+ let(:resource_class) { Kippt::Comment }
+
+ def collection_fixture
+ base_uri.split("/").last
+ end
+
+ describe "#all" do
+ it "returns collection class" do
+ stub_get("/#{base_uri}").
+ to_return(:status => 200, :body => fixture("#{collection_fixture}.json"))
+ all_resources = subject.all
+ all_resources.is_a? collection_class
+ end
+
+ it "accepts limit and offset options" do
+ stub_get("/#{base_uri}?limit=10&offset=100").
+ to_return(:status => 200, :body => fixture("#{collection_fixture}.json"))
+ resources = subject.all(:limit => 10, :offset => 100)
+ end
+
+ context "when passed unrecognized arguments" do
+ it "raises error" do
+ lambda {
+ subject.all(:foobar => true)
+ }.should raise_error(
+ ArgumentError, "Unrecognized argument: foobar")
+ end
+ end
+ end
+
+ describe "#[]" do
+ it "fetches single resource" do
+ stub_get("/#{base_uri}/10").
+ to_return(:status => 200, :body => fixture("#{singular_fixture}.json"))
+ subject[10].id.should eq 10
+ end
+
+ it "returns resource" do
+ stub_get("/#{base_uri}/10").
+ to_return(:status => 200, :body => fixture("#{singular_fixture}.json"))
+ subject[10].should be_a(resource_class)
+ end
+
+ context "when resource is not found" do
+ it "raises exception" do
+ stub_get("/#{base_uri}/10").
+ to_return(:status => 404, :body => {"message" => "Resource not found."})
+ lambda {
+ subject[10]
+ subject.all(:foobar => true)
+ }.should raise_error(
+ Kippt::APIError, "Resource could not be loaded: Resource not found.")
+ end
+ end
+ end
+
+ describe "#find" do
+ it "exists" do
+ subject.respond_to?(:find).should be_true
+ end
+ end
+
+ describe "#collection_from_url" do
+ it "returns a new collection" do
+ stub_get("/#{base_uri}/?limit=20&offset=20").
+ to_return(:status => 200, :body => fixture("#{collection_fixture}.json"))
+ collection = subject.collection_from_url("/api/#{base_uri}/?limit=20&offset=20")
+ collection.should be_a(collection_class)
+ end
+
+ context "when passed URL is blank" do
+ it "raises ArgumentError" do
+ lambda {
+ subject.collection_from_url("")
+ }.should raise_error(ArgumentError, "The parameter URL can't be blank")
+ lambda {
+ subject.collection_from_url(nil)
+ }.should raise_error(ArgumentError, "The parameter URL can't be blank")
+ end
+ end
+ end
+
+ describe "#build" do
+ it "returns new resource" do
+ subject.build.should be_a(resource_class)
+ end
+
+ it "accepts parameters" do
+ subject.object_class.should_receive(:new).with({:an => "attribute"}, client, clip)
+ subject.build(:an => "attribute")
+ end
+ end
+end
View
10 spec/spec_helper.rb
@@ -41,17 +41,21 @@ def json_fixture(file)
end
shared_examples_for "collection resource" do
+ def collection_fixture
+ base_uri.split("/").last
+ end
+
describe "#all" do
it "returns collection class" do
stub_get("/#{base_uri}").
- to_return(:status => 200, :body => fixture("#{base_uri}.json"))
+ to_return(:status => 200, :body => fixture("#{collection_fixture}.json"))
all_resources = subject.all
all_resources.is_a? collection_class
end
it "accepts limit and offset options" do
stub_get("/#{base_uri}?limit=10&offset=100").
- to_return(:status => 200, :body => fixture("#{base_uri}.json"))
+ to_return(:status => 200, :body => fixture("#{collection_fixture}.json"))
resources = subject.all(:limit => 10, :offset => 100)
end
@@ -100,7 +104,7 @@ def json_fixture(file)
describe "#collection_from_url" do
it "returns a new collection" do
stub_get("/#{base_uri}/?limit=20&offset=20").
- to_return(:status => 200, :body => fixture("#{base_uri}.json"))
+ to_return(:status => 200, :body => fixture("#{collection_fixture}.json"))
collection = subject.collection_from_url("/api/#{base_uri}/?limit=20&offset=20")
collection.should be_a(collection_class)
end

No commit comments for this range

Something went wrong with that request. Please try again.