app/models/skydrive/user.rb in lti_skydrive-1.2.0 vs app/models/skydrive/user.rb in lti_skydrive-1.2.1

- old
+ new

@@ -1,9 +1,12 @@ module Skydrive class User < ActiveRecord::Base include ActiveModel::ForbiddenAttributesProtection + after_initialize :ensure_token + after_initialize :cleanup_api_keys + has_many :api_keys has_one :token belongs_to :account validates :username, presence: true @@ -24,23 +27,29 @@ def valid_skydrive_token? !!self.token && self.token.is_valid? end def ensure_token + if self.token + begin + self.token.refresh! if self.token.requires_refresh? + rescue Skydrive::APIResponseErrorException, JSON::ParserError => error + self.reset_token! + end + end + self.token = self.create_token unless self.token end def reset_token! self.token.delete ensure_token end - # Convenience method - def onedrive_client - @onedrive_client ||= + def skydrive_client + @skydrive_client ||= Client.new(SHAREPOINT.merge( - personal_url: self.token.personal_url, - token: self.token.access_token + user_token: self.token )) end end end