service.rb 818 B

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