简体   繁体   中英

How do I used the linkedin gem to parse a profile in rails?

I am using the linkedin gem https://github.com/pengwynn/linkedin

I authorize using Omniauth and store the access token and secrets.

I then authorize by access with the client.

I appear to get something useful when I type client.profile -- but it looks like mostly Nokogiri but in a LinkedIn::Profile class.

How do I specifically access fields, and will I be able to use method calls from the View in rails or do I need to do all the parsing in the controller and pass those values to the View from there.

Example of how to access the profile image url, title, name, company that sort of thing once I have established client.profile would be great.

When I use the (:fields =>) I get back something like this:

#<LinkedIn::Profile:0xb682c72c @doc=#<Nokogiri::XML::Document:0x..fdb41630a name="document" children=[#<Nokogiri::XML::Element:0x..fdb415fae name="person" children=[#<Nokogiri::XML::Text:0x..fdb415d88 "\n  ">, #<Nokogiri::XML::Element:0x..fdb415d24 name="picture-url" children=[#<Nokogiri::XML::Text:0x..fdb415aae "http://media.linkedis:

I just want the string associated with the node "picture-url"...how do I do that?

From controller:

7   `def show`
  8     @user = User.find(params[:id]) 
  9     @client = LinkedIn::Client.new(ENV["LINKEDIN_KEY"], ENV["LINKEDIN_SECRET"]) 
 10     @client.authorize_from_access(@user.atoken, @user.asecret) 
 11     @client.profile(:id => @user.uid, :fields => ["picture-url", "headline"]) 
 12     
 13   end 

New error:

undefined method `downcase' for nil:NilClass

Here is a related question: " https://stackoverflow.com/questions/5821549/how-do-i-pass-aa-tag-through-ruby-to-linkedin-using-the-gem "

I did it by adding:

client.profile(:fields => [:positions]).positions

This would then allow me to access specific positions or fields without going into the raw xml, just using the methods in the gem. The gem works nicely once I get the format...

I suggest you get the latest version of linked_in gem from github. It uses Hashie/Mashie syntax which is much simpler than dealing with Nokogiri output and XPath. If youre using bundler add this to your Gemfile(removing any other linked_in gem reference)

gem 'linkedin', :git => "git://github.com/pengwynn/linkedin.git"

this version of the gem basically sticks the output of your Linked In search into a hash, so you would access your picture-url string as follows: profileHash["picture-url"]

PS if you do decide to stick with your version of linked_in gem, get familiarized with XPath syntax, you will need it. Based on the information you provided, the picture url string will be available via profileXML.xpath("//person/picture-url").first.text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM