Sha256: c977a451d92d3915e1af0f3b0254c8d9e94864b53bff426582d735a97b620e81

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

require 'omniauth/oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    #
    # Authenticate to TradeMe via OAuth and retrieve basic user information.
    # Usage:
    #    use OmniAuth::Strategies::TradeMe, 'consumerkey', 'consumersecret'
    #
    class TradeMe < OmniAuth::Strategies::OAuth
      def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
        client_options = {
          :access_token_path => '/Oauth/AccessToken',
          :authorize_path => '/Oauth/Authorize',
          :request_token_path => '/Oauth/RequestToken',
          :site => 'https://secure.trademe.co.nz',
        }
        super(app, :trademe, consumer_key, consumer_secret, client_options, options, &block)
      end

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

      # user info according to schema
      def user_info
        {
          'nickname' => user_hash['Nickname'],
          'first_name' => user_hash['FirstName'],
          'last_name' => user_hash['LastName'],
          'name' => [user_hash['FirstName'], user_hash['LastName']].reject{|n| n.nil? || n.empty?}.join(' '),
        }
      end

      # info as supplied by TradeMe user summary
      def user_hash
        @user_hash ||= MultiJson.decode(@access_token.get('https://api.trademe.co.nz/v1/MyTradeMe/Summary.json').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/trade_me.rb
oa-oauth-0.3.0 lib/omniauth/strategies/oauth/trade_me.rb
oa-oauth-0.3.0.rc3 lib/omniauth/strategies/oauth/trade_me.rb