service.rb 792 B

1234567891011121314151617181920212223242526272829303132
  1. class Service < ApplicationRecord
  2. belongs_to :user
  3. Devise.omniauth_configs.keys.each do |provider|
  4. scope provider, ->{ where(provider: provider) }
  5. end
  6. def client
  7. send("#{provider}_client")
  8. end
  9. def expired?
  10. expires_at? && expires_at <= Time.zone.now
  11. end
  12. def access_token
  13. send("#{provider}_refresh_token!", super) if expired?
  14. super
  15. end
  16. def twitter_client
  17. Twitter::REST::Client.new do |config|
  18. config.consumer_key = Rails.application.secrets.twitter_app_id
  19. config.consumer_secret = Rails.application.secrets.twitter_app_secret
  20. config.access_token = access_token
  21. config.access_token_secret = access_token_secret
  22. end
  23. end
  24. def twitter_refresh_token!(token); end
  25. end