Ruby如何解析此响应以将用户工作经验存储在数据库中

时间:2013-07-09 22:10:30

标签: ruby sinatra omniauth linkedin

我正在使用OmniAuth LinkedIn来验证和存储用户信息。这是i 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

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

%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

1 个答案:

答案 0 :(得分: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的调试器,也许是rackshAccessing the irb in a modular Sinatra application