Playing with ActiveResource just a bit more

before trashing all of it.  What I really ought to be doing is playing
with Sequel or getting more cozy with ActiveRecord.
This commit is contained in:
Dan Buch 2012-05-29 16:41:47 -04:00
parent 32aaca146b
commit 2599a100da
4 changed files with 20 additions and 11 deletions

View File

@ -8,6 +8,14 @@ class Person < ActiveResource::Base
def family
Family.find(self.surname.downcase)
end
def family=(family_surname)
self.surname = family_surname
Family.find(family_surname)
rescue StandardError => e
STDERR.puts("#{e.class.name}:#{e.message}\n#{e.backtrace.join("\n")}")
Family.create(surname: family_surname.downcase)
end
end
class Family < ActiveResource::Base

5
ares/server/helpers.py Normal file
View File

@ -0,0 +1,5 @@
import json
def json_loads_from_request(request):
raw = request.body.s_iter.read(int(request.headers['Content-Length']))
return json.loads(raw)

View File

@ -1,5 +1,5 @@
import json
from server.families import Families
from server.helpers import json_loads_from_request
__families__ = Families()
@ -8,9 +8,7 @@ __families__ = Families()
if GET:
response.body = __families__.getall()
elif POST:
family = json.loads(
request.body.s_iter.read(int(request.headers['Content-Length']))
)['family']
family = json_loads_from_request(request)['family']
family_id = __families__.add(family)
response.headers['Location'] = '/families/{}.json'.format(family_id)
family['id'] = family_id

View File

@ -1,6 +1,5 @@
import json
from server.people import People
from aspen import Response
from server.helpers import json_loads_from_request
__people__ = People()
@ -9,12 +8,11 @@ __people__ = People()
if GET:
response.body = __people__.getall()
elif POST:
person = json.loads(
request.body.s_iter.read(int(request.headers['Content-Length']))
)['person']
person = json_loads_from_request(request)['person']
person_id = __people__.add(person)
response.headers['Location'] = '/people/{}.json'.format(person_id)
response.body = {}
raise Response(201)
person['id'] = person_id
response.body = person
response.code = 201
# vim:filetype=python