Created
May 20, 2015 22:09
-
-
Save csaunders/f0aab6337e1f358a4483 to your computer and use it in GitHub Desktop.
A Basic Riot API Client using Rest-Client + Command Line Wrapper to show how it's used
This file contains hidden or 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
require 'rest-client' | |
require 'json' | |
module RiotAPI | |
API_ROOT = 'https://na.api.pvp.net/api/lol/na/' | |
class Client | |
def initialize(api_key) | |
@api_key = api_key | |
@api = RestClient::Resource.new(API_ROOT, verify_ssl: OpenSSL::SSL::VERIFY_NONE) | |
end | |
def get(path) | |
response = @api["#{path}?api_key=#{@api_key}"].get | |
JSON.parse(response) | |
end | |
end | |
class Summoner | |
def self.find_summoners(client, names) | |
names_csv = names.join(',') | |
client.get("v1.4/summoner/by-name/#{names_csv}") | |
end | |
end | |
class Stats | |
def self.ranked(client, summoner_id) | |
stats(client, summoner_id, 'ranked') | |
end | |
def self.summary(client, summoner_id) | |
stats(client, summoner_id, 'summary') | |
end | |
def self.stats(client, summoner_id, type) | |
client.get("v1.3/stats/by-summoner/#{summoner_id}/#{type}") | |
end | |
end | |
end |
This file contains hidden or 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
require './riotapi' | |
require 'pp' # pretty printing | |
puts "Enter your API Key:" | |
api_key = gets.chomp.strip | |
@client = RiotAPI::Client.new(api_key) | |
def client | |
@client | |
end | |
def read_command | |
commands = { | |
'f' => '[f]ind a summoner', | |
'r' => '[r]anking of a summoner', | |
's' => '[s]ummary of a summoner', | |
'q' => '[q]uit' | |
} | |
puts "enter a command:" | |
commands.each { |command, description| puts description } | |
command = gets.chomp.strip | |
if commands.keys.include?(command) | |
command | |
else | |
puts "'#{command}'' is not a valid command. Please try again." | |
read_command | |
end | |
end | |
def find_summoner | |
puts "Enter a summoner name:" | |
summoner = gets.chomp.strip | |
puts "Finding details for '#{summoner}'" | |
pp RiotAPI::Summoner.find_summoners(client, Array[summoner]) | |
end | |
def get_stats_ranked | |
puts "Enter a summoner ID:" | |
id = gets.chomp.strip | |
puts "Finding ranked statistics for SummonerID '#{id}'" | |
pp RiotAPI::Stats.ranked(client, id) | |
end | |
def get_stats_summary | |
puts "Enter a summoner ID:" | |
id = gets.chomp.strip | |
puts "Finding summarized statistics for SummonerID '#{id}'" | |
pp RiotAPI::Stats.summary(client, id) | |
end | |
def process(command) | |
case command.to_sym | |
when :f then find_summoner | |
when :r then get_stats_ranked | |
when :s then get_stats_summary | |
when :q then exit(0) | |
else puts "hmm... that's weird. #{command} isn't allowed!" | |
end | |
end | |
# Read / Eval / Print / Loop | |
# This is our console program, kinda like IRB | |
# but not really | |
while true | |
command = read_command | |
process(command) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment