Skip to content
Browse files

Added endpoint for stats, added presenter, and spec

  • Loading branch information...
1 parent be0276f commit 507c99cf11ff190dddc7e2e8e7b915cc83646dd3 @mackshkatz committed Mar 30, 2012
View
5 app/controllers/api/v1/base_controller.rb
@@ -1,3 +1,6 @@
class Api::V1::BaseController < ActionController::Base
-
+ def stats
+ presenter = StatsPresenter.new
+ render :json => presenter.present and return
+ end
end
View
6 app/presenters/api/v1/comments_presenter.rb
@@ -1,4 +1,4 @@
-class CommentsPresenter < Api::V1::BasePresenter
+class Api::V1::CommentsPresenter < Api::V1::BasePresenter
attr_accessor :comments
def initialize(comments)
@@ -16,8 +16,4 @@ def present
end
comments_array
end
-
- def count
-
- end
end
View
6 app/presenters/api/v1/posts_presenter.rb
@@ -1,4 +1,4 @@
-class PostsPresenter < Api::V1::BasePresenter
+class Api::V1::PostsPresenter < Api::V1::BasePresenter
attr_accessor :posts
def initialize(posts)
@@ -16,8 +16,4 @@ def present
end
posts_array
end
-
- def count
-
- end
end
View
12 app/presenters/api/v1/stats_presenter.rb
@@ -0,0 +1,12 @@
+class Api::V1::StatsPresenter < Api::V1::BasePresenter
+ def present
+ {
+ counts: {
+ posts: Post.count,
+ authors: User.count,
+ tags: Tag.count,
+ comments: Comment.count
+ }
+ }
+ end
+end
View
1 config/routes.rb
@@ -21,6 +21,7 @@
resources :comments
end
get '/comments/unapproved' => 'comments#unapproved'
+ get '/stats' => 'base#stats'
resources :comments
end
end
View
32 spec/controllers/api/v1/base_controller_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe Api::V1::BaseController do
+ before do
+ @post = Post.create(:title => "Test Title", :body => "Test Body", :teaser => "Test Teaser", :user_id => "1")
+ @post2 = Post.create(:title => "Test Title2", :body => "Test Body2", :teaser => "Test Teaser2", :user_id => "2")
+ @post3 = Post.create(:title => "Test Title3", :body => "Test Body3", :teaser => "Test Teaser3", :user_id => "2")
+
+ @user = User.create(:first_name => "max", :last_name => "katz", :password => "hash", :password_confirmation => "hash")
+
+ 5.times do |i|
+ @tag = Tag.create(:name => "tagz#{i}")
+ end
+
+ @comment = @post.comments.create(:body => "Test comment body", :email => "test@test.com", :approved => true)
+ @comment2 = @post.comments.create(:body => "Test comment body2", :email => "test2@test.com")
+ @comment3 = @post.comments.create(:body => "Test comment body3", :email => "test3@test.com")
+ end
+
+ it "should count the number of each model" do
+ expected_json = {
+ counts: {
+ posts: 3,
+ authors: 1,
+ tags: 5,
+ comments: 3
+ }
+ }.to_json
+ get :stats
+ response.body.should == expected_json
+ end
+end

0 comments on commit 507c99c

Please sign in to comment.
Something went wrong with that request. Please try again.