Sha256: 9969047213ea4039888b2375b94f3b8969dec723df1ee9d6bb261ed42b5a95ae

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'omniauth/oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    # 
    # Authenticate to SoundCloud via OAuth and retrieve basic
    # user information.
    #
    # Usage:
    #
    #    use OmniAuth::Strategies::SoundCloud, 'consumerkey', 'consumersecret'
    #
    
    class SoundCloud < OmniAuth::Strategies::OAuth
      def initialize(app, consumer_key, consumer_secret)
        super(app, :soundcloud, consumer_key, consumer_secret, :site => 'https://api.soundcloud.com')
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oa-oauth-0.1.6 lib/omniauth/strategies/sound_cloud.rb