Class: Authentication

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/minimum/omniauth/scaffold/templates/models/authentication.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_with_omniauth(auth) ⇒ Object

auth情報登録



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/minimum/omniauth/scaffold/templates/models/authentication.rb', line 33

def create_with_omniauth(auth)
  authentication = Authentication.new
  authentication.provider = auth["provider"]
  authentication.uid      = auth["uid"]

  if auth["info"].present?
    authentication.name     = auth["info"]["name"]
    authentication.nickname = auth["info"]["nickname"]
    authentication.image    = auth["info"]["image"]
    authentication.email    = auth["info"]["email"]
    authentication.location = auth["info"]["location"]
  end

  if auth["credentials"].present?
    authentication.token  = auth['credentials']['token']
    authentication.secret = auth['credentials']['secret']
  end

  if auth["extra"].present? and auth["extra"]["raw_info"].present?
    authentication.gender   = auth["extra"]["raw_info"]["gender"]
    authentication.location = auth["extra"]["raw_info"]["location"] if authentication.location.blank?
  end

  authentication.save!

  return authentication
end

Instance Method Details

#auth_update(auth) ⇒ Object

auth情報更新



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/minimum/omniauth/scaffold/templates/models/authentication.rb', line 9

def auth_update(auth)
  case auth["provider"]
  when "facebook"
    image_path = "https://graph.facebook.com/#{auth['info']['nickname'].presence || auth["uid"]}/picture?width=200&height=200"
  when "twitter"
    image_path = auth["info"]["image"].to_s.gsub('_normal', '') rescue nil
  when "github"
    image_path = "#{auth['info']['image']}&size=200" rescue nil
  end

  gender   = auth["extra"]["raw_info"]["gender"] rescue nil
  location = (auth["info"]["location"].presence || auth["extra"]["raw_info"]["location"]) rescue nil

  self.name     = auth["info"]["name"]     if auth["info"]["name"].present?
  self.nickname = auth["info"]["nickname"] if auth["info"]["nickname"].present?
  self.image    = image_path               if image_path.present?
  self.email    = auth["info"]["email"]    if auth["info"]["email"].present?
  self.gender   = gender                   if gender.present?
  self.location = location                 if location.present?
  self.save!
end