Skip to content

Commit

Permalink
We have one working endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pikesley committed Jan 30, 2016
1 parent 4eeed0a commit 1787b86
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/assets/javascripts/api.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/api.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the api controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
16 changes: 16 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class ApiController < ApplicationController
def create
j = JSON.parse params[:json]

k = {}
k['datetime'] = j['datetime']
k['value'] = j['value']

g = GlucoseMeasurement.new k
if g.save
render json: g, status: :ok
else
render json: k, status: 400
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/api_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ApiHelper
end
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
resources :glucose_measurements, path: 'glucose'
resources :medication_events, path: 'meds'
resources :carbohydrate_intakes, path: 'carbs'
resources :physical_exercises, path: 'exercise'
resources :physical_exercises, path: 'exercise'
resources :api

root 'welcome#index'
end
15 changes: 15 additions & 0 deletions dirty_hacky_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'xmlsimple'
require 'json'

data = XmlSimple.xml_in File.read 'spec/support/fixtures/incoming.xml'

r = data['record']
r.sort! { |x, y| x['datetime'] <=> y['datetime'] }

r.each do |datum|
datum.each_pair do |k, v|
datum[k] = v[0]
end
end

puts r
30 changes: 30 additions & 0 deletions spec/controllers/api_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe ApiController, type: :controller do
describe 'POST #create' do
context 'with valid attributes' do
it 'creates a new glucose measurement' do
expect {
post :create, json:
{
"id":"3648",
"datetime":"2016-01-30T13:58:31+00:00",
"type":"Glucose",
"subtype"=>{},
"category":"Lunch",
"value":"12.0"
}.to_json
}.to change(GlucoseMeasurement, :count).by 1
end

it 'fails with duff values' do
expect {
post :create, json:
{
"datetime":"2016-01-30T13:58:31+00:00",
"type":"Glucose",
"value":"0"
}.to_json
}.to_not change(GlucoseMeasurement, :count)
end
end
end
end
15 changes: 15 additions & 0 deletions spec/helpers/api_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the ApiHelper. For example:
#
# describe ApiHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe ApiHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
6 changes: 6 additions & 0 deletions spec/support/fixtures/incoming.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{"datetime":"2016-01-29T19:18:29+00:00","category":"Dinner","value":"12.0"}
{"datetime":"2016-01-29T19:33:12+00:00","category":"Dinner","value":"14.0"}
{"datetime":"2016-01-30T01:40:18+00:00","category":"Bedtime","value":"21.6"}
{"datetime":"2016-01-30T01:46:26+00:00","category":"Bedtime","value":"14.0"}
{"datetime":"2016-01-30T10:26:03+00:00","category":"Breakfast","value":"10.7"}
{"datetime":"2016-01-30T10:39:25+00:00","category":"Breakfast","value":"6.0","subtype":"humalog"}
58 changes: 58 additions & 0 deletions spec/support/fixtures/incoming.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<ontrack>
<record>
<id>3649</id>
<datetime>2016-01-30T14:05:00+00:00</datetime>
<type>Medication</type>
<subtype>Humalog</subtype>
<category>Lunch</category>
<value>6.0</value>
</record>
<record>
<id>3648</id>
<datetime>2016-01-30T13:58:31+00:00</datetime>
<type>Glucose</type>
<subtype></subtype>
<category>Lunch</category>
<value>12.0</value>
</record>
<record>
<id>3647</id>
<datetime>2016-01-30T10:39:25+00:00</datetime>
<type>Medication</type>
<subtype>Humalog</subtype>
<category>Breakfast</category>
<value>6.0</value>
</record>
<record>
<id>3646</id>
<datetime>2016-01-30T10:26:03+00:00</datetime>
<type>Glucose</type>
<subtype></subtype>
<category>Breakfast</category>
<value>10.7</value>
</record>
<record>
<id>3645</id>
<datetime>2016-01-30T01:46:26+00:00</datetime>
<type>Medication</type>
<subtype>Lantus</subtype>
<category>Bedtime</category>
<value>14.0</value>
</record>
<record>
<id>3644</id>
<datetime>2016-01-30T01:40:18+00:00</datetime>
<type>Glucose</type>
<subtype></subtype>
<category>Bedtime</category>
<value>21.6</value>
</record>
<record>
<id>3643</id>
<datetime>2016-01-29T19:33:12+00:00</datetime>
<type>Medication</type>
<subtype>Humalog</subtype>
<category>Dinner</category>
<value>14.0</value>
</record>
</ontrack>

0 comments on commit 1787b86

Please sign in to comment.