2

我使用 OmniAuth 连接 Facebook。我正在提取各种信息,并且在只有一种类型的信息中提取信息没有问题。例如,要获取电子邮件地址,我只需将以下内容放在 user.rb 中:

user.email = auth["info"]["email"]

问题在于多个条目。例如,对于教育,有两个结果。一所是名为普纳胡学校的高中,另一所是名为伊利诺伊大学厄巴纳-香槟分校的学院。

education: 
    - !map:Hashie::Mash 
      school: !map:Hashie::Mash 
        id: "105510192816251"
        name: Punahou School
      type: High School
    - !map:Hashie::Mash 
      school: !map:Hashie::Mash 
        id: "163536409904"
        name: University of Illinois at Urbana-Champaign
      type: College

我能够使用以下方法拉出第一所学校:

if auth["extra"]["raw_info"]["education"]
    user.school = auth["extra"]["raw_info"]["education"][0]["school"]["name"]
end

这样做的问题是,它只吸引了第一所学校,即高中。我在这里有几个问题:

  1. 如果我只想要学院,我怎么能把它拉到伊利诺伊大学厄巴纳-香槟分校?如果有高中和大学,我希望代码只选择大学的任何学校。
  2. 现在,假设我想要高中和大学。我将如何拉两者以及如何标记 Punahou 是高中而 UIUC 是大学?
  3. 如果有多个大学,我将如何提取最近的大学入学?
4

1 回答 1

0

Had a similar question with OmniAuth and LinkedIn, came across your question and it helped me come up with a solution. LinkedIn's hash returns the three current positions in a form similar to Facebook's behavior above. Change [0] to [1] and it will pull the college in your example. Use separate lines for each entry you want. I found the LinkedIn hash returned the three current positions in order with the most recent entry being in the [0] position.

于 2014-05-14T17:20:05.163 回答