开发者

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

开发者 https://www.devze.com 2023-02-28 02:36 出处:网络
I am using the linkedin gem https://github.com/pengwynn/linkedin I authorize using Omniauth and store the access token and secrets.

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-a-a-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"]

P.S. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消