-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
class Etf2lProfile | ||
attr_accessor :json | ||
|
||
def initialize(profile_body) | ||
@json = JSON.parse(profile_body) | ||
end | ||
|
||
def name | ||
json.dig('player', 'name') | ||
end | ||
|
||
def banned? | ||
active_bans.any? | ||
end | ||
|
||
def ban_reason | ||
return nil if active_bans.none? | ||
|
||
active_bans.map { |b| b['reason'] }.join(', ') | ||
end | ||
|
||
def ban_expires_at | ||
return nil if active_bans.none? | ||
|
||
active_bans.map { |b| Time.at(b['end']).to_date }.join(', ') | ||
end | ||
|
||
def self.fetch(steam_uid) | ||
response_body = Etf2lApi.profile(steam_uid) | ||
new(response_body) if response_body | ||
end | ||
|
||
private | ||
|
||
def active_bans | ||
@active_bans ||= begin | ||
now = Time.now.to_i | ||
bans = json.dig('player', 'bans') | ||
if bans&.any? | ||
bans.reject { |b| now > b['end'].to_i } | ||
else | ||
[] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class LeagueBan | ||
def self.fetch(steam_uid) | ||
klass = if SITE_HOST == 'serveme.tf' | ||
Etf2lProfile | ||
elsif SITE_HOST == 'na.serveme.tf' | ||
RglProfile | ||
end | ||
|
||
return unless klass | ||
|
||
profile = klass.fetch(steam_uid) | ||
profile.banned? && profile | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
class RglProfile | ||
attr_accessor :json | ||
|
||
def initialize(profile_body) | ||
@json = JSON.parse(profile_body) | ||
end | ||
|
||
def name | ||
json['name'] | ||
end | ||
|
||
def banned? | ||
json.dig('status', 'isBanned') == true | ||
end | ||
|
||
def ban_reason | ||
reason = json.dig('banInformation', 'reason') | ||
ActionView::Base.full_sanitizer.sanitize(reason) if reason | ||
end | ||
|
||
def ban_expires_at | ||
expires_at = json.dig('banInformation', 'endsAt') | ||
expires_at && Date.parse(expires_at) | ||
end | ||
|
||
def self.fetch(steam_uid) | ||
response_body = RglApi.profile(steam_uid) | ||
new(response_body) if response_body | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class Etf2lApi | ||
def self.profile(steam_uid) | ||
cached_profile_response(steam_uid) || fetch_profile(steam_uid) | ||
end | ||
|
||
def self.cached_profile_response(steam_uid) | ||
Rails.cache.read("etf2l_profile_#{steam_uid}") | ||
end | ||
|
||
def self.fetch_profile(steam_uid) | ||
response = etf2l_connection.get("player/#{steam_uid}") | ||
return unless response.success? | ||
|
||
Rails.cache.write("etf2l_profile_#{steam_uid}", response.body, expires_in: 1.day) | ||
|
||
response.body | ||
end | ||
|
||
def self.etf2l_connection | ||
Faraday.new(url: 'https://api-v2.etf2l.org/') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class RglApi | ||
def self.profile(steam_uid) | ||
cached_profile_response(steam_uid) || fetch_profile(steam_uid) | ||
end | ||
|
||
def self.cached_profile_response(steam_uid) | ||
Rails.cache.read("rgl_profile_#{steam_uid}") | ||
end | ||
|
||
def self.fetch_profile(steam_uid) | ||
response = rgl_connection.get("v0/profile/#{steam_uid}") | ||
return unless response.success? || response.status == 404 | ||
|
||
Rails.cache.write("rgl_profile_#{steam_uid}", response.body, expires_in: 1.day) | ||
|
||
response.body | ||
end | ||
|
||
def self.rgl_connection | ||
Faraday.new(url: 'https://api.rgl.gg/') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters