initial commit

This commit is contained in:
danijoo
2013-12-13 09:10:40 +01:00
commit 16f738c060
10 changed files with 641 additions and 0 deletions

146
lib/sightstone.rb Normal file
View File

@@ -0,0 +1,146 @@
require 'rest_client'
require 'JSON'
Dir["sightstone/*.rb"].each {|file| require file }
Dir["sightstone/modules*.rb"].each {|file| require file }
class Sightstone
attr_accessor :region, :api_key
attr_accessor :summoner, :champion
def initialize(api_key, region)
@api_key = api_key
@region = region
@summoner = SummonerModule.new(self)
@champion = ChampionModule.new(self)
end
end
# Error classes
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

View File

@@ -0,0 +1,18 @@
class Champion
attr_accessor :active, :attackRank, :botEnabled, :botMmEnabled
attr_accessor :defenseRank, :difficultyRank, :freeToPlay, :id, :magicRank, :name, :rankedPlayEnabled
def initialize(data)
@active = data['active']
@attackRank = data['attackRank']
@botEnabled = data['botEnabled']
@defenseRank = data['defenseRank']
@difficultyRank = data['difficultyRank']
@freeToPlay = data['freeToPlay']
@id = data['id']
@magicRank = data['magicRank']
@name = data['name']
@rankedPlayEnabled = data['rankedPlayEnabled']
end
end

View File

@@ -0,0 +1,36 @@
class MasteryBook
attr_accessor :pages, :summonerId
def initialize(data)
@summonerId = data['summonerId']
@pages = []
data['pages'].each do |page|
@pages << MasteryPage.new(page)
end
end
end
class MasteryPage
attr_accessor :id, :talents, :name, :current
def initialize(data)
@id = data['id']
@name = data['name']
@current = data['current']
@talents = []
data['talents'].each do |talent|
@talents << Talent.new(talent)
end
end
end
class Talent
attr_accessor :id,:rank, :name
def initialize(data)
@id = data['id']
@name = data['name']
@rank = data['rank']
end
end

View File

@@ -0,0 +1,37 @@
class RuneBook
attr_accessor :pages, :summonerId
def initialize(data)
@summonerId = data['summonerId']
@pages = []
data['pages'].each do |page|
@pages << RunePage.new(page)
end
end
end
class RunePage
attr_accessor :id, :slots, :name, :current
def initialize(data)
@id = data['id']
@name = data['name']
@current = data['current']
@slots = {}
data['slots'].each do |slot|
@slots[slot['runeSlotId']] = Rune.new(slot['rune'])
end
end
end
class Rune
attr_accessor :id, :description, :name, :tier
def initialize(data)
@id = data['id']
@description = data['description']
@name = data['name']
@tier = data['tier']
end
end

View File

@@ -0,0 +1,16 @@
class Summoner
attr_accessor :name, :id, :profile_icon_id, :revision_date, :level
def initialize(data)
@name = data['name']
@id = data['id']
@profile_icon_id = data['profileIconId']
@revision_date = data['revisionDate']
@level = data['summonerLevel']
end
end