Sha256: 28f7bc8b40a4aa2f462984bb062512a2ce2ca79ee48a4e049ddb5961ef68e9ce

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'multi_xml'
require 'omniauth/oauth'

module OmniAuth
  module Strategies
    class Goodreads < OmniAuth::Strategies::OAuth
      def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
        client_options = {
          :site => 'http://www.goodreads.com',
        }
        @consumer_key = consumer_key
        super(app, :goodreads, consumer_key, consumer_secret, client_options, options, &block)
      end

      def auth_hash
        hash = user_info(@access_token)

        OmniAuth::Utils.deep_merge(
          super, {
            'uid' => hash.delete('id'),
            'user_info' => hash,
          }
        )
      end

      def user_info(access_token)
        authenticated_user = MultiXml.parse(access_token.get('/api/auth_user').body)
        id = authenticated_user['GoodreadsResponse']['user']['id'].to_i
        response_doc = MultiXml.parse(access_token.get("/user/show/#{id}.xml?key=#{@consumer_key}").body)
        user = response_doc['GoodreadsResponse']['user']

        hash = {
          'id' => id,
          'name' => user['name'],
          'user_name' => user['user_name'],
          'image_url' => user['image_url'],
          'about' => user['about'],
          'location' => user['location'],
          'website' => user['website'],
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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