From 6c615c4eaa973d83778fc6d34157f823880765f1 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 27 Apr 2014 02:46:11 -0400 Subject: Work on the tournament creation page --- lib/sampling/riot_api.rb | 143 ++++++++++++++++++++++++----------------------- 1 file changed, 72 insertions(+), 71 deletions(-) (limited to 'lib') diff --git a/lib/sampling/riot_api.rb b/lib/sampling/riot_api.rb index 3de4185..d5e9c72 100644 --- a/lib/sampling/riot_api.rb +++ b/lib/sampling/riot_api.rb @@ -1,10 +1,72 @@ module Sampling module RiotApi + protected + def self.api_name + "prod.api.pvp.net/api/lol" + end + + protected + def self.api_key + ENV["RIOT_API_KEY"] + end + + protected + def self.region + ENV["RIOT_API_REGION"] + end + + protected + def self.url(request, args={}) + "https://prod.api.pvp.net/api/lol/#{region}/#{request % args.merge(args){|k,v|url_escape(v)}}?#{api_key}" + end + + protected + def self.url_escape(string) + URI::escape(string.to_s, /[^a-zA-Z0-9._~!$&'()*+,;=:@-]/) + end + + protected + def self.standardize(summoner_name) + summoner_name.to_s.downcase.gsub(' ', '') + end + + protected + class Job < ThrottledApiRequest + def initialize(request, args={}) + @url = Sampling::RiotApi::url(request, args) + super(api_name, 10.seconds, 10) + end + + def perform + response = open(@url) + status = response.status + data = JSON::parse(response.read) + + # Error codes that RIOT uses: + # "400"=>"Bad request" + # "401"=>"Unauthorized" + # "429"=>"Rate limit exceeded" + # "500"=>"Internal server error" + # "503"=>"Service unavailable" + # "404"=>"Not found" + # Should probably handle these better + if status[0] != "200" + raise "GET #{@url} => #{status.join(" ")}" + end + self.handle(data) + end + + def handle(data) + end + end + + ######################################################################## + ## # Return whether or not this sampling method works with the specified game. # Spoiler: It only works with League of Legends (or subclasses of it). public - def works_with?(game) + def self.works_with?(game) if api_key.nil? or region.nil? return false end @@ -19,17 +81,17 @@ module Sampling ## # This sampling method uses remote IDs public - def uses_remote? + def self.uses_remote? return true end ## # When given a summoner name for a user, figure out the summoner ID. public - def set_remote_name(user, game, summoner_name) + def self.set_remote_name(user, game, summoner_name) Delayed::Job.enqueue(UsernameJob.new(user, game, summoner_name), :queue => api_name) end - private + protected class UsernameJob < Job def initialize(user, game, summoner_name) @user_id = user.id @@ -57,21 +119,21 @@ module Sampling # When given data from RemoteUsername#value, give back a readable name to display. # Here, this is the summoner name. public - def get_remote_name(data) + def self.get_remote_name(data) data["name"] end ## # Fetch all the statistics for a match. public - def sampling_start(match) + def self.sampling_start(match) @match.teams.each do |team| team.users.each do |user| Delayed::Job.enqueue(MatchJob.new(user, match), :queue => api_name) end end end - private + protected class FetchStatisticsJob < Job def initialize(user, match) @user_id = user.id @@ -92,78 +154,17 @@ module Sampling end public - def sampling_done?(match) + def self.sampling_done?(match) # TODO end public - def render_user_interaction(match, user) + def self.render_user_interaction(match, user) return "" end public - def handle_user_interaction(match, user) - end - - ######################################################################## - - private - def api_name - "prod.api.pvp.net/api/lol" - end - - private - def api_key - ENV["RIOT_API_KEY"] - end - - private - def region - ENV["RIOT_API_REGION"] - end - - private - def url(request, args={}) - "https://prod.api.pvp.net/api/lol/#{region}/#{request % args.merge(args){|k,v|url_escape(v)}}?#{api_key}" - end - - private - def url_escape(string) - URI::escape(string.to_s, /[^a-zA-Z0-9._~!$&'()*+,;=:@-]/) - end - - private - def standardize(summoner_name) - summoner_name.to_s.downcase.gsub(' ', '') - end - - private - class Job < ThrottledApiRequest.new(api_name, 10.seconds, 10) - def initialize(request, args={}) - @url = Sampling::RiotApi::url(request, args) - end - - def perform - response = open(@url) - status = response.status - data = JSON::parse(response.read) - - # Error codes that RIOT uses: - # "400"=>"Bad request" - # "401"=>"Unauthorized" - # "429"=>"Rate limit exceeded" - # "500"=>"Internal server error" - # "503"=>"Service unavailable" - # "404"=>"Not found" - # Should probably handle these better - if status[0] != "200" - raise "GET #{@url} => #{status.join(" ")}" - end - self.handle(data) - end - - def handle(data) - end + def self.handle_user_interaction(match, user) end end end -- cgit v1.2.3