Sha256: 42e9b9cc230e16ee3e0ed56bcfd7551c8c86da0a6f46d5b467c4deeafed0ad62
Contents?: true
Size: 1.6 KB
Versions: 9
Compression:
Stored size: 1.6 KB
Contents
module BookingSync::Engine::Model extend ActiveSupport::Concern included do validates :synced_id, uniqueness: true scope :authorized, -> { where.not(oauth_access_token: nil) } end module ClassMethods def from_omniauth(auth) find_or_initialize_by(synced_id: auth.uid, provider: auth.provider).tap do |account| account.name = auth.info.business_name account.update_token(auth.credentials) account.save! end end end def token @token ||= begin token_options = {} if oauth_refresh_token token_options[:refresh_token] = oauth_refresh_token token_options[:expires_at] = oauth_expires_at end token = OAuth2::AccessToken.new(BookingSync::Engine.oauth_client, oauth_access_token, token_options) if token.expired? refresh_token!(token) else token end end end def refresh_token!(current_token = token) @token = current_token.refresh!.tap { |new_token| update_token!(new_token) } end def api @api ||= BookingSync::Engine::APIClient.new(token.token, account: self) end def update_token(token) self.oauth_access_token = token.token self.oauth_refresh_token = token.refresh_token self.oauth_expires_at = token.expires_at end def update_token!(token) Thread.new do ActiveRecord::Base.connection_pool.with_connection do update_token(token) save! end end.join end def clear_token! self.oauth_access_token = nil self.oauth_refresh_token = nil self.oauth_expires_at = nil save! end end
Version data entries
9 entries across 9 versions & 1 rubygems