weibo_concern.rb 774 B

123456789101112131415161718192021222324252627
  1. module WeiboConcern
  2. extend ActiveSupport::Concern
  3. included do
  4. gem_dependency_check { defined?(WeiboOAuth2) }
  5. self.validate :validate_weibo_options
  6. end
  7. def validate_weibo_options
  8. unless options['app_key'].present? &&
  9. options['app_secret'].present? &&
  10. options['access_token'].present?
  11. errors.add(:base, "app_key, app_secret and access_token are required")
  12. end
  13. end
  14. def weibo_client
  15. unless @weibo_client
  16. WeiboOAuth2::Config.api_key = options['app_key'] # WEIBO_APP_KEY
  17. WeiboOAuth2::Config.api_secret = options['app_secret'] # WEIBO_APP_SECRET
  18. @weibo_client = WeiboOAuth2::Client.new
  19. @weibo_client.get_token_from_hash :access_token => options['access_token']
  20. end
  21. @weibo_client
  22. end
  23. end