Sha256: d1ddf275eb820d85fbae10dd504a11484b4634d2b2a2327bc95eaeda17ffa37b

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'omniauth/oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    # Authenticate to Rdio via OAuth and retrieve basic user information.
    #
    # Usage:
    #    use OmniAuth::Strategies::Rdio, 'consumerkey', 'consumersecret'
    class Rdio < OmniAuth::Strategies::OAuth
      def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
        client_options = {
          :access_token_path => '/oauth/access_token',
          :authorize_url => 'https://www.rdio.com/oauth/authorize',
          :request_token_path => '/oauth/request_token',
          :site => 'http://api.rdio.com',
        }
        super(app, :rdio, consumer_key, consumer_secret, client_options, options, &block)
      end

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

      def user_info
        user = user_hash
        {
          'nickname' => user['username'],
          'first_name' => user['firstName'],
          'last_name' => user['lastName'],
          'name' => "#{user['firstName']} #{user['lastName']}"
        }
      end

      def user_hash
        @user_hash ||= MultiJson.decode(@access_token.post('http://api.rdio.com/1/', {:method => 'currentUser', :extras => 'username'}).body)['result']
      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/rdio.rb
oa-oauth-0.3.0 lib/omniauth/strategies/oauth/rdio.rb
oa-oauth-0.3.0.rc3 lib/omniauth/strategies/oauth/rdio.rb