evernote_concern.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module EvernoteConcern
  2. extend ActiveSupport::Concern
  3. included do
  4. include Oauthable
  5. validate :validate_evernote_options
  6. valid_oauth_providers :evernote
  7. gem_dependency_check { defined?(EvernoteOAuth) && Devise.omniauth_providers.include?(:evernote) }
  8. end
  9. def evernote_client
  10. EvernoteOAuth::Client.new(
  11. token: evernote_oauth_token,
  12. consumer_key: evernote_consumer_key,
  13. consumer_secret: evernote_consumer_secret,
  14. sandbox: use_sandbox?
  15. )
  16. end
  17. private
  18. def use_sandbox?
  19. ENV["USE_EVERNOTE_SANDBOX"] == "true"
  20. end
  21. def validate_evernote_options
  22. unless evernote_consumer_key.present? &&
  23. evernote_consumer_secret.present? &&
  24. evernote_oauth_token.present?
  25. errors.add(:base, "Evernote ENV variables and a Service are required")
  26. end
  27. end
  28. def evernote_consumer_key
  29. (config = Devise.omniauth_configs[:evernote]) && config.strategy.consumer_key
  30. end
  31. def evernote_consumer_secret
  32. (config = Devise.omniauth_configs[:evernote]) && config.strategy.consumer_secret
  33. end
  34. def evernote_oauth_token
  35. service && service.token
  36. end
  37. end