Spaghetti code do not look

This commit is contained in:
Fijxu 2024-05-18 22:29:36 -04:00
parent 7bd38e0a24
commit 00640f3ab6
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
4 changed files with 154 additions and 101 deletions

View file

@ -15,15 +15,45 @@ require "../twitchapi/*"
module Users
def self.checkBanned(gql)
puts "tst2"
pp gql["userResultByLogin"]
pp gql["userResultByID"]
if gql["userResultByLogin"].nil?
return nil
elsif gql["userResultByID"].nil?
return nil
else
# puts "checkBanned: #{gql}"
# puts (typeof(gql))
# if gql["userResultByLogin"]
# return false
# if gql["userResultByLogin"]["result"]?
# return true
# else
# return false
# end
# elsif gql["userResultByID"]?
# return false
# if gql["userResultByID"]["reason"]?
# return true
# else
# return false
# end
# else
# return true
# end
# if gql["userResultByLogin"]["reason"]?
# return true
# elsif gql["userResultByID"]["reason"]?
# return true
# else
# return false
# end
# return false
if GqlAPI.urb().includes? "login"
if gql["userResultByLogin"]["reason"]?
return true
else
return false
end
elsif GqlAPI.urb().includes? "id"
if gql["userResultByID"]["reason"]?
return true
else
return false
end
end
end
@ -54,11 +84,14 @@ module Users
gql = GqlAPI.query(params)
panels = gql["user"]["panels"]
banned = checkBanned(gql)
pp gql
# puts banned
# puts ("urb #{GqlAPI.urb()}")
# pp gql
json_data = [
{
# "banned" => banned, # TODO: FIX BANNED
"banned" => banned,
"reason" => nil,
"displayName" => gql["user"]["displayName"],
"login" => gql["user"]["login"],
"id" => gql["user"]["id"],

View file

@ -38,4 +38,3 @@ class Config
return config
end
end

View file

@ -23,7 +23,7 @@ get "/twitch/user" do |env|
Users.parseData(params)
rescue ex
env.response.status_code = 401
err = { "error" => "#{ex.message}" }
err = {"error" => "#{ex.message}"}
err.to_json
end
elsif params.has_key?("id")
@ -31,17 +31,17 @@ get "/twitch/user" do |env|
Users.parseData(params)
rescue ex
env.response.status_code = 401
err = { "error" => "#{ex.message}" }
err = {"error" => "#{ex.message}"}
err.to_json
end
else
env.response.status_code = 401
err = { "error" => "Parameter 'login' or 'id' is missing" }
err = {"error" => "Parameter 'login' or 'id' is missing"}
err.to_json
end
else
env.response.status_code = 401
err = { "error" => "No query parameters found" }
err = {"error" => "No query parameters found"}
err.to_json
end
end

View file

@ -8,8 +8,13 @@ module GqlAPI
"Authorization" => "OAuth #{CONFIG.gqlOAuth}",
"Client-Id" => "#{CONFIG.gqlClientID}",
}
@@urb : String = "fuck"
def self.userResultBy (params)
def self.urb
return @@urb
end
def self.userResultBy(params)
if params.has_key?("id")
return %(userResultByID(id: "#{params["id"]}"))
else
@ -17,6 +22,14 @@ module GqlAPI
end
end
def self.loginOrID(params)
if params.has_key?("id")
return %(userResultByID(id: "#{params["id"]}")), %(user(id: "#{params["id"]}" lookupType: ALL))
else
return %(userResultByLogin(login: "#{params["login"]}")), %(user(login: "#{params["login"]}" lookupType: ALL))
end
end
# def self.channel (params)
# if params.has_key?("id")
# return %(channel(id: "#{params["id"]}"))
@ -25,7 +38,7 @@ module GqlAPI
# end
# end
def self.user (params)
def self.user(params)
if params.has_key?("id")
return %(user(id: "#{params["id"]}" lookupType: ALL))
else
@ -34,7 +47,7 @@ module GqlAPI
end
def self.query(params)
puts params
# puts params
# id = params["id"]
# data = { "query" => "{user(id:#{id}){
@ -54,9 +67,10 @@ module GqlAPI
# stream{averageFPS,bitrate,codec,createdAt,width,height,id,viewersCount,type,game{displayName}},
# lastBroadcast{game{displayName},id,startedAt,title}
# }}" }
@@urb = userResultBy(params)
query = %(
query {
#{userResultBy(params)} {
#{@@urb} {
... on UserDoesNotExist {
key
reason
@ -143,7 +157,12 @@ module GqlAPI
}
}
)
return JSON.parse(gqlReq(params, query))["data"]
# puts JSON.parse(gqlReqq)["data"]
# puts ("gqlReq JSON.parse typeof: #{typeof (gqlReqq)}")
# puts ("gqlReq JSON.parse typeof: #{typeof(JSON.parse(gqlReq(params, query))["data"])}")
# puts ("gqlReq JSON.parse.to-H typeof: #{typeof (gqlReqq.to_h)}")
# puts ((JSON.parse(gqlReq(params, query)))["data"])["userResultByID"]["reason"]?
return (JSON.parse(gqlReq(params, query)))["data"]
end
# def self.isBanned(id : String)
@ -167,9 +186,11 @@ module GqlAPI
def self.gqlReq(params, query)
data = {"query" => query}
# puts (typeof(data))
response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: @@headers, body: data.to_json)
if response.success?
# puts (typeof(response.body))
return (response.body)
else
raise "GQL Twitch API returned #{response.status_code}: #{response.body.to_s}"