0

I am trying to show the "sgv" value on a Dashing / Smashing dashboard widget. Ultimately I would also like to show the "direction" value as well. I am running into problems pulling that precise value down which changes every 3 to 5 minutes. I have already been able to mirror the exact string using the following:

require 'net/http'
require 'rest-client'
require 'json'

url = "https://dnarnianbg.herokuapp.com/api/v1/entries/current.json"
response = RestClient.get(url)
JSON.parse(response)

# :first_in sets how long it takes before the job is first run. In this case, it is run immediately
current_nightscout = 0

SCHEDULER.every '5m' do 
  last_nightscout = current_nightscout
  current_nightscout = response

  send_event('nightscout', { current: current_nightscout, last: last_nightscout })
 end

I have also searched the archives several times. I don't wish to write this to a file like this one shows and the duplicate question has been deleted or moved.

I realize that the JSON.parse(response) is just going to parse out whatever I tell it the response equals, but I don't know how to get that response to equal SGV. Maybe the solution isn't in the RestClient, but that is where I am lost.

Here is the JSON URL: http://dnarnianbg.herokuapp.com/api/v1/entries/current.json

EDIT: The output of that link is something like this:

[{"_id":"5ba295ddb8a1ee0aede71822","sgv":87,"date":1537381813000,"dateString":"2018-09-19T18:30:13.000Z","trend":4,"direction":"Flat","device":"share2","type":"sgv"}]

  • Yes. It's a blood sugar reading. I want to display the value of the SGV on a dashboard – thatdamntiffy Sep 20 '18 at 18:48
  • Something like: current_nightscout = response[0]["sgv"] – thatdamntiffy Sep 20 '18 at 19:06
  • @user3788685 do you mind if we go through what happened there so I can use learn from this? `JSON.parse(open("url" ` - **is this parsing the JSON and opening that actual URL?** `:read_timeout => 4` - **I figured this was a read time out, but what does the => 4 equate to?** `p test [0]["sgv"]` - **I figured this was performing the puts action on the variable test but I am not sure what the [0] part is doing?** Thanks for all of your help – thatdamntiffy Sep 20 '18 at 21:18

2 Answers2

0

Here is the final code that made it work

require 'net/http'
require 'json'
require 'rest-client'

# :first_in sets how long it takes before the job is first run. In this case, it is run immediately
current_nightscout = 0

SCHEDULER.every '1m' do 

test = JSON.parse(open("https://dnarnianbg.herokuapp.com/api/v1/entries/current.json", :read_timeout => 4).read)


  last_nightscout = current_nightscout
  current_nightscout = p test[0]["sgv"]

  send_event('nightscout', { current: current_nightscout, last: last_nightscout })
 end

I can probably eliminate require 'rest-client' since that is no longer being used, but it works right now and that is all that matters.

0

You need something like response[0]["sgv"] which should return 52 if you end up with many items in the list you will need to iterate over them.

The best thing you can do is to break your problem down into easier parts to debug. As you are having problems accessing some JSON via an API you should make a simple script which only does the function you want in order to test it and see where the problem is.

Here is a short example you can put into a .rb file and run;

#!/usr/bin/ruby

require 'open-uri'
require 'json'

test = JSON.parse(open("https://dnarnianbg.herokuapp.com/api/v1/entries/current.json", :read_timeout => 4).read)

puts test[0]["sgv"]

That should return the value from sgv

I realise that short sweet example may be little use as a learner so here is a more verbose version with some comments;

#!/usr/bin/ruby

require 'open-uri'
require 'json'

# Open the URL and read the result. Time out if this takes longer then 4 sec.
get_data = open("https://dnarnianbg.herokuapp.com/api/v1/entries/current.json", :read_timeout => 4).read

# Parse the response (get_data) to JSON and put in variable output
output = JSON.parse(get_data)

# Put the output to get the 'sgv figure'
p output[0]["sgv"]

It always pays to manually examine the data you get back, in your case the data looks like this (when make pretty)

[
  {
    "_id": "5ba41a0fb8a1ee0aedf6eb2c",
    "sgv": 144,
    "date": 1537481109000,
    "dateString": "2018-09-20T22:05:09.000Z",
    "trend": 4,
    "direction": "Flat",
    "device": "share2",
    "type": "sgv"
  }
]

What you actually have is an Array. Your server returns only 1 result, numbered '0' hence you need [0] in your p statement. Once you have accessed the array id then you can simply use the object you need as [sgv]

If your app ever returns more than one record then you will need to change your code to read all of the results and iterate over them in order to get all the values you need.

user3788685
  • 2,943
  • 5
  • 26
  • 45
  • I can get it to do more than one at a time by removing the current.json and just doing entries.json. Thanks for helping me out with that! – thatdamntiffy Sep 21 '18 at 19:47