retrospring/app/models/service.rb
2022-01-05 19:49:59 +01:00

41 lines
926 B
Ruby

class Service < ApplicationRecord
attr_accessor :provider, :info
belongs_to :user
validates_uniqueness_of :uid, scope: :type
validates_length_of :post_tag, maximum: 20
class << self
def first_from_omniauth(auth_hash)
@@auth = auth_hash
where(type: service_type, uid: options[:uid]).first
end
def initialize_from_omniauth(auth_hash)
@@auth = auth_hash
service_type.constantize.new(options)
end
private
def auth
@@auth
end
def service_type
"Services::#{options[:provider].camelize}"
end
def options
{
nickname: auth['info']['nickname'],
access_token: auth['credentials']['token'],
access_secret: auth['credentials']['secret'],
uid: auth['uid'],
provider: auth['provider'],
info: auth['info']
}
end
end
end