diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 99e9cac..d9edd41 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -11,16 +11,25 @@ def create case data['type'] when 'Glucose' k['value'] = data['value'] - g = GlucoseMeasurement.new k + metric = GlucoseMeasurement.new k when 'Medication' k['dose'] = data['value'] k['insulin'] = data['subtype'].downcase - g = MedicationEvent.new k + metric = MedicationEvent.new k + + when 'Food' + k['weight'] = data['value'] + metric = CarbohydrateIntake.new k + + when 'Exercise' + k['duration'] = data['value'] + k['description'] = data['subtype'].downcase + metric = PhysicalExercise.new k end - if g.save - render json: g, status: :ok and return + if metric.save + render json: metric, status: :ok and return else render json: k, status: 400 and return end diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 069583b..b36390c 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -14,7 +14,7 @@ "id":"3648", "datetime":"2016-01-30T13:58:31+00:00", "type":"Glucose", - "subtype"=>{}, + "subtype":{}, "category":"Lunch", "value":"12.0" }.to_json @@ -25,8 +25,6 @@ end it 'fails with duff values' do - DatabaseCleaner.clean - expect { post :create, data: { @@ -38,8 +36,6 @@ end it 'creates a new meds event' do - DatabaseCleaner.clean - expect { post :create, data: { "id":"3649", @@ -47,12 +43,38 @@ "type":"Medication", "subtype":"Humalog", "category":"Lunch", - "value"=>"6.0" + "value":"6.0" }.to_json }.to change(MedicationEvent, :count).by 1 expect(MedicationEvent.first.insulin).to eq 'humalog' end + + it 'creates a new carbs intake' do + expect { + post :create, data: { + "id":"3650", + "datetime":"2016-01-29T19:01:46+00:00", + "type":"Food", + "subtype":{}, + "category":"Dinner", + "value":"70.0" + }.to_json + }.to change(CarbohydrateIntake, :count).by 1 + end + + it 'creates a new exercise' do + expect { + post :create, data: { + "id":"3651", + "datetime":"2016-01-20T19:02:13+00:00", + "type":"Exercise", + "subtype":"Drumming", + "category":"Dinner", + "value":"180.0" + }.to_json + }.to change(PhysicalExercise, :count).by 1 + end end end end