Commit d564902b5f4e408b063cdd42b5bb91168744e433
- Date: Thu Apr 10 17:34:24 +0000 2008
- Committer: Dustin Sallings (dustin@spy.net)
- Author: Dustin Sallings (dustin@spy.net)
- Commit SHA1: d564902b5f4e408b063cdd42b5bb91168744e433
- Tree SHA1: ae9506e2e11724a93620e70bb5c59fcb0927c8cc
Added an API to export info about a user.
Commit diff
| |   |
| 1 | class ApiController < ApplicationController |
| 2 | |
| 3 | def user |
| 4 | @user = User.find_by_login!(params[:user]) |
| 5 | @projects = @user.projects.find(:all, :include => [:tags, { :repositories => :project }]) |
| 6 | @repositories = @user.repositories.find(:all, :conditions => ["mainline = ?", false]) |
| 7 | end |
| 8 | |
| 9 | end |
| toggle raw diff |
--- /dev/null
+++ b/app/controllers/api_controller.rb
@@ -0,0 +1,9 @@
+class ApiController < ApplicationController
+
+ def user
+ @user = User.find_by_login!(params[:user])
+ @projects = @user.projects.find(:all, :include => [:tags, { :repositories => :project }])
+ @repositories = @user.repositories.find(:all, :conditions => ["mainline = ?", false])
+ end
+
+end |
| |   |
| 1 | module ApiHelper |
| 2 | end |
| toggle raw diff |
--- /dev/null
+++ b/app/helpers/api_helper.rb
@@ -0,0 +1,2 @@
+module ApiHelper
+end |
| |   |
| 1 | xml.instruct! :xml, :version => "1.0" |
| 2 | xml.user do |
| 3 | xml.name(@user.login) |
| 4 | xml.login(@user.login) |
| 5 | xml.email(@user.email) |
| 6 | xml.location('unknown') |
| 7 | xml.repositories do |
| 8 | @projects.each do |project| |
| 9 | xml.repository do |
| 10 | xml.name(project.slug) |
| 11 | xml.url(url_for(:controller => 'projects', :action => 'show', :id => project, :only_path => false)) |
| 12 | xml.description(project.description) |
| 13 | xml.homepage(project.home_url) |
| 14 | end |
| 15 | end |
| 16 | end |
| 17 | end |
| toggle raw diff |
--- /dev/null
+++ b/app/views/api/user.xml.builder
@@ -0,0 +1,17 @@
+xml.instruct! :xml, :version => "1.0"
+xml.user do
+ xml.name(@user.login)
+ xml.login(@user.login)
+ xml.email(@user.email)
+ xml.location('unknown')
+ xml.repositories do
+ @projects.each do |project|
+ xml.repository do
+ xml.name(project.slug)
+ xml.url(url_for(:controller => 'projects', :action => 'show', :id => project, :only_path => false))
+ xml.description(project.description)
+ xml.homepage(project.home_url)
+ end
+ end
+ end
+end
\ No newline at end of file |
| |   |
| 58 | 58 | end |
| 59 | 59 | |
| 60 | 60 | map.resource :search |
| 61 | |
| 62 | map.api '/api/:format/:action/:user', :controller => 'api', :requirements => { :id => /.+/ } |
| 61 | 63 | |
| 62 | 64 | map.with_options :controller => 'sessions' do |session| |
| 63 | 65 | session.login '/login', :action => 'new' |
| toggle raw diff |
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -58,6 +58,8 @@ ActionController::Routing::Routes.draw do |map|
end
map.resource :search
+
+ map.api '/api/:format/:action/:user', :controller => 'api', :requirements => { :id => /.+/ }
map.with_options :controller => 'sessions' do |session|
session.login '/login', :action => 'new' |
| |   |
| 1 | require File.dirname(__FILE__) + '/../spec_helper' |
| 2 | |
| 3 | describe ApiController do |
| 4 | |
| 5 | describe "users api" do |
| 6 | integrate_views |
| 7 | |
| 8 | it "should render useful user information" do |
| 9 | get :user, :format => 'xml', :user => 'johan' |
| 10 | response.body.should include(%Q{<login>johan</login>}) |
| 11 | end |
| 12 | |
| 13 | end |
| 14 | |
| 15 | end |
| toggle raw diff |
--- /dev/null
+++ b/spec/controllers/api_controller_spec.rb
@@ -0,0 +1,15 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe ApiController do
+
+ describe "users api" do
+ integrate_views
+
+ it "should render useful user information" do
+ get :user, :format => 'xml', :user => 'johan'
+ response.body.should include(%Q{<login>johan</login>})
+ end
+
+ end
+
+end |