2

I have managed to get Faraday making a POST request to a Google Calander from a .rb file. I'd like to use this functionality in my app to check Free/Busy responses on calendars before displaying a list of availble time slots to my user, after they select the date.

response = Faraday.post do |req|
req.url 'https://www.googleapis.com/calendar/v3/freeBusy?key=myapikey'
req.headers['Content-Type'] = 'application/json'
req.body = '{ "items": [ { "id": mycalendar } ], "timeMin": "2011-02-14T00:09:35Z", "timeMax": "2011-02-14T00:09:40Z" }'
end

Is it best to put the function in an appointments model, and then have an AJAX call when the user selects the date? Or would I be better to pre-cache it all in a local table?

Thanks for any help!

William Stock
  • 63
  • 2
  • 6

1 Answers1

1

First attempt:

require 'faraday'

class Booking < ActiveRecord::Base

def checktime(calid)

  response = Faraday.post do |req|
    req.url 'https://www.googleapis.com/calendar/v3/freeBusy?key=AIzaSyBsObpXXF6DQoZWj2U9QxoEQ4vdZ6GvNfI'
    req.headers['Content-Type'] = 'application/json'
    req.body = "{ 'items': [ { 'id': '#{calid}' } ], 'timeMin': '2012-02-19T19:35:00Z', 'timeMax': '2012-02-19T19:40:00Z' }"
  end

response_json = JSON.parse response.body

if response_json["calendars"]["busy"] == nil 
  return true
else
  return false
end

end
William Stock
  • 63
  • 2
  • 6