Class: Lol::Request

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

Overview

Encapsulates common methods for all requests Request classes inherit from this

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, region) ⇒ Request

Returns a new instance of Request.



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

def initialize api_key, region
  @api_key = api_key
  @region = region
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#regionString

Returns region.

Returns:

  • (String)

    region



17
18
19
# File 'lib/lol/request.rb', line 17

def region
  @region
end

Class Method Details

.api_versionString

Stub method. Each subclass should have its own api version

Returns:

  • (String)

    api version



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

def self.api_version
  "v1.1"
end

Instance Method Details

#api_url(path, params = {}) ⇒ String

Returns a full url for an API call

Parameters:

  • path (String)

    API path to call

Returns:

  • (String)

    full fledged url



29
30
31
32
33
# File 'lib/lol/request.rb', line 29

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

#perform_request(url) ⇒ String

Calls the API via HTTParty and handles errors

Parameters:

  • url (String)

    the url to call

Returns:

  • (String)

    raw response of the call

Raises:



38
39
40
41
42
43
44
# File 'lib/lol/request.rb', line 38

def perform_request url
  response = self.class.get(url)
  raise NotFound.new("404 Not Found") if response.respond_to?(:code) && response.not_found?
  raise InvalidAPIResponse.new(response["status"]["message"]) if response.is_a?(Hash) && response["status"]

  response
end