简体   繁体   English

将法拉第POST请求与Rails应用程序结合在一起。 AJAX最好?

[英]Incorporating a Faraday POST request with Rails app. AJAX best?

I have managed to get Faraday making a POST request to a Google Calander from a .rb file. 我设法使Faraday从.rb文件向Google Calander发出POST请求。 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? 最好将函数放在约会模型中,然后在用户选择日期时进行AJAX调用吗? Or would I be better to pre-cache it all in a local table? 还是最好将它们全部预先缓存在本地表中?

Thanks for any help! 谢谢你的帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM