game module

This commit is contained in:
danijoo
2013-12-13 10:08:36 +01:00
parent c5ae552fe8
commit f4bea4fe9f
7 changed files with 137 additions and 21 deletions

View File

@@ -2,10 +2,11 @@ require 'rest_client'
require 'JSON'
require 'sightstone/modules/champion_module'
require 'sightstone/modules/summoner_module'
require 'sightstone/modules/game_module'
class Sightstone
attr_accessor :region, :api_key
attr_accessor :summoner, :champion
attr_accessor :summoner, :champion, :game
def initialize(api_key, region)
@api_key = api_key
@@ -13,6 +14,7 @@ class Sightstone
@summoner = SummonerModule.new(self)
@champion = ChampionModule.new(self)
@game = GameModule.new(self)
end
end

View File

@@ -0,0 +1,51 @@
require 'sightstone/stat'
class MatchHistory
attr_accessor :games, :summonerId
def initialize(data)
@summonerId = data['summonerId']
@games = []
data['games'].each do |game|
games << Game.new(game)
end
end
end
class Game
attr_accessor :championId, :createDate, :createDateString, :fellowPlayers, :gameId, :gameMode, :gameType, :invalid, :level, :mapId, :spell1, :spell2, :statistics, :subType, :teamId
def initialize(data)
@championId=data['championId']
@createDate=data['createDate']
@createDateString=data['createDateString']
@fellowPlayers=[]
data['fellowPlayers'].each do |player|
@fellowPlayers << Player.new(player)
end
@gameId=data['gameId']
@gameMode=data['gameMode']
@gameType=data['gameType']
@invalid=data['invalid']
@level=data['level']
@mapId=data['mapId']
@spell1=data['spell1]']
@spell2=data['spell2']
@statistics = []
data['statistics'].each do |stat|
@statistics << Stat.new(stat)
end
@subType=data['subType']
@teamId=data['teamId']
end
end
class Player
attr_accessor :championId, :summonerId, :teamId
def initialize(data)
@championId = data['championId']
@summonerId = data['summonerId']
@teamId = data['teamId']
end
end

View File

@@ -6,7 +6,7 @@ class ChampionModule
@sightstone = sightstone
end
def get_champions(free_to_play=false)
def champions(free_to_play=false)
uri = "https://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/champion"
response = _get_api_response(uri, {'freeToPlay' => free_to_play})
puts response

View File

@@ -0,0 +1,50 @@
require 'sightstone/summoner'
require 'sightstone/match_history'
class GameModule
def initialize(sightstone)
@sightstone = sightstone
end
def recent(summoner)
id = if summoner.is_a? Summoner
summoner.id
else
summoner
end
uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/game/by-summoner/#{id}/recent"
response = _get_api_response(uri)
_parse_response(response) { |resp|
data = JSON.parse(resp)
return MatchHistory.new(data)
}
end
private
def _get_api_response(uri)
params = {'api_key' => @sightstone.api_key}
RestClient.get(uri, headers={:params => params}) {|response, request, result| response }
rescue SocketError => e
nil
end
def _parse_response(response, &block)
response_code = if response.nil?
500
else
response.code
end
if response_code == 200
block.call(response.body)
elsif response_code == 404
raise Sightstone::SummonerNotFoundException
elsif response_code == 500
raise Sightstone::SightstoneConnectionException
end
end
end

View File

@@ -2,15 +2,18 @@ require 'sightstone/summoner'
require 'sightstone/masterybook'
require 'sightstone/runebook'
class SummonerModule
def initialize(sightstone)
@sightstone = sightstone
end
def get_summoner_by_name(name)
uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/by-name/#{name}"
def summoner(name_or_id)
uri = if name_or_id.is_a? Integer
"http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{name_or_id}"
else
"http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/by-name/#{name_or_id}"
end
response = _get_api_response(uri)
_parse_response(response) { |resp|
data = JSON.parse(resp)
@@ -18,16 +21,8 @@ class SummonerModule
}
end
def get_summoner_by_id(id)
uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{id}"
response = _get_api_response(uri)
_parse_response(response) { |resp|
data = JSON.parse(resp)
return Summoner.new(data)
}
end
def get_summoners_names(ids)
def names(ids)
ids = ids.join(',')
uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{ids}/name"
response = _get_api_response(uri)
@@ -37,8 +32,13 @@ class SummonerModule
}
end
def get_runes_for_summoner(summoner)
uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{summoner.id}/runes"
def runes(summoner)
id = if summoner.is_a? Summoner
summoner.id
else
summoner
end
uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{id}/runes"
response = _get_api_response(uri)
_parse_response(response) { |resp|
data = JSON.parse(resp)
@@ -46,16 +46,22 @@ class SummonerModule
}
end
def get_masteries_for_summoner(summoner)
uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{summoner.id}/masteries"
def masteries(summoner)
id = if summoner.is_a? Summoner
summoner.id
else
summoner
end
uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{id}/masteries"
response = _get_api_response(uri)
_parse_response(response) { |resp|
data = JSON.parse(resp)
return MasteryBook.new(data)
}
end
private
def _get_api_response(uri)
params = {'api_key' => @sightstone.api_key}
RestClient.get(uri, headers={:params => params}) {|response, request, result| response }

7
lib/sightstone/stat.rb Normal file
View File

@@ -0,0 +1,7 @@
class Stat
def initialize(data)
@id = data['id']
@name = data['name']
@value = data['value']
end
end

Binary file not shown.