Skip to content

Commit

Permalink
Adding support for JSON in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
robbyrussell committed Sep 2, 2008
1 parent e755425 commit 36bcd07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
21 changes: 12 additions & 9 deletions app/controllers/api/links_controller.rb
@@ -1,15 +1,18 @@
class Api::LinksController < Api::BaseController
def create
respond_to do |format|
format.xml do
@link = Link.find_or_create_by_website_url( params[:link][:website_url] )
@link.ip_address = request.remote_ip if @link.new_record?
if @link.save
render :xml => @link.to_api_xml
else
render :xml => xml_error_response( "Unable to generate a RubyURL for you" )
end
@link = Link.find_or_create_by_website_url( params[:link][:website_url] )
@link.ip_address = request.remote_ip if @link.new_record?

if @link.save
respond_to do |format|
format.xml { render :xml => @link.to_api_xml }
format.json { render :json => @link.to_api_json }
end
else
respond_to do |format|
format.xml { render :xml => xml_error_response( "Unable to generate a RubyURL for you" ) }
format.json { render :json => "Unable to generate a RubyURL for you".to_json }
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/link.rb
Expand Up @@ -29,6 +29,10 @@ def to_api_xml
end
end

def to_api_json
self.to_json( :only => [ :website_url, :permalink ] )
end

private

def generate_token
Expand Down

0 comments on commit 36bcd07

Please sign in to comment.