lib/bnet/starcraft2/ladder.rb in bnet-0.0.5 vs lib/bnet/starcraft2/ladder.rb in bnet-0.0.10

- old
+ new

@@ -13,11 +13,10 @@ "matchMakingQueue" => :matchmaking_queue, "wins" => :wins, "losses" => :losses } - def self.find_current(profile, args = {}) profile_id = profile.profile_id name = profile.name realm = profile.realm || '1' locale = args[:locale] || 'en_US' @@ -29,24 +28,11 @@ begin data = open(call_url) raw_collection_response = JSON.parse(data.read) if Bnet::API.valid_call?(data.status, raw_collection_response) - - ladders = raw_collection_response["currentSeason"].collect do |raw_ladder_character_response| - raw_characters = raw_ladder_character_response["characters"] - raw_ladders = raw_ladder_character_response["ladder"].collect do |raw_ladder| - ladder = from_api(raw_ladder) - ladder.characters = raw_characters - ladder - end - - raw_ladders - end - - ladders.flatten! - + ladders = collection_from_api(raw_collection_response["currentSeason"]) else ladders = [] end rescue OpenURI::HTTPError => e @@ -70,24 +56,11 @@ begin data = open(call_url) raw_collection_response = JSON.parse(data.read) if Bnet::API.valid_call?(data.status, raw_collection_response) - - ladders = raw_collection_response["previousSeason"].collect do |raw_ladder_character_response| - raw_characters = raw_ladder_character_response["characters"] - raw_ladders = raw_ladder_character_response["ladder"].collect do |raw_ladder| - ladder = from_api(raw_ladder) - ladder.characters = raw_characters - ladder - end - - raw_ladders - end - - ladders.flatten! - + ladders = collection_from_api(raw_collection_response["previousSeason"]) else ladders = [] end rescue OpenURI::HTTPError => e @@ -95,7 +68,26 @@ end return ladders end + + private + + def self.collection_from_api(raw_collection_response) + ladders = raw_collection_response.collect do |raw_response| + raw_characters = raw_response["characters"] + raw_ladders = raw_response["ladder"].collect do |raw_ladder| + ladder = from_api(raw_ladder) + ladder.characters = raw_characters + ladder + end + + raw_ladders + end + + ladders.flatten! + + end + end