Skip to content

Commit

Permalink
View summoners in regions besides NA
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McDonald committed Jul 5, 2015
1 parent a23cd88 commit a749beb
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SearchController < ApplicationController
def view
@summoner_name = summoner_name

matches = match_history_fetcher.fetch(summoner_name)
matches = match_history_fetcher.fetch(summoner_name, region)

@matches = matches.map do |match|
match_summarizer.summarize(match)
Expand All @@ -18,6 +18,10 @@ def summoner_name
params[:summoner_name]
end

def region
params[:region]
end

def match_history_fetcher
@match_history_fetcher ||= MatchHistoryFetcher.new
end
Expand Down
2 changes: 1 addition & 1 deletion app/objects/pro_game_scraper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def successful_games_scraper
end

def champion_endpoint
"#{LolApiConfig::LOL_API_BASE_URL}/api/lol/static-data/#{LolApiConfig::LOL_API_REGION}/v1.2/champion?api_key=#{ENV['LOL_API_KEY']}"
"#{LolApiConfig::LOL_API_GLOBAL_BASE_URL}/api/lol/static-data/#{LolApiConfig::LOL_API_REGION}/v1.2/champion?api_key=#{ENV['LOL_API_KEY']}"
end
end
12 changes: 12 additions & 0 deletions app/views/search/_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

<%= label_tag(:summoner_name, 'Summoner Name') %>
<%= text_field_tag(:summoner_name, nil) %>
<select name="region">
<option selected value="na">NA</option>
<option value="euw">EUW</option>
<option value="kr">KR</option>
<option value="eune">EUNE</option>
<option value="br">BR</option>
<option value="lan">LAN</option>
<option value="las">LAS</option>
<option value="oce">OCE</option>
<option value="ru">RU</option>
<option value="tr">TR</option>
</select>
<%= submit_tag 'Search' %>

<% end %>
4 changes: 2 additions & 2 deletions lib/champion_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def fetch(champion_id)
@@champions ||= all_champions
return @@champions[champion_id] if @@champions[champion_id]

champion_info_endpoint = "#{LolApiConfig::LOL_API_BASE_URL}/api/lol/static-data/#{LolApiConfig::LOL_API_REGION}/v1.2/champion/#{champion_id}?api_key=#{ENV['LOL_API_KEY']}"
champion_info_endpoint = "#{LolApiConfig::LOL_API_GLOBAL_BASE_URL}/api/lol/static-data/#{LolApiConfig::LOL_API_REGION}/v1.2/champion/#{champion_id}?api_key=#{ENV['LOL_API_KEY']}"
champion_info_response = RestClient.get(champion_info_endpoint)
@@champions[champion_id] = JSON.parse(champion_info_response)
end

private
def all_champions
all_champions = JSON.parse(RestClient.get("#{LolApiConfig::LOL_API_BASE_URL}/api/lol/static-data/#{LolApiConfig::LOL_API_REGION}/v1.2/champion?api_key=#{ENV['LOL_API_KEY']}"))
all_champions = JSON.parse(RestClient.get("#{LolApiConfig::LOL_API_GLOBAL_BASE_URL}/api/lol/static-data/#{LolApiConfig::LOL_API_REGION}/v1.2/champion?api_key=#{ENV['LOL_API_KEY']}"))
all_champions['data'].reduce({}) do |champions, (_, champion)|
champions[champion['id']] = champion
champions
Expand Down
7 changes: 6 additions & 1 deletion lib/lol_api_config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module LolApiConfig
LOL_API_BASE_URL = 'https://na.api.pvp.net'
LOL_API_GLOBAL_BASE_URL = 'https://global.api.pvp.net'
LOL_API_REGION = 'na'

def self.base_url(region)
"https://#{region}.api.pvp.net"
end

end
6 changes: 3 additions & 3 deletions lib/match_history_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
require 'uri'

class MatchHistoryFetcher
def fetch(summoner_name)
def fetch(summoner_name, region)
summoner_name = summoner_name.downcase.delete(' ')

summoner_id_endpoint = "#{LolApiConfig::LOL_API_BASE_URL}/api/lol/#{LolApiConfig::LOL_API_REGION}/v1.4/summoner/by-name/#{URI.escape(summoner_name)}?api_key=#{ENV['LOL_API_KEY']}"
summoner_id_endpoint = "#{LolApiConfig.base_url(region)}/api/lol/#{region}/v1.4/summoner/by-name/#{URI.escape(summoner_name)}?api_key=#{ENV['LOL_API_KEY']}"
summoner_id_response = RestClient.get(summoner_id_endpoint)
summoner_id = JSON.parse(summoner_id_response)[summoner_name]['id']

match_history_endpoint = "#{LolApiConfig::LOL_API_BASE_URL}/api/lol/#{LolApiConfig::LOL_API_REGION}/v2.2/matchhistory/#{summoner_id}?api_key=#{ENV['LOL_API_KEY']}"
match_history_endpoint = "#{LolApiConfig.base_url(region)}/api/lol/#{region}/v2.2/matchhistory/#{summoner_id}?api_key=#{ENV['LOL_API_KEY']}"
match_history_response = RestClient.get(match_history_endpoint)
JSON.parse(match_history_response)['matches']
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rune_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.by_id(id)

private
def self.all_runes
all_runes = JSON.parse(RestClient.get("#{LolApiConfig::LOL_API_BASE_URL}/api/lol/static-data/#{LolApiConfig::LOL_API_REGION}/v1.2/rune?runeListData=colloq,depth&api_key=#{ENV['LOL_API_KEY']}"))
all_runes = JSON.parse(RestClient.get("#{LolApiConfig::LOL_API_GLOBAL_BASE_URL}/api/lol/static-data/#{LolApiConfig::LOL_API_REGION}/v1.2/rune?runeListData=colloq,depth&api_key=#{ENV['LOL_API_KEY']}"))
all_runes['data'].reduce({}) do |runes, (id, rune)|
runes[id] = Rune.new(id.to_i, rune['name'], rune['rune']['type'])
runes
Expand Down

0 comments on commit a749beb

Please sign in to comment.