public
Description: Your favorite URL-shortening service in all of Ruby land
Homepage: http://rubyurl.com
Clone URL: git://github.com/robbyrussell/rubyurl.git
Adding support for JSON in the API
Robby Russell (author)
Mon Sep 01 18:10:23 -0700 2008
commit  36bcd0716a0a86fdaaf352d7760653b886877f2e
tree    c9b038c24cd0d744e788436b429e03880637e0bb
parent  e7554251c7b4e0ab4b235c064d086aff92a047c8
...
1
2
3
4
5
6
7
8
9
10
11
 
 
 
 
 
 
 
12
 
 
 
 
 
13
14
15
...
1
2
 
 
 
 
 
 
 
 
 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0
@@ -1,15 +1,18 @@
0
 class Api::LinksController < Api::BaseController
0
   def create
0
-    respond_to do |format|
0
-      format.xml do
0
-        @link = Link.find_or_create_by_website_url( params[:link][:website_url] )
0
-        @link.ip_address = request.remote_ip if @link.new_record?      
0
-        if @link.save
0
-          render :xml => @link.to_api_xml
0
-        else
0
-          render :xml => xml_error_response( "Unable to generate a RubyURL for you" )
0
-        end
0
+    @link = Link.find_or_create_by_website_url( params[:link][:website_url] )
0
+    @link.ip_address = request.remote_ip if @link.new_record?      
0
+
0
+    if @link.save        
0
+      respond_to do |format|
0
+        format.xml { render :xml => @link.to_api_xml }
0
+        format.json { render :json => @link.to_api_json }        
0
       end
0
+    else
0
+      respond_to do |format|
0
+        format.xml { render :xml => xml_error_response( "Unable to generate a RubyURL for you" ) }
0
+        format.json { render :json => "Unable to generate a RubyURL for you".to_json }
0
+      end  
0
     end
0
   end
0
 end
...
29
30
31
 
 
 
 
32
33
34
...
29
30
31
32
33
34
35
36
37
38
0
@@ -29,6 +29,10 @@ class Link < ActiveRecord::Base
0
     end
0
   end
0
   
0
+  def to_api_json
0
+    self.to_json( :only => [ :website_url, :permalink ] )
0
+  end
0
+  
0
   private
0
   
0
     def generate_token

Comments