-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
86 lines (73 loc) · 1.94 KB
/
server.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
require "rubygems"
require "haml"
require "sinatra"
require "linkedin"
require 'json'
enable :sessions
helpers do
def login?
!session[:atoken].nil?
end
def profile
linkedin_client.profile unless session[:atoken].nil?
end
def connections
linkedin_client.connections unless session[:atoken].nil?
end
private
def linkedin_client
client = LinkedIn::Client.new(settings.api, settings.secret)
client.authorize_from_access(session[:atoken], session[:asecret])
client
end
end
configure do
# get your api keys at https://www.linkedin.com/secure/developer
set :api, "75rgtmh80zyxog"
set :secret, "ssQv20q9qEHahxBG"
end
get "/auth" do
client = LinkedIn::Client.new(settings.api, settings.secret)
request_token = client.request_token(:oauth_callback => "http://#{request.host}:#{request.port}/auth/callback")
session[:rtoken] = request_token.token
session[:rsecret] = request_token.secret
redirect client.request_token.authorize_url
end
get "/auth/logout" do
session[:atoken] = nil
redirect "/"
end
get "/auth/callback" do
client = LinkedIn::Client.new(settings.api, settings.secret)
if session[:atoken].nil?
pin = params[:oauth_verifier]
atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
session[:atoken] = atoken
session[:asecret] = asecret
end
redirect "/profile"
end
get "/profile" do
begin
search_hash = Hash[params.map{ |k, v| [k.to_sym, v] }]
search = linkedin_client.profile(search_hash)
data = search.to_json
rescue LinkedIn::Errors::UnauthorizedError
redirect "/auth"
rescue Exception => e
return e.to_json
end
return data
end
get "/company" do
begin
search_hash = Hash[params.map{ |k, v| [k.to_sym, v] }]
search = linkedin_client.search(search_hash, type ='company')
data = search.to_json
rescue LinkedIn::Errors::UnauthorizedError
redirect "/auth"
rescue Exception => e
return e.to_json
end
return data
end