=begin
> I am barely familiar with both REST and Camping, and I'd like
> to explore some RESTful Camping as a way to learn both.
i still don't know exactly what REST is (maybe there is no exact definition),
part of it is mapping of methods to URIs for a HTTP RPC
camping makes playing with all this easy
some "REST" examples i see involve query strings, but these can become rather
ugly and unreadable/hackable
in camping you may do something like
=end
module Aero::Controllers
class Foil < R '/foil/([^/]+)/([^/]+)/([^/]+)'
def get geo, angle, mach
# do some math here and return results
end
end
=begin
the last line in the 'get' method is implicitly returned, it can be text, or a
call to a view method..
you would do something like GET /foil/1/90.000/3 in this case,
or perhaps query string is more your type:
GET /foil?mach=3&angle=37.000
=end
class Foil
def get
lift = input.mach * input.angle # etc
end
end
=begin
i think the ST of REST stands for "state transfer" - which is more accurate and
easy in JSON than query-string vars:
in JS+jQuery:
function rpc(o) {
$.ajax($.extend({
url : '/foil',
type : "post",
contentType: 'application/json',
dataType : 'json',
data:$.toJSON({mach: 3, forces: [0.00024, 1.33343, 6.777156]}),
processData: false,
beforeSend: function(xml) {
xml.setRequestHeader("Accept", "application/json");
}
},o))
}
in camping:
=end
class Foil
def post
@mach, @f = input.mach, input.forces
end
end
end
module Aero
def service *a
if @method == "post" and @env.CONTENT_TYPE == 'application/json'
@input = JSON.parse(@in.read)
end
super *a
end
end
=begin
and now you can share any object between ECMAscript and Ruby, as long as you
stick to floats, ints, strings, arrays, hashes..
what business does your REST server have generating HTML anwyays? its there
just to provide calculating service..
=end
module Aero::Controllers
class Foil
def post
@headers['Content-Type'] = @env[:HTTP_ACCEPT]
# do some heavy lifting
{:lift => lift, :drag => drag, :pitch => pitch}.to_json
end
end
end
=begin
because you set 'dataType' to 'json' in jQuery's AJAJ options, you get the
object passed to the success callback:
rpc({success: function(r){
$("#yaw").text(r.pitch * r.skew)
}})
this gives you an overview of 1 old fashioned (querystring) 1 camping-fashioned
(custom URIs defined via regex) and 1 neo-classical (JSON RPC via post) means
to REST while camping. i've skipped over POST and HTML forms. since its
identical to querystring except the string is in the POST body instead of
appended to the URL. ive also skipped custom methods (you could 'def foil' in
your controller if you only wanted it to respond to FOIL requests intead of GET
requests) which may be useful if you dont want your app to appear to work to
RFC compliant web browsers..
cheers! and happy camping.
=end
_______________________________________________
Camping-list mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/camping-list