rails linkedin gem:如何扫描所有用户字段的空白

时间:2012-08-06 19:08:53

标签: ruby-on-rails linkedin

快速提问:我正在使用linkedin gem来提取用户数据,但如果用户的linkedin个人资料中的特定数据字段为空,我的应用会中断。有没有一种最佳的方法来扫描每个配置文件的所有数据字段中的空白,并只提取那些存在的以防止破坏?

这是我的auth_controller ...我知道它不是DRY而且需要重构。谢谢!

require 'linkedin'

class AuthController < ApplicationController

  def index
    client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
    request_token = client.request_token(:oauth_callback => 
                                      "http://#{request.host_with_port}/callback")
    session[:rtoken] = request_token.token
    session[:rsecret] = request_token.secret
    redirect_to client.request_token.authorize_url
  end

  def callback
    client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
    if session[:atoken].nil?
      pin = params[:oauth_verifier]
      atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
      session[:atoken] = atoken
      session[:asecret] = asecret
    else
      client.authorize_from_access(session[:atoken], session[:asecret])
    end

        current_user = client.profile(:fields => %w(positions educations))
        @user = current_user
        educations = current_user.educations.all
        positions = current_user.positions.all

        companies = current_user.positions.all.map{ |t| t.company }

        @current_company = companies[0]['name']
      @past_company_one = companies[1]['name']
      @past_company_two = companies[2]['name']
      @past_company_three = companies[3]['name']

      @current_industry = companies[0]['industry']
    @past_industry_one = companies[1]['industry']
    @past_industry_two = companies[2]['industry']
    @past_industry_three = companies[3]['industry']

        @first_name = client.profile(:fields => ["first_name"]).first_name
    @last_name = client.profile(:fields => ["last_name"]).last_name
    @headline = client.profile(:fields => ["headline"]).headline
        @picture = client.profile(:fields => ["picture-url"]).picture_url

        @school_one_name = educations[0]['school-name']
        @school_one_degree = educations[0]['degree']
    @school_one_field = educations[0]['field-of-study']
    @school_one_start = educations[0]['start-date']['year'].to_s
    @school_one_end = educations[0]['end-date']['year'].to_s

    @school_two_name = educations[1]['school-name']
        @school_two_degree = educations[1]['degree']
    @school_two_field = educations[1]['field-of-study']
    @school_two_start = educations[1]['start-date']['year'].to_s
    @school_two_end = educations[1]['end-date']['year'].to_s

    @current_title = positions[0]['title']
    @past_title_one = positions[1]['title']
    @past_title_two = positions[2]['title']
    @past_title_three = positions[3]['title']

    @current_start_date = Date::MONTHNAMES[positions[0]['start-date']['month']] + " " + positions[0]['start-date']['year'].to_s

    @past_start_date_one = Date::MONTHNAMES[positions[1]['start-date']['month']] + " " + positions[1]['start-date']['year'].to_s
    @past_end_date_one = Date::MONTHNAMES[positions[1]['end-date']['month']] + " " + positions[1]['end-date']['year'].to_s

    @past_start_date_two = Date::MONTHNAMES[positions[2]['start-date']['month']] + " " + positions[2]['start-date']['year'].to_s
    @past_end_date_two = Date::MONTHNAMES[positions[2]['end-date']['month']] + " " + positions[2]['end-date']['year'].to_s

    @past_start_date_three = Date::MONTHNAMES[positions[3]['start-date']['month']] + " " + positions[3]['start-date']['year'].to_s
    @past_end_date_three = Date::MONTHNAMES[positions[3]['end-date']['month']] + " " + positions[3]['end-date']['year'].to_s

  end
end

1 个答案:

答案 0 :(得分:0)

考虑到您当前的代码可能会在响应中突破任何意外值并假设它在上面的callback方法中发生,您可以考虑将quick'n'dirty异常处理应用到你的代码。

例如,只需将可能有问题的代码封装在begin / end块中并使用rescue子句来处理任何异常:

def callback
  client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])

  if session[:atoken].nil?
    # ...code
    else
    #...code
  end

  # start handling exceptions here
  begin
    # ...potentially offending code here
    current_user = client.profile(:fields => %w(positions educations))
    # ...more code
    @past_end_date_three = Date::MONTHNAMES[positions[3]['end-date']['month']] + " " + positions[3]['end-date']['year'].to_s
  rescue
    # oops, something happened:
    # ...your code to handle the exception here
  end

end