Sha256: 29e05f59b02c9508338b76d3ce903765b55ca7ccd920b8906973853a8fe768c2

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

require 'nokogiri'
require 'omniauth/oauth'

module OmniAuth
  module Strategies
    class LinkedIn < OmniAuth::Strategies::OAuth
      def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
        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}, 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)
        person = Nokogiri::XML::Document.parse(@access_token.get('/v1/people/~:(id,first-name,last-name,headline,member-url-resources,picture-url,location,public-profile-url)').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 |h,element|
            h[element.xpath('name').text] = element.xpath('url').text
            h
          end
        }
        
        hash['urls']['LinkedIn'] = person.xpath('public-profile-url').text
        hash['name'] = "#{hash['first_name']} #{hash['last_name']}"
        
        hash
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
oa-oauth-0.2.0.beta4 lib/omniauth/strategies/linked_in.rb
oa-oauth-0.2.0.beta3 lib/omniauth/strategies/linked_in.rb
oa-oauth-0.2.0.beta2 lib/omniauth/strategies/linked_in.rb
oa-oauth-0.2.0.beta1 lib/omniauth/strategies/linked_in.rb