Sha256: f659a64f0bb0ddcb9383de94f66cf9c32e267b761251e9eff5925872ed50b74f

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

require 'omniauth/oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    # 
    # Authenticate to Twitter via OAuth and retrieve basic
    # user information.
    #
    # Usage:
    #
    #    use OmniAuth::Strategies::Twitter, 'consumerkey', 'consumersecret'
    #
    class Twitter < OmniAuth::Strategies::OAuth
      # Initialize the middleware
      #
      # @option options [Boolean, true] :sign_in When true, use the "Sign in with Twitter" flow instead of the authorization flow.
      def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
        client_options = {
          :site => 'https://api.twitter.com'
        }
        
        client_options[:authorize_path] = '/oauth/authenticate' unless options[:sign_in] == false
        super(app, :twitter, consumer_key, consumer_secret, client_options, options)
      end
      
      def auth_hash
        OmniAuth::Utils.deep_merge(super, {
          'uid' => @access_token.params[:user_id],
          'user_info' => user_info,
          'extra' => {'user_hash' => user_hash}
        })
      end
      
      def user_info
        user_hash = self.user_hash
        
        {
          'nickname' => user_hash['screen_name'],
          'name' => user_hash['name'],
          'location' => user_hash['location'],
          'image' => user_hash['profile_image_url'],
          'description' => user_hash['description'],
          'urls' => {
            'Website' => user_hash['url'],
            'Twitter' => 'http://twitter.com/' + user_hash['screen_name']
          }
        }
      end
      
      def user_hash
        @user_hash ||= MultiJson.decode(@access_token.get('/1/account/verify_credentials.json').body)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
oa-oauth-0.2.0 lib/omniauth/strategies/twitter.rb
oa-oauth-0.2.0.beta5 lib/omniauth/strategies/twitter.rb
oa-oauth-0.2.0.beta4 lib/omniauth/strategies/twitter.rb
oa-oauth-0.2.0.beta3 lib/omniauth/strategies/twitter.rb
oa-oauth-0.2.0.beta2 lib/omniauth/strategies/twitter.rb
oa-oauth-0.2.0.beta1 lib/omniauth/strategies/twitter.rb