Class: Lol::Request
Overview
Encapsulates common methods for all requests Request classes inherit from this
Direct Known Subclasses
ChampionRequest, GameRequest, LeagueRequest, StatsRequest, SummonerRequest, TeamRequest
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#region ⇒ String
Region.
Class Method Summary collapse
-
.api_version ⇒ String
Stub method.
Instance Method Summary collapse
-
#api_url(path, params = {}) ⇒ String
Returns a full url for an API call.
-
#initialize(api_key, region) ⇒ Request
constructor
A new instance of Request.
-
#perform_request(url) ⇒ String
Calls the API via HTTParty and handles errors.
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_key ⇒ Object (readonly)
Returns the value of attribute api_key.
12 13 14 |
# File 'lib/lol/request.rb', line 12 def api_key @api_key end |
#region ⇒ String
Returns region.
17 18 19 |
# File 'lib/lol/request.rb', line 17 def region @region end |
Class Method Details
.api_version ⇒ String
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 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
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 |