Skip to content

Commit

Permalink
can create zombies via the api
Browse files Browse the repository at this point in the history
  • Loading branch information
ecavazos committed Mar 25, 2012
1 parent 70b2658 commit 4f51e55
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 40 deletions.
79 changes: 60 additions & 19 deletions app/controllers/api/v1/zombies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,70 @@
class Api::V1::ZombiesController < Api::Controller
respond_to :json

# This is a very simple and early iteration of the super_zombie API.
# For the time being, all of the logic is here in the controller.
# Future iterations will see this code refactored out and into
# the model layer.
def index
data = Zombie.all.map do |zombie|
brain = zombie.brain
gut = zombie.gut

{
:id => zombie.id,
:name => zombie.name,
:gender => zombie.gender,
:age => zombie.age,
:brain => {
:id => brain.id,
:kind => brain.kind,
:size => brain.size
},
:gut => {
:id => gut.id,
:kind => gut.kind,
:species => gut.species
}
}
ZombiePresenter.new zombie
end

render :json => Yajl.dump(data), :content_type => 'application/json'
end

def create
zombie = Zombie.new \
:name => params[:name],
:age => params[:age],
:gender => params[:gender]

zombie.brain = Brain.find_or_initialize_by_id params[:brain_id]
zombie.gut = Gut.find_or_initialize_by_id params[:gut_id]

if zombie.brain.new_record?
zombie.brain.update_attributes \
:id => nil,
:kind => params[:brain_kind],
:size => params[:brain_size]
end

if zombie.gut.new_record?
zombie.gut.update_attributes \
:id => nil,
:kind => params[:brain_kind],
:species => params[:brain_size]
end

if zombie.save
json = Yajl.dump ZombiePresenter.new(zombie)
render :json => json, :content_type => 'application/json'
else
render :json => '', :content_type => 'application/json', :status => :bad_request
end
end

private

class ZombiePresenter
def initialize(zombie)
@zombie = zombie
end

def as_json(*)
brain = @zombie.brain
gut = @zombie.gut

hash = @zombie.attributes
[:brain_id, :gut_id, :updated_at].each { |s| hash.delete(s) }

hash[:brain] = brain.attributes
hash[:brain].delete :updated_at

hash[:gut] = gut.attributes
hash[:gut].delete :updated_at

hash
end
end
end
114 changes: 93 additions & 21 deletions spec/api/v1/zombies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,105 @@ def app
Rails.application
end

it 'should return all zombies' do
zombies = []
zombies << Zombie.gen(:name => 'Vash')
zombies << Zombie.gen(:name => 'Wolfwood')
zombies << Zombie.gen(:name => 'Meryl')
zombies << Zombie.gen(:name => 'Milly')
def should_be_zombie_json(json, zombie)
json['id'].should == zombie.id
json['name'].should == zombie.name
json['gender'].should == zombie.gender
json['age'].should == zombie.age
json['created_at'].should == zombie.created_at.as_json
json['brain']['id'].should == zombie.brain.id
json['brain']['kind'].should == zombie.brain.kind
json['brain']['size'].should == zombie.brain.size
json['brain']['created_at'].should == zombie.brain.created_at.as_json
json['gut']['id'].should == zombie.gut.id
json['gut']['kind'].should == zombie.gut.kind
json['gut']['species'].should == zombie.gut.species
json['gut']['created_at'].should == zombie.gut.created_at.as_json
end

context 'Index' do

it 'should return all zombies' do
Zombie.gen :name => 'Vash'
Zombie.gen :name => 'Wolfwood'
Zombie.gen :name => 'Meryl'
Zombie.gen :name => 'Milly'

get '/api/v1/zombies.json'

last_response.headers['Content-Type'].should == 'application/json'
last_response.status.should == 200

zombies = Zombie.all

Yajl.load(last_response.body).each_with_index do |response, i|
should_be_zombie_json response, zombies[i]
end
end
end

context 'Create' do

it 'should create a zombie and return the zombie as json' do
Zombie.count.should == 0

get '/api/v1/zombies.json'
post '/api/v1/zombies.json', {
:name => 'UUURRGGH',
:gender => 'Male',
:age => 22,
:brain_id => Brain.gen.id,
:gut_id => Gut.gen.id
}

last_response.headers['Content-Type'].should == 'application/json'
last_response.status.should == 200
Zombie.count.should == 1

last_response.headers['Content-Type'].should == 'application/json'
last_response.status.should == 200

zombie = Zombie.first
json = Yajl.load(last_response.body)

should_be_zombie_json json, zombie
end

it 'should create brain and gut when post data does not contain thier ids' do
Zombie.count.should == 0
Brain.count.should == 0
Gut.count.should == 0

post '/api/v1/zombies.json', {
:name => 'UUURRGGH',
:gender => 'Male',
:age => 22,
:brain_kind => 'Damaged',
:brain_size => 'Medium',
:gut_kind => 'Juicy',
:gut_species => 'Bear'
}

Zombie.count.should == 1
Brain.count.should == 1
Gut.count.should == 1

last_response.headers['Content-Type'].should == 'application/json'
last_response.status.should == 200

zombie = Zombie.first
json = Yajl.load(last_response.body)

should_be_zombie_json json, zombie
end

Yajl.load(last_response.body).each_with_index do |resp, i|
zombie = zombies[i]
it 'should respond with 400 when post data is not valid' do
Zombie.count.should == 0

resp['id' ].should == zombie.id
resp['name' ].should == zombie.name
resp['gender'].should == zombie.gender
resp['age' ].should == zombie.age
post '/api/v1/zombies.json', :name => 'UUURRGGH'

resp['brain']['id' ].should == zombie.brain.id
resp['brain']['kind'].should == zombie.brain.kind
resp['brain']['size'].should == zombie.brain.size
Zombie.count.should == 0

resp['gut']['id' ].should == zombie.gut.id
resp['gut']['kind' ].should == zombie.gut.kind
resp['gut']['species'].should == zombie.gut.species
last_response.headers['Content-Type'].should == 'application/json'
last_response.status.should == 400
last_response.body.should == ''
end
end
end
Expand Down

0 comments on commit 4f51e55

Please sign in to comment.