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 0000000..20d0c46 Binary files /dev/null and b/sightstone-0.0.0.gem differ