-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
65 lines (53 loc) · 1.98 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'sinatra'
require 'open-uri'
require 'json'
class MySinatraApp < Sinatra::Base
API_KEY = ENV['RIOT_API_KEY']
URL = "https://na1.api.riotgames.com/lol"
get "/" do
erb :index
end
get "/summoner/:summonerName" do
content_type :json
result = JSON.parse(open("#{URL}/summoner/v3/summoners/by-name/#{params['summonerName']}?api_key=#{API_KEY}").read)
result.to_json
end
get "/games/:accountId" do
content_type :json
result = []
matches = JSON.parse(open("#{URL}/match/v3/matchlists/by-account/#{params['accountId']}?endIndex=10&api_key=#{API_KEY}").read)
# For each match, we need to do another request to get the statistic
matches["matches"].each do |match|
matchInfo = JSON.parse(open("#{URL}/match/v3/matches/#{match['gameId']}?api_key=#{API_KEY}").read)
participantId = 0
# We need to know the participantId
matchInfo["participantIdentities"].each do |participant|
# Have to convert to string for comparaison
if participant["player"]["accountId"].to_s == params['accountId'].to_s
participantId = participant["participantId"]
break
end
end
# Now we can find the stats
matchInfo["participants"].each do |participant|
if participant["participantId"] == participantId
result << {
"gameId" => match['gameId'],
"championId" => participant["championId"],
"kills" => participant["stats"]["kills"],
"assists" => participant["stats"]["assists"],
"deaths" => participant["stats"]["deaths"],
"win" => participant["stats"]["win"]
}
break
end
end
end
result.to_json
end
get "/champion/:championId" do
content_type :json
result = JSON.parse(open("#{URL}/static-data/v3/champions/#{params['championId']}?locale=en_US&tags=image&api_key=#{API_KEY}").read)
result.to_json
end
end