implemented teams module

This commit is contained in:
danijoo
2013-12-13 19:09:04 +01:00
parent 5458d25c1f
commit 83c04d8ffb
2 changed files with 133 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
require 'sightstone/summoner'
require 'sightstone/team'
class TeamModule
def initialize(sightstone)
@sightstone = sightstone
end
def teams(summoner)
id = if summoner.is_a? Summoner
summoner.id
else
summoner
end
uri = "http://prod.api.pvp.net/api/#{@sightstone.region}/v2.1/team/by-summoner/#{id}"
response = _get_api_response(uri)
_parse_response(response) { |resp|
data = JSON.parse(resp)
teams = []
data.each do |team|
teams << Team.new(team)
end
return teams
}
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

77
lib/sightstone/team.rb Normal file
View File

@@ -0,0 +1,77 @@
class Team
attr_accessor :teamStatSummary, :status, :tag, :roster, :lastGameDate, :modifyDate, :teamId, :timestamp, :lastJoinDate, :secondLastJoinDate, :matchHistory, :lastJoinedRankedTeamQueueDate, :name, :thirdLastJoinDate, :createDate
def initialize(data)
@status = data['status']
@tag = data['tag']
@roster = Roster.new(data['roster'])
@lastGameDate = data['lastGameDate']
@modifyDate = data['modifyDate']
@teamId = data['teamId']['fullId']
@timestamp = data['timestamp']
@lastJoinDate = data['lastJoinDate']
@secondLastJoinDate = data['secondLastJoinDate']
@matchHistory = []
data['matchHistory'].each do |game|
matchHistory << Game.new(game)
end
@lastJoinedRankedTeamQueueDate=data['lastJoinedRankedTeamQueueDate']
@name = data['name']
@thirdLastJoinDate = data['thirdLastJoinDate']
@createDate = data['createDate']
@teamStatSummary = []
data['teamStatSummary']['teamStatDetails'].each do |detail|
teamStatSummary << TeamStat.new(detail)
end
end
end
class Roster
attr_accessor :ownerId, :memberList
def initialize(data)
@ownerId = data['ownerId']
@memberList = []
data['memberList'].each do |m|
memberList << Member.new(m)
end
end
end
class Member
attr_accessor :joinDate, :inviteDate, :status, :playerId
def initialize(data)
@joinDate = data['joinDate']
@inviteDate = data['inviteDate']
@status = data['status']
@playerId = data['playerId']
end
end
class TeamStat
attr_accessor :wins, :losses, :averageGamesPlayed, :teamId, :teamStatType
def initialize(data)
@wins = data['wins']
@losses = data['losses']
@averageGamesPlayed = data['averageGamesPlayed']
@teamId = data['teamId']
@teamStatType = data['teamStatType']
end
end
class Game
attr_accessor :gameMode, :mapId, :assists, :opposingTeamName, :invalid, :deaths, :gameId, :kills, :win, :date, :opposingTeamKills
def initialize(data)
@gameMode = data['gameMode']
@mapId = data['mapId']
@assists = data['assists']
@opposingTeamName = data['opposingTeamName']
@invalid = data['invalid']
@deaths = data['deaths']
@gameId = data['gameId']
@kills = data['kills']
@win = data['win']
@date = data['date']
@opposingTeamKills = data['opposingTeamKills']
end
end