initial public api'
This commit is contained in:
parent
2d366bcf71
commit
ea3d839e1c
5 changed files with 165 additions and 1 deletions
61
app/controllers/apis_controller.rb
Normal file
61
app/controllers/apis_controller.rb
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
class ApisController < ActionController::Metal
|
||||
include ActionController::Rendering
|
||||
include ActionController::Renderers::All
|
||||
|
||||
## posts
|
||||
def posts_index
|
||||
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
|
||||
end
|
||||
|
||||
def posts
|
||||
sm = StatusMessage.where(:guid => params[:guid], :public => true).includes(:photos, :author => :profile).first
|
||||
if sm
|
||||
render :json => sm.to_json
|
||||
else
|
||||
render(:nothing => true, :status => 404)
|
||||
end
|
||||
end
|
||||
|
||||
#tags
|
||||
#
|
||||
def tag_posts
|
||||
set_defaults
|
||||
posts = StatusMessage.where(:public => true, :pending => false)
|
||||
posts = posts.tagged_with(params[:tag])
|
||||
posts = posts.includes(:comments, :photos).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
|
||||
render :json => posts.as_json
|
||||
end
|
||||
|
||||
def tag_people
|
||||
set_defaults
|
||||
profiles = Profile.tagged_with(params[:tag]).where(:searchable => true).select('profiles.id, profiles.person_id')
|
||||
people = Person.where(:id => profiles.map{|p| p.person_id}).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
|
||||
render :json => people.as_json
|
||||
end
|
||||
|
||||
##people
|
||||
def people_index
|
||||
set_defaults
|
||||
people = Person.search(params[:q]).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
|
||||
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[:order] ||= 'created_at'
|
||||
params[:page] ||= 1
|
||||
end
|
||||
end
|
||||
|
|
@ -180,7 +180,7 @@ class Person < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def as_json(opts={})
|
||||
{:id => self.guid, :name => self.name, :avatar => self.profile.image_url(:thumb_small), :handle => self.diaspora_handle, :url => "/people/#{self.id}"}
|
||||
super(:include => [:profile], :except => [:mongo_id, :owner_id, :serialized_public_key])
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
|||
|
|
@ -96,6 +96,17 @@ class StatusMessage < Post
|
|||
identifiers.empty? ? [] : Person.where(:diaspora_handle => identifiers)
|
||||
end
|
||||
|
||||
def as_json(opts={})
|
||||
{:guid => self.guid,
|
||||
:id => self.id,
|
||||
:author => self.author.as_json,
|
||||
:photos => self.photos.as_json,
|
||||
:created_at => self.created_at,
|
||||
:updated_at => self.updated_at,
|
||||
:raw_message => self.raw_message,
|
||||
:text => self.text, }
|
||||
end
|
||||
|
||||
def to_activity
|
||||
<<-XML
|
||||
<entry>
|
||||
|
|
|
|||
|
|
@ -82,6 +82,15 @@ 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'
|
||||
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
|
||||
|
||||
match'localize', :to => "localize#show"
|
||||
match 'mobile/toggle', :to => 'home#toggle_mobile', :as => 'toggle_mobile'
|
||||
#root
|
||||
|
|
|
|||
83
spec/controllers/apis_controller_spec.rb
Normal file
83
spec/controllers/apis_controller_spec.rb
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
require 'spec_helper'
|
||||
|
||||
class ApisController
|
||||
include ActionController::UrlFor
|
||||
include ActionController::Testing
|
||||
include Rails.application.routes.url_helpers
|
||||
include ActionController::Compatibility
|
||||
end
|
||||
|
||||
describe ApisController do
|
||||
before :all do
|
||||
@status_message1 = Factory(:status_message, :text => '#bobby #flay #sux', :public => true)
|
||||
|
||||
@status_message2 = Factory(:status_message, :public => true)
|
||||
@status_message3 = Factory(:status_message)
|
||||
@person = Factory(:person, :profile => Factory.build(:profile,:first_name => 'bobby', :searchable => true, :tag_string => '#zord'))
|
||||
@person.profile.save
|
||||
end
|
||||
|
||||
describe '#posts' do
|
||||
|
||||
it 'returns all of the public posts' do
|
||||
get :posts_index, :format => :json
|
||||
@posts = JSON.parse(response.body)
|
||||
@posts.count.should == 2
|
||||
end
|
||||
|
||||
it 'accepts an order paramater' do
|
||||
pending
|
||||
end
|
||||
|
||||
it 'accpets a page paramater' do
|
||||
pending
|
||||
end
|
||||
|
||||
it 'accepts a per_page param' do
|
||||
get :posts_index, :format => :json, :per_page=> 1
|
||||
JSON.parse(response.body).count.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
describe '#post' do
|
||||
it 'returns a post' do
|
||||
get :posts, :guid => @status_message1.guid, :format => :json
|
||||
p = JSON.parse(response.body)
|
||||
p['guid'].should == @status_message1.guid
|
||||
end
|
||||
|
||||
it 'returns a 404 if does not exsist' do
|
||||
get :posts, :guid => 999
|
||||
response.code.should == '404'
|
||||
end
|
||||
end
|
||||
|
||||
describe '#tag_posts' do
|
||||
it 'succeeds' do
|
||||
get :tag_posts, :tag => 'flay'
|
||||
p = JSON.parse(response.body).first
|
||||
p['guid'].should == @status_message1.guid
|
||||
end
|
||||
end
|
||||
|
||||
describe '#tag_people' do
|
||||
it 'succeeds' do
|
||||
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_be 1
|
||||
p.first['person']['id'].should == @person.id
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe '#people' do
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue