Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added followers lookup

  • Loading branch information...
commit f23b956442941365014c877799f271cfda540281 1 parent 59015da
Chris Matthieu authored
Showing with 37 additions and 20 deletions.
  1. +1 −0  Gemfile
  2. +1 −0  Gemfile.lock
  3. +35 −20 githubchat.rb
1  Gemfile
View
@@ -2,3 +2,4 @@ source 'http://rubygems.org'
gem 'sinatra'
gem 'omniauth'
+gem 'rest-client'
1  Gemfile.lock
View
@@ -70,4 +70,5 @@ PLATFORMS
DEPENDENCIES
omniauth
+ rest-client
sinatra
55 githubchat.rb
View
@@ -1,11 +1,8 @@
-begin
- require 'sinatra'
- require 'omniauth'
-rescue LoadError
- require 'rubygems'
- require 'sinatra'
- require 'omniauth'
-end
+require 'rubygems'
+require 'sinatra'
+require 'omniauth'
+require 'rest-client'
+require 'json'
use Rack::Session::Cookie
use OmniAuth::Builder do
@@ -13,28 +10,46 @@
end
get '/' do
- <<-HTML
- <a href='/auth/github'>Sign in with GitHub</a>
- HTML
-end
+ if session[:user]
+ response = RestClient.get 'https://api.github.com/user/followers?access_token=' + session[:token]
+ followers = JSON.parse(response.body)
+
+ html = "Hello #{session[:user][:name]}! (<a href=\"/logout\">logout</a>)<br/><p>Your followers:</p>"
+
+ followers.each do |follower|
+ html << "<img src=\"#{follower['avatar_url']}\" width=\"25\"> #{follower['login']}<br/>" rescue ""
+ end
+
+ html
-post '/auth/:name/callback' do
- omniauth = request.env['omniauth.auth']
- # do whatever you want with the information!
+ else
+ <<-HTML
+ <a href='/auth/github'><img src="/login-with-github.png"></a>
+ HTML
+ end
+
end
+
get '/auth/:name/callback' do
omniauth = request.env['omniauth.auth']
+ puts " *** omniauth: " + omniauth.inspect;
# create a new hash
@authhash = Hash.new
- omniauth['user_info']['email'] ? @authhash[:email] = omniauth['user_info']['email'] : @authhash[:email] = ''
- omniauth['user_info']['name'] ? @authhash[:name] = omniauth['user_info']['name'] : @authhash[:name] = ''
- omniauth['extra']['user_hash']['id'] ? @authhash[:uid] = omniauth['extra']['user_hash']['id'].to_s : @authhash[:uid] = ''
- omniauth['provider'] ? @authhash[:provider] = omniauth['provider'] : @authhash[:provider] = ''
+ omniauth['user_info']['email'] ? @authhash[:email] = omniauth['user_info']['email'] : @authhash[:email] = ''
+ omniauth['user_info']['name'] ? @authhash[:name] = omniauth['user_info']['name'] : @authhash[:name] = ''
+ omniauth['extra']['user_hash']['id'] ? @authhash[:uid] = omniauth['extra']['user_hash']['id'].to_s : @authhash[:uid] = ''
+ omniauth['provider'] ? @authhash[:provider] = omniauth['provider'] : @authhash[:provider] = ''
- # session[:user] = @authhash
+ session[:user] = @authhash
+ session[:token] = omniauth['credentials']['token']
+ redirect "/"
end
+get '/logout' do
+ session[:user] = nil
+ redirect "/"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.