Skip to content

Commit

Permalink
Import carbs and exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
pikesley committed Jan 30, 2016
1 parent 072e0d5 commit 06197e6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
17 changes: 13 additions & 4 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 28 additions & 6 deletions spec/controllers/api_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,8 +25,6 @@
end

it 'fails with duff values' do
DatabaseCleaner.clean

expect {
post :create, data:
{
Expand All @@ -38,21 +36,45 @@
end

it 'creates a new meds event' do
DatabaseCleaner.clean

expect {
post :create, data: {
"id":"3649",
"datetime":"2016-01-30T14:05:00+00:00",
"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

0 comments on commit 06197e6

Please sign in to comment.