Merge pull request #64 from Prommah/user-api-steamid

Allow querying of the user API with SteamIDs
This commit is contained in:
simplefl 2015-10-28 18:12:00 +01:00
commit b0b0770f10
2 changed files with 56 additions and 12 deletions

View file

@ -4,7 +4,19 @@ class Api::V1::UsersController < Api::V1::BaseController
end
def show
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

View file

@ -10,16 +10,15 @@ describe Api::V1::UsersController do
@user = create :user, :chris
end
it "returns user data" do
get :show, id: @user.id
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?)
def user_expectation(json, user)
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 +29,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!