Class: SocialAuth::Service

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/social_auth/service.rb

Constant Summary collapse

ACCEPTED_METHODS =
%w(Authenticated Connected)
REDIS_CACHE =

cache expiry in seconds

2_592_000

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_to_associated_services(id) ⇒ Object



70
71
72
73
74
75
# File 'app/models/social_auth/service.rb', line 70

def self.append_to_associated_services(id)
  service = find(id)
  service.services.each do |s|
    s.append_to_friends_list(service)
  end
end

.connect_with(user, auth_token) ⇒ Object



38
39
40
# File 'app/models/social_auth/service.rb', line 38

def self.connect_with(user, auth_token)
  raise "need to override"
end

.create_with_request(remote_id, user, method = "Connected", access_token = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/social_auth/service.rb', line 42

def self.create_with_request(remote_id, user, method="Connected", access_token={})
  remote_id = remote_id.to_s

  unless (service = find_by_remote_id_and_method(remote_id, method)) && method == "Authenticated"
    #attempts to look if some other user connected this same facebook account if its an authentication request
    if count = where(remote_id: remote_id, method: "Connected").count == 1 and method == "Authenticated"
      service = find_by_remote_id_and_method(remote_id, "Connected")
    else
      service = new
      service.remote_id = remote_id

      #gives the owner one last chance to perform some app level logic on the user before being created
      user = user.validate_existing_user(remote_id, service.type) if user.respond_to?(:validate_existing_user)
      service.user = user
      service.method = method
    end
  end

  service.access_token = access_token
  service.save

  return service
end

.disconnect_user(user) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'app/models/social_auth/service.rb', line 92

def self.disconnect_user(user)
  service = find_by_user_id(user.id)
  if service
    raise Error.new("Cannot disconnect a service you used to authenticate with") if service.authenticated?

    service.disconnect(nil, false)
  else
    raise ServiceDoesNotExist.new("Couldn't find service for this user")
  end
end

.init_with(auth_token) ⇒ Object



34
35
36
# File 'app/models/social_auth/service.rb', line 34

def self.init_with(auth_token)
  raise "need to override"
end

Instance Method Details

#access_tokenObject



26
27
28
29
30
31
32
# File 'app/models/social_auth/service.rb', line 26

def access_token
  if super.blank?
    {}
  else
    super.with_indifferent_access
  end
end

#append_to_friends_list(service) ⇒ Object



77
78
79
80
81
82
# File 'app/models/social_auth/service.rb', line 77

def append_to_friends_list(service)
  if redis_instance.exists(self.redis_key(:friends))
    redis_instance.sadd(self.redis_key(:friends), service.remote_id)
  end
  user.friend_joined_the_app_callback(service.user) if user.respond_to?(:friend_joined_the_app_callback)
end

#authenticated?Boolean

Returns:

  • (Boolean)


124
125
126
127
# File 'app/models/social_auth/service.rb', line 124

def authenticated?
  return true if method == 'Authenticated'
  false
end

#connected?Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'app/models/social_auth/service.rb', line 129

def connected?
  return true if method == 'Connected'
  false
end

#disconnect(e = nil, callback = true) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/social_auth/service.rb', line 103

def disconnect(e=nil, callback=true)
  if connected?
    #destroys service
    self.destroy
    #notifies the user that their service is about to be disconnected
    user.service_disconnected_callback(self) if user.respond_to?(:service_disconnected_callback) and callback
  else
    #re_raises the exception
    raise InvalidToken.new(e ? e.message : "Token has become invalid") #move to localization file
  end
end

#friend_idsObject



84
85
86
87
88
89
90
# File 'app/models/social_auth/service.rb', line 84

def friend_ids
  if redis_instance.exists(redis_key(:friends))
    friend_ids = redis_instance.smembers(redis_key(:friends))
  else
    []
  end
end

#nameObject



22
23
24
# File 'app/models/social_auth/service.rb', line 22

def name
  raise "need to implement"
end

#redis_instanceObject



120
121
122
# File 'app/models/social_auth/service.rb', line 120

def redis_instance
  SocialAuth.redis_instance_method
end

#redis_key(str) ⇒ Object

helper method to generate redis keys



116
117
118
# File 'app/models/social_auth/service.rb', line 116

def redis_key(str)
  "#{type}:#{id}:#{str}"
end

#servicesObject



66
67
68
# File 'app/models/social_auth/service.rb', line 66

def services
  self.class.where('remote_id IN (?)', friend_ids)
end