diff --git a/lib/sightstone.rb b/lib/sightstone.rb index 522b9f9..e23b665 100644 --- a/lib/sightstone.rb +++ b/lib/sightstone.rb @@ -4,10 +4,11 @@ require 'sightstone/modules/champion_module' require 'sightstone/modules/summoner_module' require 'sightstone/modules/game_module' require 'sightstone/modules/league_module' +require 'sightstone/modules/stats_module' class Sightstone attr_accessor :region, :api_key - attr_accessor :summoner, :champion, :game, :league + attr_accessor :summoner, :champion, :game, :league, :stats def initialize(api_key, region) @api_key = api_key @@ -17,6 +18,7 @@ class Sightstone @champion = ChampionModule.new(self) @game = GameModule.new(self) @league = LeagueModule.new(self) + @stats = StatsModule.new(self) end end diff --git a/lib/sightstone/modules/stats_module.rb b/lib/sightstone/modules/stats_module.rb new file mode 100644 index 0000000..483b4dd --- /dev/null +++ b/lib/sightstone/modules/stats_module.rb @@ -0,0 +1,74 @@ +require 'sightstone/summoner' +require 'sightstone/player_stats_summary' +require 'sightstone/ranked_stats' + +class StatsModule + def initialize(sightstone) + @sightstone = sightstone + end + + def summary(summoner, season=nil) + id = if summoner.is_a? Summoner + summoner.id + else + summoner + end + uri = "https://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/stats/by-summoner/#{id}/summary" + response = if season.nil? + _get_api_response(uri) + else + _get_api_response(uri, {'season' => season}) + end + + _parse_response(response) { |resp| + data = JSON.parse(resp) + return PlayerStatsSummaryList.new(data) + } + + end + + def ranked(summoner, season=nil) + id = if summoner.is_a? Summoner + summoner.id + else + summoner + end + uri = "https://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/stats/by-summoner/#{id}/ranked" + response = if season.nil? + _get_api_response(uri) + else + _get_api_response(uri, {'season' => season}) + end + + _parse_response(response) { |resp| + data = JSON.parse(resp) + return RankedStats.new(data) + } + + end + + def _get_api_response(uri, header={}) + params = {'api_key' => @sightstone.api_key}.merge header + 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 \ No newline at end of file diff --git a/lib/sightstone/player_stats_summary.rb b/lib/sightstone/player_stats_summary.rb new file mode 100644 index 0000000..6240283 --- /dev/null +++ b/lib/sightstone/player_stats_summary.rb @@ -0,0 +1,30 @@ +require 'sightstone/stat' + +class PlayerStatsSummaryList + + attr_accessor :summonerId, :playerStatSummaries + + def initialize(data) + @summonerId = data['playerStatSummary'] + @playerStatSummaries = [] + data['playerStatSummaries'].each do |summary| + @playerStatSummaries << PlayerStatSummary.new(summary) + end + end +end + +class PlayerStatSummary + attr_accessor :wins, :losses, :modifyDateStr, :modifyDate, :playerStatSummaryType, :aggregatedStats + + def initialize(data) + @wins = data['wins'] + @losses = data['losses'] + @modifyDatStr = data['modifyDateStr'] + @modifyDate = data['modifyDate'] + @playerStatSummaryType = data['playerStatSummaryType'] + @aggregatedStats = [] + data['aggregatedStats'].each do |stat| + @aggregatedStats << Stat.new(data) + end + end +end \ No newline at end of file diff --git a/lib/sightstone/ranked_stats.rb b/lib/sightstone/ranked_stats.rb new file mode 100644 index 0000000..fb83bc5 --- /dev/null +++ b/lib/sightstone/ranked_stats.rb @@ -0,0 +1,40 @@ +require 'sightstone/stat' + +class RankedStats + attr_accessor :champions, :modifyData, :modifyDateStr, :summonerId + + def initialize(data) + @summonerId = data['summonerId'] + @modifyDate = data['modifyDate'] + @modifyDateStr = data['modifyDateStr'] + @champions = [] + + data['champions'].each do |champ| + @champions << ChampionStats.new(champ) + end + end +end + +class ChampionStats + attr_accessor :id, :name, :stats + + def initialize(data) + @id = data['id'] + @name = data['name'] + @stats = [] + + data['stats'].each do |stat| + @stats << ChampionStat.new(stat) + end + end +end + +class ChampionStat < Stat + + attr_accessor :count + + def initialize(data) + super(data) + @count = data['c'] + end +end \ No newline at end of file diff --git a/lib/sightstone/stat.rb b/lib/sightstone/stat.rb index 66f4088..131598f 100644 --- a/lib/sightstone/stat.rb +++ b/lib/sightstone/stat.rb @@ -1,7 +1,13 @@ class Stat + attr_accessor :id, :name, :value + def initialize(data) @id = data['id'] @name = data['name'] - @value = data['value'] + @value = if data.has_key? 'value' + data['value'] + else + data['count'] + end end end \ No newline at end of file