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



129
130
131
132
# File 'lib/lol/client.rb', line 129

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.



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

def api_key
  @api_key
end

#regionString

Returns name of region.

Returns:

  • (String)

    name of region



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

def region
  @region
end

Instance Method Details

#api_url(version, path, params = {}) ⇒ 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



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

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

#championObject

Calls the latest API version of champion



40
41
42
# File 'lib/lol/client.rb', line 40

def champion
  champion11
end

#champion11Array

Retrieve all champions, v1.1

Returns:

  • (Array)

    an array of champions



46
47
48
# File 'lib/lol/client.rb', line 46

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



52
53
54
# File 'lib/lol/client.rb', line 52

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



59
60
61
62
63
64
# File 'lib/lol/client.rb', line 59

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



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

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

#league(summoner_id) ⇒ Object

Calls the latest API version of league



67
68
69
# File 'lib/lol/client.rb', line 67

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



73
74
75
76
# File 'lib/lol/client.rb', line 73

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

#ranked_stats(*args) ⇒ Object

Calls the latest API version of ranked_stats



96
97
98
# File 'lib/lol/client.rb', line 96

def ranked_stats *args
  ranked_stats11 *args
end

#ranked_stats11(summoner_id, extra = {}) ⇒ RankedStatisticsSummary

Retrieves ranked statistics summary for the given summoner

Returns:



103
104
105
106
107
108
109
# File 'lib/lol/client.rb', line 103

def ranked_stats11 summoner_id, extra = {}
  if extra.keys.select { |k| k.to_sym != :season }.any?
    raise ArgumentError, 'Only :season is allowed as extra parameter'
  end
  stats_api_path = "stats/by-summoner/#{summoner_id}/ranked"
  RankedStatisticsSummary.new get api_url 'v1.1', stats_api_path, extra
end

#stats(*args) ⇒ Object

Calls the latest API version of stats



79
80
81
# File 'lib/lol/client.rb', line 79

def stats *args
  stats11 *args
end

#stats11(summoner_id, extra = {}) ⇒ Array

Retrieves player statistics summaries for the given summoner

Returns:

  • (Array)

    an array of player statistics, one per queue type



85
86
87
88
89
90
91
92
93
# File 'lib/lol/client.rb', line 85

def stats11 summoner_id, extra = {}
  if extra.keys.select { |k| k.to_sym != :season }.any?
    raise ArgumentError, 'Only :season is allowed as extra parameter'
  end
  stats_api_path = "stats/by-summoner/#{summoner_id}/summary"
  get(api_url('v1.1', stats_api_path, extra))['playerStatSummaries'].map do |player_stat_data|
    PlayerStatistic.new player_stat_data
  end
end

#team(*args) ⇒ Object

Calls the latest API version of team returning the list of teams for the given summoner



112
113
114
# File 'lib/lol/client.rb', line 112

def team *args
  team21 *args
end

#team21(summoner_id) ⇒ Array

Retrieves the list of Teams for the given summoner

Returns:

  • (Array)

    List of Team



118
119
120
121
122
# File 'lib/lol/client.rb', line 118

def team21 summoner_id
  get(api_url 'v2.1', "team/by-summoner/#{summoner_id}").map do |team_data|
    Team.new team_data
  end
end