0

快速提问:我正在使用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
4

1 回答 1

0

考虑到您当前的代码可能会因响应中的任何意外值而中断,并假设它发生在您的上述callback方法中,您可以考虑对您的代码应用快速'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
于 2012-08-07T07:37:33.027 回答