Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added Sinatra example #93

Merged
merged 1 commit into from

2 participants

@amscotti

Added a small one file Sinatra example

@sferik sferik merged commit 070be86 into hexgnu:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 16, 2011
  1. @amscotti

    Added sinatra example

    amscotti authored
This page is out of date. Refresh to see the latest.
Showing with 82 additions and 0 deletions.
  1. +82 −0 examples/sinatra.rb
View
82 examples/sinatra.rb
@@ -0,0 +1,82 @@
+require "rubygems"
+require "haml"
+require "sinatra"
+require "linkedin"
+
+enable :sessions
+
+helpers do
+ def login?
+ if session[:atoken].nil?
+ return false
+ else
+ return true
+ end
+ end
+
+ def profile
+ unless session[:atoken].nil?
+ client = LinkedIn::Client.new(settings.api, settings.secret)
+ client.authorize_from_access(session[:atoken], session[:asecret])
+ return client.profile
+ end
+ end
+
+ def connections
+ unless session[:atoken].nil?
+ client = LinkedIn::Client.new(settings.api, settings.secret)
+ client.authorize_from_access(session[:atoken], session[:asecret])
+ return client.connections
+ end
+ end
+
+end
+
+configure do
+ # get your api keys at https://www.linkedin.com/secure/developer
+ set :api, "your_api_key"
+ set :secret, "your_secret"
+end
+
+get "/" do
+ haml :index
+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 "/"
+end
+
+
+__END__
+@@index
+-if login?
+ %p Welcome #{profile.first_name}!
+ %a{:href => "/auth/logout"} Logout
+ %p= profile.headline
+ %br
+ %div= "You have #{connections.total} connections!"
+ -connections.all.each do |c|
+ %div= "#{c.first_name} #{c.last_name} - #{c.headline}"
+-else
+ %a{:href => "/auth"} Login using LinkedIn
Something went wrong with that request. Please try again.