Retrospring/app/models/service.rb

42 lines
926 B
Ruby
Raw Normal View History

2020-04-18 15:59:18 -07:00
class Service < ApplicationRecord
2014-12-12 13:35:23 -08:00
attr_accessor :provider, :info
2014-12-12 12:42:34 -08:00
belongs_to :user
validates_uniqueness_of :uid, scope: :type
validates_length_of :post_tag, maximum: 20
2014-12-12 12:42:34 -08:00
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