0

我正在使用 OmniAuth LinkedIn 来验证和存储用户信息。这是我 YAML::dump(response.env['omniauth.auth'].extra.raw_info) 时显示的输出

我的问题:我如何解析这个响应来存储用户的工作经验(列出的几家公司)和用户的教育?

--- !ruby/hash:OmniAuth::AuthHash
educations: !ruby/hash:OmniAuth::AuthHash
  _total: 1
  values:
  - !ruby/hash:OmniAuth::AuthHash
    degree: ! '"BS"'
    endDate: !ruby/hash:OmniAuth::AuthHash
      year: 2014
    fieldOfStudy: Information Technology
    id: 154175914
    schoolName: Florida State University
    startDate: !ruby/hash:OmniAuth::AuthHash
      year: 2012
emailAddress: mci12@my.fsu.edu
firstName: Michael
headline: Co founder/CTO at Prept.co
id: 9rFcoUgX7u
industry: Information Technology and Services
lastName: Iglesias
location: !ruby/hash:OmniAuth::AuthHash
  country: !ruby/hash:OmniAuth::AuthHash
    code: us
  name: Miami/Fort Lauderdale Area
pictureUrl: http://m.c.lnkd.licdn.com/mpr/mprx/0_eyd5KS_5QNRWff1-HxoVKfQQX-yWSdi-EgjVKaLVRAeMtSqtXsJWOmlqospnaaGOWxIUxWULYrkb
positions: !ruby/hash:OmniAuth::AuthHash
  _total: 4
  values:
  - !ruby/hash:OmniAuth::AuthHash
    company: !ruby/hash:OmniAuth::AuthHash
      industry: Information Technology and Services
      name: InterviewChamp.co
    id: 409892806
    isCurrent: true
    startDate: !ruby/hash:OmniAuth::AuthHash
      month: 4
      year: 2013
    summary: Online marketplace where job seekers can connect with industry professionals
  in their desired fields for online video conferencing practice interviews. Job
  seekers are able to practice case, behavioral, and technical interviews to help
  them prepare and excel at real interviews.
    title: Founder/CTO
  - !ruby/hash:OmniAuth::AuthHash
    company: !ruby/hash:OmniAuth::AuthHash
      industry: Information Technology and Services
      name: Open Momento Web Development
    id: 385398346
    isCurrent: true
    startDate: !ruby/hash:OmniAuth::AuthHash
      month: 5
      year: 2012
    title: Software Engineer
  - !ruby/hash:OmniAuth::AuthHash
     company: !ruby/hash:OmniAuth::AuthHash
      industry: Information Technology and Services
      name: LetUsDorm.com
    id: 385404241
    isCurrent: true
    startDate: !ruby/hash:OmniAuth::AuthHash
      month: 1
      year: 2012
    title: Founder/CEO
  - !ruby/hash:OmniAuth::AuthHash
    company: !ruby/hash:OmniAuth::AuthHash
      id: 2780
       industry: Information Technology and Services
      name: Amadeus
      size: 10,001+ employees
      type: Public Company
    endDate: !ruby/hash:OmniAuth::AuthHash
      month: 5
      year: 2012
    id: 385401346
    isCurrent: false
    startDate: !ruby/hash:OmniAuth::AuthHash
      month: 5
      year: 2011
    title: IT Intern
publicProfileUrl: http://www.linkedin.com/in/iglesiasm

这就是我在基于 sinatra 的 app.rb 文件中的内容...

%w(get post).each do |method|
    send(method, "/auth/:provider/callback") do

        response =  (request.env['omniauth.auth']).extra.raw_info
        "<pre>" + YAML::dump(response) + "</pre>" + "<br /><br />" 
    end
end
4

1 回答 1

0

我会尝试以下方法:

res = YAML::parse(response)

然后你可以这样做:

puts res.inspect # show the nested bits via  stdout
                 # or through some other logging

没有 ominauth 我无法确定测试,但看起来您可以访问这样的元素。

res["lastname"] # -> Iglesias
res["educations]["id"] # -> 154175914
res["location"]["country"]...

您可能想尝试一些解决方案来获得围绕 sinatra 的调试器,也许是racksh在模块化 Sinatra 应用程序中访问 irb

于 2013-07-10T12:27:31.977 回答