Class: ApiUserAuth::Providers::Facebook

Inherits:
Object
  • Object
show all
Defined in:
lib/api_user_auth/providers/facebook.rb

Overview

Get info from Facebook token

Constant Summary collapse

API_PATH =
'https://graph.facebook.com/v3.0/me'.freeze
FIELDS =
%w[id name email birthday hometown gender picture].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Facebook

Returns a new instance of Facebook.



10
11
12
13
# File 'lib/api_user_auth/providers/facebook.rb', line 10

def initialize(token)
  @token = token
  @data = {}
end

Class Method Details

.get_user(token) ⇒ Object



43
44
45
46
# File 'lib/api_user_auth/providers/facebook.rb', line 43

def self.get_user(token)
  fb = Facebook.new(token)
  fb.get_user_data
end

Instance Method Details

#api_info_urlObject



15
16
17
18
19
20
21
22
# File 'lib/api_user_auth/providers/facebook.rb', line 15

def api_info_url
  params = {
    access_token: @token,
    fields: FIELDS.join(','),
    format: 'json'
  }
  URI("#{API_PATH}?#{params.to_query}")
end

#get_user_dataObject



24
25
26
27
# File 'lib/api_user_auth/providers/facebook.rb', line 24

def get_user_data
  api_get_request
  user_data
end

#user_dataObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/api_user_auth/providers/facebook.rb', line 29

def user_data
  {
    id: @data[:id], name: @data[:name],
    email: @data[:email] || "#{@data[:id]}@facebook.com",
    provider: 'facebook',
    img_url: (@data[:picture] || {}).try(:[], :data).try(:[], :url),
    info: {
      birthday: @data[:birthday],
      city: (@data[:hometown] || {}).try(:[], :name),
      gender: @data[:gender]
    }
  }
end