Sha256: 474d4b94336ea85b8e91ccb31a444e54263c7e3c030a97909db8d9a580cd961d

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'omniauth/core'
require 'omniauth/strategies/oauth'
require 'nokogiri'

module OmniAuth
  module Strategies
    class LinkedIn < OmniAuth::Strategies::OAuth
      def initialize(app, consumer_key, consumer_secret)
        super(app, :linked_in, consumer_key, consumer_secret,
                :site => 'https://api.linkedin.com',
                :request_token_path => '/uas/oauth/requestToken',
                :access_token_path => '/uas/oauth/accessToken',
                :authorize_path => '/uas/oauth/authorize',
                :scheme => :header)
      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)
        person = Nokogiri::XML::Document.parse(@access_token.get('/v1/people/~:(id,first-name,last-name,headline,member-url-resources,picture-url,location)').body).xpath('person')
        
        hash = {
          'id' => person.xpath('id').text,
          'first_name' => person.xpath('first-name').text,
          'last_name' => person.xpath('last-name').text,
          'location' => person.xpath('location/name').text,
          'image' => person.xpath('picture-url').text,
          'description' => person.xpath('headline').text,
          'urls' => person.css('member-url-resources member-url').inject({}) do |hash,element|
            hash[element.xpath('name').text] = element.xpath('url').text
            hash
          end
        }
        
        hash[:name] = "#{hash['first_name']} #{hash['last_name']}"
        hash
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oa-oauth-0.0.1 lib/omniauth/strategies/linked_in.rb