|
| 1 | +class DataDragonService |
| 2 | + BASE_URL = 'https://ddragon.leagueoflegends.com'.freeze |
| 3 | + |
| 4 | + class DataDragonError < StandardError; end |
| 5 | + |
| 6 | + def initialize |
| 7 | + @latest_version = nil |
| 8 | + end |
| 9 | + |
| 10 | + # Get the latest game version |
| 11 | + def latest_version |
| 12 | + @latest_version ||= fetch_latest_version |
| 13 | + end |
| 14 | + |
| 15 | + # Get champion ID to name mapping |
| 16 | + def champion_id_map |
| 17 | + Rails.cache.fetch('riot:champion_id_map', expires_in: 1.week) do |
| 18 | + fetch_champion_data |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + # Get champion name to ID mapping (reverse) |
| 23 | + def champion_name_map |
| 24 | + Rails.cache.fetch('riot:champion_name_map', expires_in: 1.week) do |
| 25 | + champion_id_map.invert |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + # Get all champions data (full details) |
| 30 | + def all_champions |
| 31 | + Rails.cache.fetch('riot:all_champions', expires_in: 1.week) do |
| 32 | + fetch_all_champions_data |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + # Get specific champion data by key |
| 37 | + def champion_by_key(champion_key) |
| 38 | + all_champions[champion_key] |
| 39 | + end |
| 40 | + |
| 41 | + # Get profile icons data |
| 42 | + def profile_icons |
| 43 | + Rails.cache.fetch('riot:profile_icons', expires_in: 1.week) do |
| 44 | + fetch_profile_icons |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + # Get summoner spells data |
| 49 | + def summoner_spells |
| 50 | + Rails.cache.fetch('riot:summoner_spells', expires_in: 1.week) do |
| 51 | + fetch_summoner_spells |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # Get items data |
| 56 | + def items |
| 57 | + Rails.cache.fetch('riot:items', expires_in: 1.week) do |
| 58 | + fetch_items |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + # Clear all cached data |
| 63 | + def clear_cache! |
| 64 | + Rails.cache.delete('riot:champion_id_map') |
| 65 | + Rails.cache.delete('riot:champion_name_map') |
| 66 | + Rails.cache.delete('riot:all_champions') |
| 67 | + Rails.cache.delete('riot:profile_icons') |
| 68 | + Rails.cache.delete('riot:summoner_spells') |
| 69 | + Rails.cache.delete('riot:items') |
| 70 | + Rails.cache.delete('riot:latest_version') |
| 71 | + @latest_version = nil |
| 72 | + end |
| 73 | + |
| 74 | + private |
| 75 | + |
| 76 | + def fetch_latest_version |
| 77 | + cached_version = Rails.cache.read('riot:latest_version') |
| 78 | + return cached_version if cached_version.present? |
| 79 | + |
| 80 | + url = "#{BASE_URL}/api/versions.json" |
| 81 | + response = make_request(url) |
| 82 | + versions = JSON.parse(response.body) |
| 83 | + |
| 84 | + latest = versions.first |
| 85 | + Rails.cache.write('riot:latest_version', latest, expires_in: 1.day) |
| 86 | + latest |
| 87 | + rescue StandardError => e |
| 88 | + Rails.logger.error("Failed to fetch latest version: #{e.message}") |
| 89 | + # Fallback to a recent known version |
| 90 | + '14.1.1' |
| 91 | + end |
| 92 | + |
| 93 | + def fetch_champion_data |
| 94 | + version = latest_version |
| 95 | + url = "#{BASE_URL}/cdn/#{version}/data/en_US/champion.json" |
| 96 | + |
| 97 | + response = make_request(url) |
| 98 | + data = JSON.parse(response.body) |
| 99 | + |
| 100 | + # Create mapping: champion_id (integer) => champion_name (string) |
| 101 | + champion_map = {} |
| 102 | + data['data'].each do |_key, champion| |
| 103 | + champion_id = champion['key'].to_i |
| 104 | + champion_name = champion['id'] # This is the champion name like "Aatrox" |
| 105 | + champion_map[champion_id] = champion_name |
| 106 | + end |
| 107 | + |
| 108 | + champion_map |
| 109 | + rescue StandardError => e |
| 110 | + Rails.logger.error("Failed to fetch champion data: #{e.message}") |
| 111 | + {} |
| 112 | + end |
| 113 | + |
| 114 | + def fetch_all_champions_data |
| 115 | + version = latest_version |
| 116 | + url = "#{BASE_URL}/cdn/#{version}/data/en_US/champion.json" |
| 117 | + |
| 118 | + response = make_request(url) |
| 119 | + data = JSON.parse(response.body) |
| 120 | + |
| 121 | + data['data'] |
| 122 | + rescue StandardError => e |
| 123 | + Rails.logger.error("Failed to fetch all champions data: #{e.message}") |
| 124 | + {} |
| 125 | + end |
| 126 | + |
| 127 | + def fetch_profile_icons |
| 128 | + version = latest_version |
| 129 | + url = "#{BASE_URL}/cdn/#{version}/data/en_US/profileicon.json" |
| 130 | + |
| 131 | + response = make_request(url) |
| 132 | + data = JSON.parse(response.body) |
| 133 | + |
| 134 | + data['data'] |
| 135 | + rescue StandardError => e |
| 136 | + Rails.logger.error("Failed to fetch profile icons: #{e.message}") |
| 137 | + {} |
| 138 | + end |
| 139 | + |
| 140 | + def fetch_summoner_spells |
| 141 | + version = latest_version |
| 142 | + url = "#{BASE_URL}/cdn/#{version}/data/en_US/summoner.json" |
| 143 | + |
| 144 | + response = make_request(url) |
| 145 | + data = JSON.parse(response.body) |
| 146 | + |
| 147 | + data['data'] |
| 148 | + rescue StandardError => e |
| 149 | + Rails.logger.error("Failed to fetch summoner spells: #{e.message}") |
| 150 | + {} |
| 151 | + end |
| 152 | + |
| 153 | + def fetch_items |
| 154 | + version = latest_version |
| 155 | + url = "#{BASE_URL}/cdn/#{version}/data/en_US/item.json" |
| 156 | + |
| 157 | + response = make_request(url) |
| 158 | + data = JSON.parse(response.body) |
| 159 | + |
| 160 | + data['data'] |
| 161 | + rescue StandardError => e |
| 162 | + Rails.logger.error("Failed to fetch items: #{e.message}") |
| 163 | + {} |
| 164 | + end |
| 165 | + |
| 166 | + def make_request(url) |
| 167 | + conn = Faraday.new do |f| |
| 168 | + f.request :retry, max: 3, interval: 0.5, backoff_factor: 2 |
| 169 | + f.adapter Faraday.default_adapter |
| 170 | + end |
| 171 | + |
| 172 | + response = conn.get(url) do |req| |
| 173 | + req.options.timeout = 10 |
| 174 | + end |
| 175 | + |
| 176 | + unless response.success? |
| 177 | + raise DataDragonError, "Request failed with status #{response.status}" |
| 178 | + end |
| 179 | + |
| 180 | + response |
| 181 | + rescue Faraday::TimeoutError => e |
| 182 | + raise DataDragonError, "Request timeout: #{e.message}" |
| 183 | + rescue Faraday::Error => e |
| 184 | + raise DataDragonError, "Network error: #{e.message}" |
| 185 | + end |
| 186 | +end |
0 commit comments