我有一个导轨应用程序。我试图调整不同的模型和控制器。我可以让事情发挥作用,但我不确定我是否采用了首选方式。所以我有一个event.rb用于我自己的应用程序fullcalendar,在那里我可以删除新事件,并为omniauth授权(本例中为谷歌日历)提供social.rb。我试图在我的fullcalendar中显示gcal事件,所以根据social.rb数据(标记,键),我调用一个API调用google来获取gcal事件时间,然后在events/index页面中显示数据(fullcalendar )。导轨调用方法控制器vs模型
下面是我的两个方法,我不知道如何放置在我的应用程序中。我的问题(我有3个,因为它们紧密相连):
- 方法类型。正如我看到init_google_api_calendar_client应该是一个类方法,如果我把它放到social.rb(我甚至不知道是否应该把它放在那里)。对于get_busy_events,我根本无法确定要使用的类型。
- 我想我应该把这些方法放在模型/模块中,但哪一个(社会/事件/别的东西)?
- 我应该如何调用这些方法?
def index
@user = current_user
@google = @user.socials.where(provider: "google_oauth2").first
@client = Social.init_google_api_calendar_client(@google) ### Is this the proper way to call this method?
@get_busy_times = #####how to call the get_buys_events method?
@event = Event.new
@events = Event.allevents(current_user)
respond_to do |format|
format.html
format.json { render json: @events }
format.js
end
end
social.rb
def self.init_google_api_calendar_client(google_account)
#method only called if google_oauth2 social exists
client = Google::APIClient.new
client.authorization.access_token = google_account.token
client.authorization.client_id = ENV['GOOGLE_API_KEY']
client.authorization.client_secret = ENV['GOOGLE_API_SECRET']
client.authorization.refresh_token = google_account.refresh_token
return client
end
如果把这种方法吗?应该是类/实例吗?应该如何调用?
def get_busy_events(client)
service = client.discovered_api('calendar', 'v3')
result = client.execute(
api_method: service.freebusy.query,
body_object: { timeMin: start_at,
timeMax: end_at,
items: items},
headers: {'Content-Type' => 'application/json'})
end
肖恩,thx!你能给我发一篇好文章给第二个版本吗?对于这个功能,我需要这种方法(尽管我将使用sidekiq进行电子邮件通知)。 –
当然。这个是约会几年,但遵循约定做得很好,而且很具描述性:http://code.tutsplus.com/articles/writing-an-api-wrapper-in-ruby-with-tdd--net- 23875 –