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



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

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

Instance Attribute Details

#api_keyString (readonly)



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

def api_key
  @api_key
end

#regionString



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



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



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

def api_url path, params = {}
  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

Raises:



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

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