1
0

service.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. class Service < ActiveRecord::Base
  2. serialize :options, Hash
  3. belongs_to :user, :inverse_of => :services
  4. has_many :agents, :inverse_of => :service
  5. validates_presence_of :user_id, :provider, :name, :token
  6. before_destroy :disable_agents
  7. scope :available_to_user, lambda { |user| where("services.user_id = ? or services.global = true", user.id) }
  8. scope :by_name, lambda { |dir = 'desc'| order("services.name #{dir}") }
  9. def disable_agents(conditions = {})
  10. agents.where.not(conditions[:where_not] || {}).each do |agent|
  11. agent.service_id = nil
  12. agent.disabled = true
  13. agent.save!(validate: false)
  14. end
  15. end
  16. def toggle_availability!
  17. disable_agents(where_not: {user_id: self.user_id}) if global
  18. self.global = !self.global
  19. self.save!
  20. end
  21. def prepare_request
  22. if expires_at && Time.now > expires_at
  23. refresh_token!
  24. end
  25. end
  26. def refresh_token!
  27. response = HTTParty.post(endpoint, query: {
  28. type: 'refresh',
  29. client_id: oauth_key,
  30. client_secret: oauth_secret,
  31. refresh_token: refresh_token
  32. })
  33. data = JSON.parse(response.body)
  34. update(expires_at: Time.now + data['expires_in'], token: data['access_token'], refresh_token: data['refresh_token'].presence || refresh_token)
  35. end
  36. def endpoint
  37. client_options = "OmniAuth::Strategies::#{OmniAuth::Utils.camelize(self.provider)}".constantize.default_options['client_options']
  38. URI.join(client_options['site'], client_options['token_url'])
  39. end
  40. def oauth_key
  41. (config = Devise.omniauth_configs[provider.to_sym]) && config.args[0]
  42. end
  43. def oauth_secret
  44. (config = Devise.omniauth_configs[provider.to_sym]) && config.args[1]
  45. end
  46. def self.initialize_or_update_via_omniauth(omniauth)
  47. options = get_options(omniauth)
  48. find_or_initialize_by(provider: omniauth['provider'], uid: omniauth['uid'].to_s).tap do |service|
  49. service.assign_attributes token: omniauth['credentials']['token'],
  50. secret: omniauth['credentials']['secret'],
  51. name: options[:name],
  52. refresh_token: omniauth['credentials']['refresh_token'],
  53. expires_at: omniauth['credentials']['expires_at'] && Time.at(omniauth['credentials']['expires_at']),
  54. options: options
  55. end
  56. end
  57. def self.register_options_provider(provider_name, &block)
  58. option_providers[provider_name] = block
  59. end
  60. def self.get_options(omniauth)
  61. option_providers.fetch(omniauth['provider'], option_providers['default']).call(omniauth)
  62. end
  63. private
  64. @@option_providers = HashWithIndifferentAccess.new
  65. cattr_reader :option_providers
  66. register_options_provider('default') do |omniauth|
  67. {name: omniauth['info']['nickname'] || omniauth['info']['name']}
  68. end
  69. register_options_provider('37signals') do |omniauth|
  70. {user_id: omniauth['extra']['accounts'][0]['id'], name: omniauth['info']['name']}
  71. end
  72. end