lemoynelibrary / lemoyne

Files used in setting up new web design in LibGuides v2 and elsewhere
5 stars 1 forks source link

LibCal JSONP Hours API Broken #59

Closed tomkeays closed 4 years ago

tomkeays commented 4 years ago

Code delivered by the LibCal hours API is broken.

https://codepen.io/tomkeays/pen/WNbNOaw?editors=0010

The code contains extraneous quote characters surrounding the main JSON object and escapes all quote chars inside that object. This change by Springshare was unannounced and probably unintended.

https://api3.libcal.com/api_hours_grid.php?iid=567&format=json&weeks=1&callback=?

?("{\"loc_585\":{\"lid\":585,\"name\":\"Library Hours:\",\"category\":\"library\",\"desc\":\"\",\"url\":\"\",\"contact\":\"\",\"lat\":\"43.048366\",\"long\":\"-76.090515\",\"color\":\"#000000\",\"fn\":\"\",\"weeks\":[{\"Monday\":{\"date\":\"2020-02-17\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"8am\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"8am - 2am\"},\"Tuesday\":{\"date\":\"2020-02-18\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"8am\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"8am - 2am\"},\"Wednesday\":{\"date\":\"2020-02-19\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"8am\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"8am - 2am\"},\"Thursday\":{\"date\":\"2020-02-20\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"8am\",\"to\":\"2am\"}],\"currently_open\":true},\"rendered\":\"8am - 2am\"},\"Friday\":{\"date\":\"2020-02-21\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"8am\",\"to\":\"7pm\"}],\"currently_open\":false},\"rendered\":\"8am - 7pm\"},\"Saturday\":{\"date\":\"2020-02-22\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"9am\",\"to\":\"7pm\"}],\"currently_open\":false},\"rendered\":\"9am - 7pm\"},\"Sunday\":{\"date\":\"2020-02-23\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"9am\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"9am - 2am\"}}]},\"loc_662\":{\"lid\":662,\"name\":\"Card Entry:\",\"category\":\"department\",\"desc\":\"\",\"url\":\"\",\"contact\":\"\",\"lat\":\"\",\"long\":\"\",\"color\":\"#000000\",\"parent_lid\":585,\"weeks\":[{\"Monday\":{\"date\":\"2020-02-17\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"9pm\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"9pm - 2am\"},\"Tuesday\":{\"date\":\"2020-02-18\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"9pm\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"9pm - 2am\"},\"Wednesday\":{\"date\":\"2020-02-19\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"9pm\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"9pm - 2am\"},\"Thursday\":{\"date\":\"2020-02-20\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"9pm\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"9pm - 2am\"},\"Friday\":{\"date\":\"2020-02-21\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"4:30pm\",\"to\":\"7pm\"}],\"currently_open\":false},\"rendered\":\"4:30pm - 7pm\"},\"Saturday\":{\"date\":\"2020-02-22\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"4:30pm\",\"to\":\"7pm\"}],\"currently_open\":false},\"rendered\":\"4:30pm - 7pm\"},\"Sunday\":{\"date\":\"2020-02-23\",\"times\":{\"status\":\"open\",\"hours\":[{\"from\":\"4:30pm\",\"to\":\"2am\"}],\"currently_open\":false},\"rendered\":\"4:30pm - 2am\"}}]}}");

tomkeays commented 4 years ago

Fixed! Here's what the output should look like.

