Contents

Writing a League of Legends API Wrapper

Contents

League of Legends used to be one of my favorite games. One of my practice Rails sites is a LoL stats tracker that lets you look up play stats from RiotGame’s API. Rather than use a gem that does all the work for me, I wrote my own wrapper to practice making my own API calls.

/posts/2015-04-13-writing-a-league-of-legends-api-wrapper/images/player-data-raw.png
Raw Player Data Output

The process was fairly simple. I initialized my LeagueWrapper object with my API key that’s stored in Rails secrets.yml. This will refer to the league_api_key variable I have in my secrets.yml.

development:
  league_api_key:

Next I follow Riot’s documentation on how to make API calls and use net/http to return the results. And voila! We now have data returned from Riot that we can muck around with and manipulate. I now have retrieved the summoner ID from my friend’s username, Killerbill, and got data from his recent matches. By decoding the JSON retrieved, we can clearly see the data needed to manipulate.

/posts/2015-04-13-writing-a-league-of-legends-api-wrapper/images/match-history.png
Match History
/posts/2015-04-13-writing-a-league-of-legends-api-wrapper/images/match-history-json.png
Match History JSON

From using that data, in my models I can pick out the information I need and save it to my database:

/posts/2015-04-13-writing-a-league-of-legends-api-wrapper/images/match-history.png
Save to Database Code

This allows me to store stats such as KDA, or something more interesting such as average gold per game at various times in the game, or performance across multiple game modes.

/posts/2015-04-13-writing-a-league-of-legends-api-wrapper/images/match-history-parsed.png
Parsed Match History

And there you have it, a simple way to use Riot’s League of Legends API with Rails.