Class: RiotAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/riot_api/client.rb

Overview

The +RiotAPI::Client+ class is resposible to handle HTTP requests between the library and the Riot Games API.

Constant Summary collapse

BASE_URLS =
{
  # Regional endpoints
  br:   "https://br1.api.riotgames.com",
  eune: "https://eun1.api.riotgames.com",
  euw:  "https://euw1.api.riotgames.com",
  jp:   "https://jp1.api.riotgames.com",
  kr:   "https://kr.api.riotgames.com",
  lan:  "https://la1.api.riotgames.com",
  las:  "https://la2.api.riotgames.com",
  na:   "https://na1.api.riotgames.com",
  oce:  "https://oc1.api.riotgames.com",
  tr:   "https://tr1.api.riotgames.com",
  ru:   "https://ru.api.riotgames.com",
  pbe:  "https://pbe1.api.riotgames.com",

  # Regional proxies
  americas: "https://americas.api.riotgames.com",
  europe:   "https://europe.api.riotgames.com",
  asia:     "https://asia.api.riotgames.com"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



32
33
34
35
36
37
38
# File 'lib/riot_api/client.rb', line 32

def initialize(api_key)
  if api_key.nil? || api_key.empty?
    raise ArgumentError, "The API key must be informed"
  end

  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/riot_api/client.rb', line 9

def api_key
  @api_key
end

Instance Method Details

#request(method, path, region, params = {}) ⇒ Hash

Makes an HTTP request to the Riot Games API, based on the selected +region+. If the response status is different than HTTP 200, it will raise a +RiotAPI::ResponseError+ exception.

Parameters:

  • method (Symbol)

    The HTTP method to use, e.g.: +:get+, +:post+, etc.

  • path (String)

    The request path, e.g.: +/matches/1+

  • region (Symbol)

    The League of Legends region to request. The supported list of regions are listed on +RiotAPI::Client::BASE_URLS+.

  • params (Hash) (defaults to: {})

    The request parameters, e.g.: +{ name: "Example" }+

Returns:

  • (Hash)

    The request response



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/riot_api/client.rb', line 48

def request(method, path, region, params = {})
  unless BASE_URLS.key?(region)
    raise RiotAPI::InvalidRequestError, "The informed region is not supported. Select one of the following: #{BASE_URLS.keys}"
  end

  url = URI.escape("#{BASE_URLS[region]}#{path}")

  params.merge!({ api_key: @api_key })

  case method
  when :get
    request_get(URI(url), params)
  else
    raise RiotAPI::InvalidRequestError, "The method #{method} is not allowed"
  end
end