From 7eb0bf2b4526a651a8480346363bbdeca4a8e4ec Mon Sep 17 00:00:00 2001 From: Prommah Date: Wed, 28 Oct 2015 14:31:05 +0000 Subject: [PATCH 1/3] Add support for querying users via steamid in the user API --- app/controllers/api/v1/users_controller.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index acbb10e..6c02b0b 100755 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -4,7 +4,19 @@ class Api::V1::UsersController < Api::V1::BaseController end def show - @user = User.find(params[:id]) + if params[:format].nil? || params[:format] == "id" + @user = User.find(params[:id]) + elsif params[:format] == "steamid" + steamid_i = params[:id].to_i + @user = User.first(conditions: { steamid: format("0:%d:%d", steamid_i % 2, steamid_i >> 1) }) + elsif params[:format] == "steamidstr" + @user = User.first(conditions: { steamid: params[:id] }) + end + + if @user.nil? + raise ActionController::RoutingError.new("User Not Found") + end + if @user.steamid.present? @steam = steam_profile @user end @@ -16,6 +28,8 @@ class Api::V1::UsersController < Api::V1::BaseController time_zone: @user.time_zone, avatar: @user.profile.avatar.url, admin: @user.admin?, + referee: @user.ref?, + caster: @user.caster?, moderator: @user.gather_moderator?, steam: @user.steamid.nil? ? nil : { id: @user.steamid, @@ -30,7 +44,7 @@ class Api::V1::UsersController < Api::V1::BaseController team: @user.team.present? ? { id: @user.team.id, name: @user.team.name } : nil } rescue ActiveRecord::RecordNotFound - raise ActionController::RoutingError.new('User Not Found') + raise ActionController::RoutingError.new("User Not Found") end private From 237d80d6c57caac28aaab3441abcf4d950a3e55a Mon Sep 17 00:00:00 2001 From: Prommah Date: Wed, 28 Oct 2015 16:11:01 +0000 Subject: [PATCH 2/3] Update user API spec for new values & SteamID queries --- .../api/v1/users_controller_spec.rb | 49 +++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 7bf091e..f802ae4 100755 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -10,16 +10,16 @@ describe Api::V1::UsersController do @user = create :user, :chris end - it "returns user data" do - get :show, id: @user.id - + def user_expectation(json, user) expect(response).to be_success - expect(json["id"]).to eq(@user.id) - expect(json["username"]).to eq(@user.username) - expect(json["country"]).to eq(@user.country) - expect(json["time_zone"]).to eq(@user.time_zone) - expect(json["admin"]).to eq(@user.admin?) - expect(json["moderator"]).to eq(@user.gather_moderator?) + expect(json["id"]).to eq(user.id) + expect(json["username"]).to eq(user.username) + expect(json["country"]).to eq(user.country) + expect(json["time_zone"]).to eq(user.time_zone) + expect(json["admin"]).to eq(user.admin?) + expect(json["referee"]).to eq(user.ref?) + expect(json["caster"]).to eq(user.caster?) + expect(json["moderator"]).to eq(user.gather_moderator?) expect(json).to have_key("steam") expect(json["steam"]).to have_key("id") expect(json["steam"]).to have_key("url") @@ -30,6 +30,37 @@ describe Api::V1::UsersController do expect(json["team"]).to be_nil end + it "returns user data" do + get :show, id: @user.id + + expect(response).to be_success + user_expectation(json, @user) + end + + it "returns user data for query with id specified as format" do + get :show, id: @user.id, format: "id" + + expect(response).to be_success + user_expectation(json, @user) + end + + it "returns user data for a numeric steamid query" do + m = @user.steamid.match(/\A0:([01]):(\d{1,10})\Z/) + steamid = (m[2].to_i << 1) + m[1].to_i + + get :show, id: steamid, format: "steamid" + + expect(response).to be_success + user_expectation(json, @user) + end + + it "returns user data for a string steamid query" do + get :show, id: @user.steamid, format: "steamidstr" + + expect(response).to be_success + user_expectation(json, @user) + end + it "returns nulled steam data for users who had invalid steam ids" do @user.steamid = nil @user.save! From f17271e35ed6bf9e6820af2761ef690c3ada30a8 Mon Sep 17 00:00:00 2001 From: Prommah Date: Wed, 28 Oct 2015 16:44:00 +0000 Subject: [PATCH 3/3] Update users_controller_spec.rb --- spec/controllers/api/v1/users_controller_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index f802ae4..2dd4380 100755 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -11,7 +11,6 @@ describe Api::V1::UsersController do end def user_expectation(json, user) - expect(response).to be_success expect(json["id"]).to eq(user.id) expect(json["username"]).to eq(user.username) expect(json["country"]).to eq(user.country)