Sha256: fd396843ef5625ca2d01cdeb826fb83491c3f72d3bf57b9cb38188c7618a9000

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require 'omniauth/oauth'

module OmniAuth
  module Strategies
    # Authenticate to Tumblr via OAuth and retrieve basic
    # user information.
    #
    # Usage:
    #    use OmniAuth::Strategies::Tumblr, 'consumerkey', 'consumersecret'
    class Tumblr < OmniAuth::Strategies::OAuth
      # @option options [Boolean, true] :sign_in When true, use the "Sign in with Tumblr" flow instead of the authorization flow.
      def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
        client_options = {
          :site => 'http://www.tumblr.com',
        }
        client_options[:authorize_path] = '/oauth/authorize' unless options[:sign_in] == false
        super(app, :tumblr, consumer_key, consumer_secret, client_options, options, &block)
      end

      def auth_hash
        OmniAuth::Utils.deep_merge(
          super, {
            'uid' => user['name'],
            'user_info' => user_info,
            'extra' => {
              'user_hash' => user
            },
          }
        )
      end

      def user_info
        {
          'nickname' => user['name'],
          'name' => user['title'],
          'image' => user['avatar_url'],
          'urls' => {
            'website' => user['url'],
          }
        }
      end

      def user
        tumblelogs = user_hash['tumblr']['tumblelog']
        if tumblelogs.kind_of?(Array)
          @user ||= tumblelogs[0]
        else
          @user ||= tumblelogs
        end
      end

      def user_hash
        url = 'http://www.tumblr.com/api/authenticate'
        @user_hash ||= Hash.from_xml(@access_token.get(url).body)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
oa-oauth-0.3.2 lib/omniauth/strategies/oauth/tumblr.rb
oa-oauth-0.3.0 lib/omniauth/strategies/oauth/tumblr.rb
oa-oauth-0.3.0.rc3 lib/omniauth/strategies/oauth/tumblr.rb