Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby如何解析这个响应以将用户的工作经验存储在数据库中_Ruby_Sinatra_Omniauth_Linkedin - Fatal编程技术网

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

Ruby如何解析这个响应以将用户的工作经验存储在数据库中,ruby,sinatra,omniauth,linkedin,Ruby,Sinatra,Omniauth,Linkedin,我正在使用OmniAuth LinkedIn来验证和存储用户信息。这是i YAML::dump(response.env['omniauth.auth'].extra.raw_info)时显示的输出 res = YAML::parse(response) 我的问题是:如何解析这个响应来存储用户的工作经验(列出的几家公司)和用户教育 --- !ruby/hash:OmniAuth::AuthHash educations: !ruby/hash:OmniAuth::AuthHash _tot

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

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

--- !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
%w(get post)。每个do |方法|
发送(方法“/auth/:provider/callback”)执行
response=(request.env['omniauth.auth'])。extra.raw\u info
res = YAML::parse(response)
“”+YAML::转储(响应)+“”+“

” 结束 结束
我会尝试以下方法:

puts res.inspect # show the nested bits via  stdout
                 # or through some other logging
然后您可以这样做:

res["lastname"] # -> Iglesias
res["educations]["id"] # -> 154175914
res["location"]["country"]...
没有ominauth我不能确定测试,但看起来您可以像这样访问元素


您可能想尝试一些解决方案,以便在sinatra周围获得调试器

我是ruby的新手,这让我发疯。我可以访问电子邮件地址、id和所有其他第一层属性,但我根本不知道如何迭代这些嵌套实体以提取我想要存储的所有信息,如工作经验等。请尝试YAML::parse(…)在yaml数据上,然后进行检查。您可能希望在irb中尝试进行检查。检查上面的代码块,我在我的method@PaulRubel-我将如何做你推荐的我尝试过的事情?提前感谢你的帮助。