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 def family
Family.find(self.surname.downcase) Family.find(self.surname.downcase)
end 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 end
class Family < ActiveResource::Base 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.families import Families
from server.helpers import json_loads_from_request
__families__ = Families() __families__ = Families()
@ -8,9 +8,7 @@ __families__ = Families()
if GET: if GET:
response.body = __families__.getall() response.body = __families__.getall()
elif POST: elif POST:
family = json.loads( family = json_loads_from_request(request)['family']
request.body.s_iter.read(int(request.headers['Content-Length']))
)['family']
family_id = __families__.add(family) family_id = __families__.add(family)
response.headers['Location'] = '/families/{}.json'.format(family_id) response.headers['Location'] = '/families/{}.json'.format(family_id)
family['id'] = family_id family['id'] = family_id

View File

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