-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathlinkedin.rb
executable file
·151 lines (120 loc) · 4.01 KB
/
linkedin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'omniauth-oauth2'
module OmniAuth
module Strategies
class LinkedIn < OmniAuth::Strategies::OAuth2
option :name, 'linkedin'
option :client_options, {
:site => 'https://api.linkedin.com',
:authorize_url => 'https://www.linkedin.com/oauth/v2/authorization?response_type=code',
:token_url => 'https://www.linkedin.com/oauth/v2/accessToken'
}
option :scope, 'r_liteprofile r_emailaddress'
option :fields, ['id', 'first-name', 'last-name', 'picture-url', 'email-address']
uid do
raw_info['id']
end
info do
options.scope.include?('r_basicprofile') ? lite_info.merge(basic_info) : lite_info
end
extra do
{
'raw_info' => raw_info
}
end
def callback_url
full_host + script_name + callback_path
end
alias :oauth2_access_token :access_token
def access_token
::OAuth2::AccessToken.new(client, oauth2_access_token.token, {
:expires_in => oauth2_access_token.expires_in,
:expires_at => oauth2_access_token.expires_at
})
end
def raw_info
@raw_info ||= access_token.get(profile_endpoint).parsed
end
private
def lite_info
{
:email => email_address,
:first_name => localized_field('firstName'),
:last_name => localized_field('lastName'),
:picture_url => picture_url
}
end
def basic_info
{
:vanity_name => raw_info['vanityName'],
:maiden_name => localized_field('maidenName'),
:headline => localized_field('headline')
}
end
def email_address
if options.fields.include? 'email-address'
fetch_email_address
parse_email_address
end
end
def fetch_email_address
@email_address_response ||= access_token.get(email_address_endpoint).parsed
end
def parse_email_address
return unless email_address_available?
@email_address_response['elements'].first['handle~']['emailAddress']
end
def email_address_available?
@email_address_response['elements'] &&
@email_address_response['elements'].is_a?(Array) &&
@email_address_response['elements'].first &&
@email_address_response['elements'].first['handle~']
end
def fields_mapping
{
'id' => 'id',
'first-name' => 'firstName',
'last-name' => 'lastName',
'picture-url' => 'profilePicture(displayImage~:playableStreams)',
'vanity-name' => 'vanityName',
'maiden-name' => 'maidenName',
'headline' => 'headline'
}
end
def fields
options.fields.each.with_object([]) do |field, result|
result << fields_mapping[field] if fields_mapping.has_key? field
end
end
def localized_field field_name
return unless localized_field_available? field_name
raw_info[field_name]['localized'][field_locale(field_name)]
end
def field_locale field_name
"#{ raw_info[field_name]['preferredLocale']['language'] }_" \
"#{ raw_info[field_name]['preferredLocale']['country'] }"
end
def localized_field_available? field_name
raw_info[field_name] && raw_info[field_name]['localized']
end
def picture_url
return unless picture_available?
picture_references.last['identifiers'].first['identifier']
end
def picture_available?
raw_info['profilePicture'] &&
raw_info['profilePicture']['displayImage~'] &&
picture_references
end
def picture_references
raw_info['profilePicture']['displayImage~']['elements']
end
def email_address_endpoint
'/v2/emailAddress?q=members&projection=(elements*(handle~))'
end
def profile_endpoint
"/v2/me?projection=(#{ fields.join(',') })"
end
end
end
end
OmniAuth.config.add_camelization 'linkedin', 'LinkedIn'