Class: Lol::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/lol/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Lol::Client

Initializes a Lol::Client

Parameters:

  • api_key (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :region (String) — default: "EUW"

    The region on which the requests will be made



81
82
83
84
# File 'lib/lol/client.rb', line 81

def initialize api_key, options = {}
  @api_key = api_key
  @region = options.delete(:region) || "euw"
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



15
16
17
# File 'lib/lol/client.rb', line 15

def api_key
  @api_key
end

#regionString

Returns name of region.

Returns:

  • (String)

    name of region



11
12
13
# File 'lib/lol/client.rb', line 11

def region
  @region
end

Instance Method Details

#api_url(version, path) ⇒ String

Returns a full url for an API call

Parameters:

  • version (String)

    API version to call

  • path (String)

    API path to call

Returns:

  • (String)

    full fledged url



21
22
23
24
# File 'lib/lol/client.rb', line 21

def api_url version, path
  lol = version == "v1.1" ? "lol" : ""
  File.join "http://prod.api.pvp.net/api/", lol, "/#{region}/#{version}/", "#{path}?api_key=#{api_key}"
end

#championObject

Calls the latest API version of champion



38
39
40
# File 'lib/lol/client.rb', line 38

def champion
  champion11
end

#champion11Array

Retrieve all champions, v1.1

Returns:

  • (Array)

    an array of champions



44
45
46
# File 'lib/lol/client.rb', line 44

def champion11
  get(api_url("v1.1", "champion"))["champions"].map {|c| Champion.new(c)}
end

#game(*args) ⇒ Object

Calls the latest API version of game returning the list of recent games played by a summoner



50
51
52
# File 'lib/lol/client.rb', line 50

def game *args
  game11 *args
end

#game11(summoner_id) ⇒ Array

Returns a list of the recent games played by a summoner

Parameters:

  • summoner_id (Fixnum)

    Summoner id

Returns:

  • (Array)

    an array of games



57
58
59
60
61
62
# File 'lib/lol/client.rb', line 57

def game11 summoner_id
  summoner_api_path = "game/by-summoner/#{summoner_id}/recent"
  get(api_url("v1.1", summoner_api_path))["games"].map do |game_data|
    Game.new game_data
  end
end

#get(url) ⇒ Object

Calls the API via HTTParty and handles errors



28
29
30
31
32
33
34
35
# File 'lib/lol/client.rb', line 28

def get url
  response = self.class.get(url)
  if response["status"]
    raise InvalidAPIResponse.new(response["status"]["message"])
  else
    response
  end
end

#league(summoner_id) ⇒ Object

Calls the latest API version of league



65
66
67
# File 'lib/lol/client.rb', line 65

def league summoner_id
  league21 summoner_id
end

#league21(summoner_id) ⇒ Array

Retrieves leagues data for summoner, including leagues for all of summoner’s teams, v2.1

Returns:

  • (Array)

    an array of champions



71
72
73
# File 'lib/lol/client.rb', line 71

def league21 summoner_id
  get(api_url("v2.1", "league/by-summoner/#{summoner_id}"))[summoner_id].map {|l| League.new}
end