From c5ae552fe8e911983ba80a4ac05bc3175ca97559 Mon Sep 17 00:00:00 2001 From: danijoo Date: Fri, 13 Dec 2013 09:43:51 +0100 Subject: [PATCH] created modules --- lib/sightstone.rb | 126 +--------------------- lib/sightstone/modules/champion_module.rb | 47 ++++++++ lib/sightstone/modules/summoner_module.rb | 82 ++++++++++++++ sightstone-0.0.0.gem | Bin 0 -> 5632 bytes 4 files changed, 131 insertions(+), 124 deletions(-) create mode 100644 lib/sightstone/modules/champion_module.rb create mode 100644 lib/sightstone/modules/summoner_module.rb create mode 100644 sightstone-0.0.0.gem diff --git a/lib/sightstone.rb b/lib/sightstone.rb index 08eb725..1064827 100644 --- a/lib/sightstone.rb +++ b/lib/sightstone.rb @@ -1,8 +1,7 @@ require 'rest_client' require 'JSON' -Dir["sightstone/*.rb"].each {|file| require file } -Dir["sightstone/modules*.rb"].each {|file| require file } - +require 'sightstone/modules/champion_module' +require 'sightstone/modules/summoner_module' class Sightstone attr_accessor :region, :api_key @@ -23,124 +22,3 @@ class SightstoneApiException < Exception; end class SummonerNotFoundException < SightstoneApiException; end class SightstoneConnectionException < SightstoneApiException; end - -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}" - response = _get_api_response(uri) - _parse_response(response) { |resp| - data = JSON.parse(resp) - return Summoner.new(data) - } - 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) - ids = ids.join(',') - uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{ids}/name" - response = _get_api_response(uri) - _parse_response(response) { |resp| - data = JSON.parse(resp) - return data['summoners'] - } - end - - def get_runes_for_summoner(summoner) - uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{summoner.id}/runes" - response = _get_api_response(uri) - _parse_response(response) { |resp| - data = JSON.parse(resp) - return RuneBook.new(data) - } - end - - def get_masteries_for_summoner(summoner) - uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{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 } - 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 - -class ChampionModule - def initialize(sightstone) - @sightstone = sightstone - end - - def get_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}) - - _parse_response(response) { |resp| - data = JSON.parse(resp) - champions = [] - data['champions'].each do |champ| - champions << Champion.new(champ) - end - return champions - } - end - - def _get_api_response(uri, headers={}) - params = {'api_key' => @sightstone.api_key}.merge headers - 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 == 500 - raise Sightstone::SightstoneConnectionException - end - end -end \ No newline at end of file diff --git a/lib/sightstone/modules/champion_module.rb b/lib/sightstone/modules/champion_module.rb new file mode 100644 index 0000000..ea2455e --- /dev/null +++ b/lib/sightstone/modules/champion_module.rb @@ -0,0 +1,47 @@ +require 'sightstone/champion' + +class ChampionModule + + def initialize(sightstone) + @sightstone = sightstone + end + + def get_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 + + _parse_response(response) { |resp| + data = JSON.parse(resp) + champions = [] + data['champions'].each do |champ| + champions << Champion.new(champ) + end + return champions + } + end + + private + def _get_api_response(uri, headers={}) + params = {'api_key' => @sightstone.api_key}.merge headers + 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 == 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 new file mode 100644 index 0000000..fb1849c --- /dev/null +++ b/lib/sightstone/modules/summoner_module.rb @@ -0,0 +1,82 @@ +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}" + response = _get_api_response(uri) + _parse_response(response) { |resp| + data = JSON.parse(resp) + return Summoner.new(data) + } + 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) + ids = ids.join(',') + uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{ids}/name" + response = _get_api_response(uri) + _parse_response(response) { |resp| + data = JSON.parse(resp) + return data['summoners'] + } + end + + def get_runes_for_summoner(summoner) + uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{summoner.id}/runes" + response = _get_api_response(uri) + _parse_response(response) { |resp| + data = JSON.parse(resp) + return RuneBook.new(data) + } + end + + def get_masteries_for_summoner(summoner) + uri = "http://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/summoner/#{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 } + 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 diff --git a/sightstone-0.0.0.gem b/sightstone-0.0.0.gem new file mode 100644 index 0000000000000000000000000000000000000000..20d0c46214f51dd7524688d551e83675dc94e4f3 GIT binary patch literal 5632 zcmeHKcU05K7DkaKQi2gg1Va-r2*HFdO<1a^fEZkAfEaoUO{9u6=~YykbOQ>65osGxyA$x!=rpW`X%NN5CMIB%duE+kM2m zr@q3A%@kfK_2fs*R1Jx+`_*6sqSBlA$qN&Yj@Y}`$u^r?$yeJ>vmC9P@n-0Bl}Dzt7r!C)fz$Vkmy z6A&H)GdD0;-3uJZ(=5_%lTfUu)Lqb)J$`jUHLd$~ALw?1Fs_$mU~CmvDr0~3CatM; z8N1e;T3Y0efr0F)BDn}GyPm-2d^sKk!k+TJjy*XPHtoPNF@D7~3rk$s3c6bdS#&W0 z%YMWjDp>^7Jbx+&x(s~#MgS58@bRMrg`l$u6T*DQS#P9Y6*)dExVWUd@lh<3dkx~~ z0XA*1T&Ou~)Iv5^b@PZFIIN`I5-Q+kZqbPUuuroD5y`ISc!G9p7Xo_5$3{L0ZSQ?b z83f4*)=j*h5i)v7gLf!oW?3_YI;JaR*qc^yYZ!^pn+@Hkmv0=7%Xt`&!=>at(XYO~ znH$B0)7Uo4i`zUGVypzNBoj+b91{pmK(yy0(DHh)2`x2S(gX#twW+mJ&uB!1zNNb6 z!`45iT)3;ioYWFmeOyM#w@9Xaxj||*^BIF1jod;L$~oZ{?5scy-WRD^S0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