How to parse Mash from LinkedIn to create a Ruby object

I used the LinkedIn gem by pengwynn to get authentication from LinkedIn. Everything works fine, and I get Mash in a callback that looks like this:

#<LinkedIn::Mash all=[#<LinkedIn::Mash company=#<LinkedIn::Mash id=1422 industry="Banking" name="Company" size="10,001+ employees" ticker="ABC" type="Public Company"> id=2851554 is_current=true start_date=#<LinkedIn::Mash month=12 year=2008> summary="" title="Boss">] total=1> 

How can I parse it with something similar to Rails options to create a new object from it?

Thanks.

+4
source share
2 answers

When you get a list of connections of any type from LinkedIn, you need to go to the list from all . On the object that you received from LinkedIn, you have {all, total} . total will give you the number of objects in the array, all will give you all the objects. Therefore, if you want to turn the first company into a hash, you would call object.all.first.to_hash . You can iterate through everything by doing object.all.each {|c| # your block} object.all.each {|c| # your block} .

If your own Rails models match the objects returned from the linked pearls, you can do:

 companies.all.each do |company| Company.create(company.to_hash) end 

If they do not display 1: 1, you can simply select the required fields:

 companies.all.each do |company| c = Company.new c.name = company.name c.year_founded = company.start_date.year c.ticker = company.ticker # etc. etc. etc. c.save end 
+3
source

You can simply call .to_hash to turn Mash into a hash (e.g. params ).

A source:

https://github.com/intridea/hashie/blob/master/lib/hashie/hash.rb

+2
source

Source: https://habr.com/ru/post/1382087/


All Articles