Skip to content

Commit

Permalink
Adds first working example with Twitter strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Sep 28, 2011
0 parents commit 4ca8741
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'http://rubygems.org'

# Specify your gem's dependencies in omniauth-contrib.gemspec
gem 'omniauth', :git => 'git://github.com/intridea/omniauth.git'
gem 'omniauth-oauth', :git => 'git://github.com/intridea/omniauth-oauth.git'

gemspec

group :example do
gem 'sinatra'
end
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
26 changes: 26 additions & 0 deletions examples/sinatra.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rubygems'
require 'bundler'

Bundler.setup :default, :development, :example
require 'sinatra'
require 'omniauth'
require 'omniauth-contrib'

use Rack::Session::Cookie

use OmniAuth::Builder do
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
end

get '/' do
<<-HTML
<ul>
<li><a href='/auth/twitter'>Sign in with Twitter</a></li>
</ul>
HTML
end

get '/auth/:provider/callback' do
content_type 'text/plain'
request.env['omniauth.auth'].inspect
end
7 changes: 7 additions & 0 deletions lib/omniauth-contrib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "omniauth-contrib/version"

module OmniAuth
module Strategies
autoload :Twitter, 'omniauth/strategies/twitter'
end
end
5 changes: 5 additions & 0 deletions lib/omniauth-contrib/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OmniAuth
module Contrib
VERSION = "0.0.1"
end
end
43 changes: 43 additions & 0 deletions lib/omniauth/strategies/twitter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'omniauth-oauth'
require 'multi_json'

module OmniAuth
module Strategies
class Twitter < OmniAuth::Strategies::OAuth
option :name, 'twitter'
option :client_options, {:site => 'https://api.twitter.com'}
option :sign_in, true
option :force_sign_in, false

def initialize(*args)
super
options.client_options[:authorize_path] = '/oauth/authenticate' if options.sign_in?
options.authorize_params[:force_sign_in] = 'true' if options.force_sign_in?
end

def uid
access_token.params[:user_id]
end

def info
{
:nickname => raw_info['screen_name'],
:name => raw_info['name'],
:location => raw_info['location'],
:image => raw_info['profile_image_url'],
:description => raw_info['description'],
:urls => {
'Website' => raw_info['url'],
'Twitter' => 'http://twitter.com/' + raw_info['screen_name'],
}
}
end

def raw_info
@raw_info ||= MultiJson.decode(access_token.get('/1/account/verify_credentials.json').body)
rescue ::Errno::ETIMEDOUT
raise ::Timeout::Error
end
end
end
end
21 changes: 21 additions & 0 deletions omniauth-contrib.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/omniauth-contrib/version', __FILE__)

Gem::Specification.new do |gem|
gem.add_dependency 'omniauth', '~> 1.0.0.alpha'
gem.add_dependency 'omniauth-oauth', '~> 1.0.0.alpha'
gem.add_dependency 'multi_json'

gem.authors = ["TODO: Write your name"]
gem.email = ["michael@intridea.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""

gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "omniauth-contrib"
gem.require_paths = ["lib"]
gem.version = OmniAuth::Contrib::VERSION
end

0 comments on commit 4ca8741

Please sign in to comment.