Sha256: 2744ad6156b832ade95501e4d571658d3182902671d2b8053dc80c6e4e52567e

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

require 'nokogiri'
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_hash(@access_token)

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

      def user_hash(access_token)
        authenticated_user = Nokogiri::XML::Document.parse(@access_token.get('/api/auth_user').body)
        id = authenticated_user.xpath('GoodreadsResponse/user').attribute('id').value.to_i
        response_doc = Nokogiri::XML::Document.parse(open("http://www.goodreads.com/user/show/#{id}.xml?key=#{@consumer_key}").read)
        user = response_doc.xpath('GoodreadsResponse/user')

        hash = {
          'id' => id,
          'name' => user.xpath('name').text,
          'user_name' => user.xpath('user_name').text,
          'image_url' => user.xpath('image_url').text,
          'about' => user.xpath('about').text,
          'location' => user.xpath('location').text,
          'website' => user.xpath('website').text,
        }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
oa-oauth-0.2.4 lib/omniauth/strategies/goodreads.rb
oa-oauth-0.2.3 lib/omniauth/strategies/goodreads.rb
oa-oauth-0.2.2 lib/omniauth/strategies/goodreads.rb
oa-oauth-0.2.1 lib/omniauth/strategies/goodreads.rb
oa-oauth-0.2.0 lib/omniauth/strategies/goodreads.rb
oa-oauth-0.2.0.beta5 lib/omniauth/strategies/goodreads.rb
oa-oauth-0.2.0.beta4 lib/omniauth/strategies/goodreads.rb
oa-oauth-0.2.0.beta3 lib/omniauth/strategies/goodreads.rb
oa-oauth-0.2.0.beta2 lib/omniauth/strategies/goodreads.rb