diff --git a/lib/sightstone.rb b/lib/sightstone.rb index 1064827..c713cf7 100644 --- a/lib/sightstone.rb +++ b/lib/sightstone.rb @@ -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 diff --git a/lib/sightstone/match_history.rb b/lib/sightstone/match_history.rb new file mode 100644 index 0000000..c98ec14 --- /dev/null +++ b/lib/sightstone/match_history.rb @@ -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 \ No newline at end of file diff --git a/lib/sightstone/modules/champion_module.rb b/lib/sightstone/modules/champion_module.rb index ea2455e..b1e8be6 100644 --- a/lib/sightstone/modules/champion_module.rb +++ b/lib/sightstone/modules/champion_module.rb @@ -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 diff --git a/lib/sightstone/modules/game_module.rb b/lib/sightstone/modules/game_module.rb new file mode 100644 index 0000000..7058803 --- /dev/null +++ b/lib/sightstone/modules/game_module.rb @@ -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 \ No newline at end of file diff --git a/lib/sightstone/modules/summoner_module.rb b/lib/sightstone/modules/summoner_module.rb index fb1849c..b3190f8 100644 --- a/lib/sightstone/modules/summoner_module.rb +++ b/lib/sightstone/modules/summoner_module.rb @@ -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 } diff --git a/lib/sightstone/stat.rb b/lib/sightstone/stat.rb new file mode 100644 index 0000000..66f4088 --- /dev/null +++ b/lib/sightstone/stat.rb @@ -0,0 +1,7 @@ +class Stat + def initialize(data) + @id = data['id'] + @name = data['name'] + @value = data['value'] + end +end \ No newline at end of file diff --git a/sightstone-0.0.0.gem b/sightstone-0.0.0.gem index 20d0c46..bf27279 100644 Binary files a/sightstone-0.0.0.gem and b/sightstone-0.0.0.gem differ