1

我设法让法拉第从 .rb 文件向 Google Calander 发出 POST 请求。在我的用户选择日期后,我想在我的应用程序中使用此功能检查日历上的忙/闲响应,然后再向我的用户显示可用时间段列表。

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

是否最好将函数放在约会模型中,然后在用户选择日期时进行 AJAX 调用?或者我会更好地将其全部预缓存在本地表中吗?

谢谢你的帮助!

4

1 回答 1

1

第一次尝试:

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
于 2012-02-19T23:12:36.220 回答