From c7a81d8e75ec709df573413866dfbb97e296a5de Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Mon, 17 Aug 2015 19:06:47 +0100 Subject: [PATCH] Invalid steam ids on the users API now returns null for steam attributes --- app/controllers/api/v1/users_controller.rb | 14 +++++++++++--- spec/controllers/api/v1/users_controller_spec.rb | 11 +++++++++++ spec/services/api/v1/users_collection_spec.rb | 4 ++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index ea0f56b..bae11dc 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -5,7 +5,7 @@ class Api::V1::UsersController < Api::V1::BaseController def show @user = User.find(params[:id]) - @steam = SteamCondenser::Community::SteamId.from_steam_id("STEAM_#{@user.steamid}") + @steam = steam_profile @user render json: { id: @user.id, @@ -15,8 +15,8 @@ class Api::V1::UsersController < Api::V1::BaseController avatar: @user.profile.avatar.url, admin: @user.admin?, steam: { - url: @steam.base_url, - nickname: @steam.nickname + url: @steam.nil? ? nil : @steam.base_url, + nickname: @steam.nil? ? nil : @steam.nickname }, bans: { gather: @user.banned?(Ban::TYPE_GATHER).present?, @@ -28,4 +28,12 @@ class Api::V1::UsersController < Api::V1::BaseController rescue ActiveRecord::RecordNotFound raise ActionController::RoutingError.new('User Not Found') end + + private + + def steam_profile user + SteamCondenser::Community::SteamId.from_steam_id("STEAM_#{user.steamid}") + rescue SteamCondenser::Error => e + return nil + end end diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 3a8d07e..4ea3f54 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -28,6 +28,17 @@ describe Api::V1::UsersController do expect(json['team']).to be_nil end + it 'returns data for users with invalid steam ids' do + @user.steamid = '0:0:000' + @user.save! + + get :show, id: @user.id + + expect(response).to be_success + expect(json['steam']['url']).to be_nil + expect(json['steam']['nickname']).to be_nil + end + it 'returns 404 if user does not exist' do expect { get :show, id: -1 diff --git a/spec/services/api/v1/users_collection_spec.rb b/spec/services/api/v1/users_collection_spec.rb index 9d29c02..62dfbd9 100644 --- a/spec/services/api/v1/users_collection_spec.rb +++ b/spec/services/api/v1/users_collection_spec.rb @@ -23,8 +23,8 @@ describe Api::V1::UsersCollection do expect(collection.execute_query.size).to eq(3) end - it 'returns 5 columns' do - expect(collection.execute_query.first.size).to eq(5) + it 'returns 6 columns' do + expect(collection.execute_query.first.size).to eq(6) end end end