簡単な質問: ユーザー データを取得するために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