?({"locations":[{"lid":585,"name":"Library Hours:","category":"library","desc":"","url":"","contact":"","lat":"43.048366","long":"-76.090515","color":"#000000","fn":"","weeks":[{"Monday":{"date":"2020-02-17","times":{"status":"open","hours":[{"from":"8am","to":"2am"}],"currently_open":false},"rendered":"8am - 2am"},"Tuesday":{"date":"2020-02-18","times":{"status":"open","hours":[{"from":"8am","to":"2am"}],"currently_open":false},"rendered":"8am - 2am"},"Wednesday":{"date":"2020-02-19","times":{"status":"open","hours":[{"from":"8am","to":"2am"}],"currently_open":false},"rendered":"8am - 2am"},"Thursday":{"date":"2020-02-20","times":{"status":"open","hours":[{"from":"8am","to":"2am"}],"currently_open":false},"rendered":"8am - 2am"},"Friday":{"date":"2020-02-21","times":{"status":"open","hours":[{"from":"8am","to":"7pm"}],"currently_open":false},"rendered":"8am - 7pm"},"Saturday":{"date":"2020-02-22","times":{"status":"open","hours":[{"from":"9am","to":"7pm"}],"currently_open":false},"rendered":"9am - 7pm"},"Sunday":{"date":"2020-02-23","times":{"status":"open","hours":[{"from":"9am","to":"2am"}],"currently_open":false},"rendered":"9am - 2am"}}]},{"lid":662,"name":"Card Entry:","category":"department","desc":"","url":"","contact":"","lat":"","long":"","color":"#000000","parent_lid":585,"weeks":[{"Monday":{"date":"2020-02-17","times":{"status":"open","hours":[{"from":"9pm","to":"2am"}],"currently_open":false},"rendered":"9pm - 2am"},"Tuesday":{"date":"2020-02-18","times":{"status":"open","hours":[{"from":"9pm","to":"2am"}],"currently_open":false},"rendered":"9pm - 2am"},"Wednesday":{"date":"2020-02-19","times":{"status":"open","hours":[{"from":"9pm","to":"2am"}],"currently_open":false},"rendered":"9pm - 2am"},"Thursday":{"date":"2020-02-20","times":{"status":"open","hours":[{"from":"9pm","to":"2am"}],"currently_open":false},"rendered":"9pm - 2am"},"Friday":{"date":"2020-02-21","times":{"status":"open","hours":[{"from":"4:30pm","to":"7pm"}],"currently_open":false},"rendered":"4:30pm - 7pm"},"Saturday":{"date":"2020-02-22","times":{"status":"open","hours":[{"from":"4:30pm","to":"7pm"}],"currently_open":false},"rendered":"4:30pm - 7pm"},"Sunday":{"date":"2020-02-23","times":{"status":"open","hours":[{"from":"4:30pm","to":"2am"}],"currently_open":false},"rendered":"4:30pm - 2am"}}]}]});

tomkeays commented 4 years ago

Keeping this ticket open. The "currently_open" attribute for today's hours is reading false even though we're currently open. The script highlights today's hours based on this flag.

tomkeays commented 4 years ago

Springshare advised me that the new URL for the hours API has been customized to use the library's domain.

https://lemoyne.libcal.com/widget/hours/grid?iid=567&format=json&weeks=1&callback=?

A new CodePen using it is at:

https://codepen.io/tomkeays/pen/GRJrQvL

tomkeays commented 4 years ago

API is still broken, in that the "currently_open" attribute for the current day always reads false, even when hours of operation are within the range defined by the "hours" attribute. E.G., here's today's portion of the API string.

"Friday": {
  "date": "2020-02-28",
  "times": {
    "status": "open",
    "hours": [{
      "from": "8am",
      "to": "7pm"
    }],
    "currently_open": false
  },
  "rendered": "8am - 7pm"
}

The outcome of this flag is that the rendered HTML entry for today's hours should have a "hours--open_now" class added to the <dl> element.

<dl class="hours--open_now">
  <dt title="2020-02-28">Friday</dt>
  <dd>Library Hours: 8am - 7pm</dd>
  <dd>Card Required for Entry: 4:30pm - 7pm</dd>
</dl>

This will add a 1px border around the rendered item. (Note: this rendering was accomplished by manually adding the class to the element using Chrome Dev Tools.)

image

tomkeays commented 4 years ago

Springshare support emailed me this morning to say

Our LibCal team let us know this was an issue with caching and they have since resolved it.

Indeed, when I first looked, the border was back on today's hours of service. But, when I looked a few minutes later, the border was gone again. I looked at both the current and deprecated version of the API URL and both were rendering identically, with "currently_open":false for today's hours.

This has happened a few times now, with the API oscillating between "currently_open":true and "currently_open":false. So clearly, something in the API or the caching system is still not working completely correctly.

I replied to Springshare that I would monitor the situation throughout the day and advise them if it ever settles down. I also asked them a related side question. I'll update this comment when I hear back from them.

If I remember correctly, the Hours API is supposed to clear out the cache whenever we make changes to our hours of service in LibCal. We haven't needed to do that in the past week. But I was also under the impression that there was a cronjob running that would flush the cache every 1/2 hour (or maybe it was every 1 hour, I forget) even if we made no changes. Am I right about the scheduled refresh/purging of the cache?

tomkeays commented 4 years ago

With the COVID-19 epidemic and the fact that we're closed and can no longer test this situation, I'm going to close this ticket. When we put the hours back up, we can reassess things.