将法拉第POST请求与Rails app结合使用。 AJAX最好?

时间:2012-02-19 11:46:31

标签: ruby-on-rails ajax google-api faraday

我设法让法拉第从.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调用吗?或者我会更好地将它全部预先缓存在本地表中?

感谢您的帮助!

1 个答案:

答案 0 :(得分: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