Skip to content

Instantly share code, notes, and snippets.

@jwang
Created April 24, 2010 00:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwang/377353 to your computer and use it in GitHub Desktop.
Save jwang/377353 to your computer and use it in GitHub Desktop.
These are the necessary changes to add RESTful JSON (and in the process, fix RESTful XML) to the demo on Gist
These are the necessary changes to add RESTful JSON (and in the process, fix RESTful XML) to the demo on Gist: <a href="http://gist.github.com/268192">Rails 2.3.5 on App Engine (DataMapper)</a>
They're fairly straightforward, but there's a <a href="http://bit.ly/aOWE0G">write-up here</a>. And the source can be found on the <a href="http://github.com/freshblocks/iPhone-JSON">Github Repository</a>.
require 'appengine-rack'
require 'appengine-apis/urlfetch'
require 'cgi'
require 'json'
require 'dm-core'
require 'dm-serializer'
AppEngine::Rack.configure_app(
:application => 'iphone-json',
:precompilation_enabled => true,
:sessions_enabled => true,
:version => "1")
# original generated code
=begin
AppEngine::Rack.app.resource_files.exclude :rails_excludes
ENV['RAILS_ENV'] = AppEngine::Rack.environment
require ::File.expand_path('../config/environment', __FILE__)
use Rails::Rack::LogTailer if ENV['RAILS_ENV'].eql? 'development'
run ActionController::Dispatcher.new
=end
# code from jruby-depot.appspot.com
AppEngine::Rack.app.resource_files.exclude :rails_excludes
ENV['RAILS_ENV'] = AppEngine::Rack.environment
deferred_dispatcher = AppEngine::Rack::DeferredDispatcher.new(
:require => File.expand_path('../config/environment', __FILE__),
:dispatch => 'ActionController::Dispatcher', :isolate => true)
map '/admin' do
use AppEngine::Rack::AdminRequired
run deferred_dispatcher
end
map '/user' do
use AppEngine::Rack::LoginRequired
run deferred_dispatcher
end
map '/' do
run deferred_dispatcher
end
class ContactsController < ApplicationController
# GET /contacts
# GET /contacts.xml
# GET /contacts.json
def index
@contacts = Contact.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @contacts }
format.json { render :json => @contacts }
end
end
# GET /contacts/1
# GET /contacts/1.xml
# GET /contacts/1.json
def show
@contact = Contact.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @contact }
format.json { render :json => @contact }
end
end
# GET /contacts/new
# GET /contacts/new.xml
# GET /contacts/new.json
def new
@contact = Contact.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @contact }
format.json { render :json => @contact }
end
end
# GET /contacts/1/edit
def edit
@contact = Contact.find(params[:id])
end
# POST /contacts
# POST /contacts.xml
# POST /contacts.json
def create
@contact = Contact.new(params[:contact])
respond_to do |format|
if @contact.save
flash[:notice] = 'Contact was successfully created.'
format.html { redirect_to(@contact) }
format.xml { render :xml => @contact, :status => :created, :location => @contact }
format.json { render :json => @contact, :status => :created, :location => @contact }
else
format.html { render :action => "new" }
format.xml { render :xml => @contact.errors, :status => :unprocessable_entity }
format.json { render :json => @contact.errors, :status => :unprocessable_entity }
end
end
end
# PUT /contacts/1
# PUT /contacts/1.xml
# PUT /contacts/1.json
def update
@contact = Contact.find(params[:id])
respond_to do |format|
if @contact.update_attributes(params[:contact])
flash[:notice] = 'Contact was successfully updated.'
format.html { redirect_to(@contact) }
format.xml { head :ok }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @contact.errors, :status => :unprocessable_entity }
format.json { render :json => @contact.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /contacts/1
# DELETE /contacts/1.xml
def destroy
@contact = Contact.find(params[:id])
@contact.destroy
respond_to do |format|
format.html { redirect_to(contacts_url) }
format.xml { head :ok }
format.json { head :ok }
end
end
end
# Critical default settings:
disable_system_gems
disable_rubygems
bundle_path '.gems/bundler_gems'
# List gems to bundle here:
gem 'rails_dm_datastore'
gem 'rails', "2.3.5"
gem 'dm-core'
gem "json-jruby"
gem "dm-serializer"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment