Sha256: e26f7e125a0dcde2721ebf2f4ae291e9d5865fbcd81f606e5c90334234434db0

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

require 'omniauth/oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    # Authenticate to SoundCloud via OAuth2 and retrieve basic
    # user information.
    #
    # Usage:
    #    use OmniAuth::Strategies::SoundCloud, 'consumerkey', 'consumersecret'
    class SoundCloud < OmniAuth::Strategies::OAuth2
      def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
        client_options = {
          :site => 'https://api.soundcloud.com',
          :authorize_url => 'https://soundcloud.com/connect',
          :token_url => 'https://api.soundcloud.com/oauth2/token'
        }
        super(app, :soundcloud, consumer_key, consumer_secret, client_options, options, &block)
      end

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

      def user_info
        user_hash = self.user_hash
        {
          'name' => user_hash['full_name'],
          'nickname' => user_hash['username'],
          'location' => user_hash['city'],
          'description' => user_hash['description'],
          'image' => user_hash['avatar_url'],
          'urls' => {
            'Website' => user_hash['website'],
          },
        }
      end

      def user_hash
        @user_hash ||= MultiJson.decode(@access_token.get('/me.json').body)
      end

      # OAuth2 by default uses 'Bearer %s' in the header
      def build_access_token
        access_token = super
        access_token.options[:header_format] = "OAuth %s"
        access_token
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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