0
快速問題:我使用linkedin gem來提取用戶數據,但是如果用戶的LinkedIn配置文件中某個特定的數據字段爲空,我的應用會中斷。有沒有一種最佳的方式來掃描每個配置文件的所有數據字段的空白,並只拉取那些存在以防止破壞?rails linkedin gem:如何掃描所有用戶字段的空虛
這裏是我的auth_controller ...我知道它不是乾的,需要重構。謝謝!
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
謝謝!說'開始'有違規的代碼。我希望'救援'可以忽略它並移動到linkedin個人資料上的下一個數據點(例如@past_end_date_four)。這個最好的方法是什麼? – keypulsations 2012-08-07 13:08:46