statuses/ and users/ GET routes

This commit is contained in:
danielgrippi 2011-03-23 14:38:43 -07:00
parent 2dca7b4807
commit 517f3fd802
5 changed files with 140 additions and 55 deletions

View file

@ -1,25 +1,74 @@
class ApisController < ActionController::Metal
include ActionController::Rendering
include ActionController::Renderers::All
class ApisController < ApplicationController #ActionController::Metal
#include ActionController::Rendering
#include ActionController::Renderers::All
respond_to :json
## posts
def posts_index
#posts
def public_timeline
set_defaults
sm = StatusMessage.where(:public => true).includes(:photos, :author => :profile).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
render :json => sm.to_json
timeline = StatusMessage.where(:public => true).includes(:photos, :author => :profile).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
respond_with timeline do |format|
format.json{ render :json => timeline.to_json(:format => :twitter) }
end
end
def posts
sm = StatusMessage.where(:guid => params[:guid], :public => true).includes(:photos, :author => :profile).first
if sm
render :json => sm.to_json
def user_timeline
set_defaults
if params[:user_id]
if person = Person.find(params[:user_id])
timeline = person.posts.where(:type => "StatusMessage", :public => true).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
end
end
respond_with timeline
end
def statuses
status = StatusMessage.where(:guid => params[:guid], :public => true).includes(:photos, :author => :profile).first
if status
respond_with status do |format|
format.json{ render :json => status.to_json(:format => :twitter) }
end
else
render(:nothing => true, :status => 404)
end
end
#people
def users
if params[:user_id]
person = Person.where(:id => params[:user_id]).first
elsif params[:screen_name]
person = Person.where(:diaspora_handle => params[:screen_name]).first
end
if person
respond_with person do |format|
format.json{ render :json => person.to_json(:format => :twitter) }
end
else
render(:nothing => true, :status => 404)
end
end
def users_search
set_defaults
if params[:q]
people = Person.public_search(params[:q]).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
end
if people
respond_with people do |format|
format.json{ render :json => people.to_json(:format => :twitter) }
end
else
render(:nothing => true, :status => 404)
end
end
#tags
#
def tag_posts
set_defaults
posts = StatusMessage.where(:public => true, :pending => false)
@ -35,26 +84,9 @@ class ApisController < ActionController::Metal
render :json => people.as_json
end
##people
def people_index
set_defaults
people = Person.public_search(params[:q]).paginate(:page => params[:page], :per_page => params[:per_page], :order => "profiles.last_name ASC, profiles.first_name ASC")
render :json => people.as_json
end
def people
person = Person.where(:diaspora_handle => params[:diaspora_handle]).first
if person
render :json => person.as_json
else
render(:nothing => true, :status => 404)
end
end
protected
def set_defaults
params[:per_page] ||= 15
params[:per_page] ||= 20
params[:order] ||= 'created_at'
params[:page] ||= 1
end

View file

@ -186,7 +186,21 @@ class Person < ActiveRecord::Base
end
def as_json(opts={})
super(:include => [:profile], :except => [:mongo_id, :owner_id, :serialized_public_key])
opts ||= {}
if(opts[:format] == :twitter)
{
:id => self.id,
:screen_name => self.diaspora_handle,
:name => self.name,
:created_at => self.created_at,
:profile_image_url => self.profile.image_url(:thumb_small)
}
else
super(:include => [:profile], :except => [:mongo_id, :owner_id, :serialized_public_key])
end
end
def to_twitter(format=:json)
end
protected

View file

@ -122,6 +122,26 @@ class StatusMessage < Post
XML
end
def as_json(opts={})
opts ||= {}
if(opts[:format] == :twitter)
{
:id => self.guid,
:text => self.formatted_message(:plain_text => true),
:entities => {
:urls => [],
:hashtags => self.tag_list,
:user_mentions => self.mentioned_people.map{|p| p.diaspora_handle},
},
:source => 'diaspora',
:created_at => self.created_at,
:user => self.author.to_json(opts)
}
else
super(opts)
end
end
protected
def message_or_photos_present?

View file

@ -82,12 +82,16 @@ Diaspora::Application.routes.draw do
match 'receive/users/:guid', :to => 'publics#receive'
match 'hub', :to => 'publics#hub'
scope '/v-1' do
match '/posts', :to => 'apis#posts_index'
match '/posts/:guid', :to => 'apis#posts'
scope '/api' do
match '/statuses/public_timeline', :to => 'apis#public_timeline'
match '/statuses/show/:guid', :to => 'apis#statuses'
match '/statuses/user_timeline', :to => 'apis#user_timeline'
match '/users/show', :to => 'apis#users'
match '/users/search', :to => 'apis#users_search'
match '/tags_posts/:tag', :to => 'apis#tag_posts'
match '/tags_people/:tag', :to => 'apis#tag_people'
match '/people', :to => 'apis#people_index'
match '/person/:diaspora_handle', :to => 'apis#people'
end

View file

@ -17,10 +17,10 @@ describe ApisController do
@person.profile.save
end
describe '#posts' do
describe '#public_timeline' do
it 'returns all of the public posts' do
get :posts_index, :format => :json
get :public_timeline, :format => :json
@posts = JSON.parse(response.body)
@posts.count.should == 2
end
@ -34,50 +34,65 @@ describe ApisController do
end
it 'accepts a per_page param' do
get :posts_index, :format => :json, :per_page=> 1
get :public_timeline, :format => :json, :per_page=> 1
JSON.parse(response.body).count.should == 1
end
end
describe '#post' do
describe '#statuses' do
it 'returns a post' do
get :posts, :guid => @status_message1.guid, :format => :json
get :statuses, :guid => @status_message1.guid, :format => :json
p = JSON.parse(response.body)
p['guid'].should == @status_message1.guid
p['id'].should == @status_message1.guid
p['text'].should == @status_message1.formatted_message(:plain_text => true)
p['entities'].class.should == Hash
p['source'].should == 'diaspora'
p['user'].should == @status_message1.author.to_json(:format => :twitter)
p['created_at'].should_not be_nil
end
it 'returns a 404 if does not exsist' do
get :posts, :guid => 999
get :statuses, :guid => 999
response.code.should == '404'
end
end
describe '#users' do
it 'succeeds' do
get :users, :user_id => @person.id, :format => :json
p = JSON.parse(response.body)
p['id'].should == @person.id
p['name'].should == @person.name
p['screen_name'].should == @person.diaspora_handle
p['profile_image_url'].should == @person.profile.image_url(:thumb_small)
p['created_at'].should_not be_nil
end
end
describe '#users_search' do
it 'searches' do
get :users_search, :q => @person.name, :format => :json
p = JSON.parse(response.body)
response.code.should == '200'
end
end
describe '#tag_posts' do
it 'succeeds' do
pending
get :tag_posts, :tag => 'flay'
p = JSON.parse(response.body).first
p['guid'].should == @status_message1.guid
p['id'].should == @status_message1.guid
end
end
describe '#tag_people' do
it 'succeeds' do
pending
get :tag_people, :tag => 'zord'
p = JSON.parse(response.body).first
p['person']['id'].should == @person.id
end
end
describe '#people_index' do
it 'succeeds' do
get :people_index, :q => 'bobby'
p = JSON.parse(response.body)
p.count.should == 1
p.first['person']['id'].should == @person.id
end
end
describe '#people' do
end
end