Class: Lol::Request
Overview
Encapsulates common methods for all requests Request classes inherit from this
Direct Known Subclasses
ChampionRequest, CurrentGameRequest, FeaturedGamesRequest, GameRequest, LeagueRequest, LolStatusRequest, MatchHistoryRequest, MatchRequest, StaticRequest, StatsRequest, SummonerRequest, TeamRequest
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
Api_key.
-
#cache_store ⇒ Object
readonly
@!attribute cache_store.
-
#region ⇒ String
Region.
Class Method Summary collapse
-
.api_version ⇒ String
Stub method.
Instance Method Summary collapse
- #api_base_url ⇒ Object
- #api_query_string(params = {}) ⇒ Object
-
#api_url(path, params = {}) ⇒ String
Returns a full url for an API call.
-
#cached? ⇒ Boolean
True if the request should be cached.
-
#clean_url(url) ⇒ String
Returns just a path from a full api url.
-
#initialize(api_key, region, cache_store = {}) ⇒ Request
constructor
Initializes a new Request.
-
#perform_request(url) ⇒ String
Calls the API via HTTParty and handles errors.
-
#store ⇒ Redis
Returns the cache store.
-
#ttl ⇒ Fixnum
The ttl to apply to cached keys.
Constructor Details
#initialize(api_key, region, cache_store = {}) ⇒ Request
Initializes a new Request
100 101 102 103 104 105 |
# File 'lib/lol/request.rb', line 100 def initialize api_key, region, cache_store = {} @cache_store = cache_store raise InvalidCacheStore if cached? && !store.is_a?(Redis) @api_key = api_key @region = region end |
Instance Attribute Details
#api_key ⇒ String (readonly)
Returns api_key.
16 17 18 |
# File 'lib/lol/request.rb', line 16 def api_key @api_key end |
#cache_store ⇒ Object (readonly)
@!attribute cache_store
25 26 27 |
# File 'lib/lol/request.rb', line 25 def cache_store @cache_store end |
#region ⇒ String
Returns region.
21 22 23 |
# File 'lib/lol/request.rb', line 21 def region @region end |
Class Method Details
.api_version ⇒ String
Stub method. Each subclass should have its own api version
29 30 31 |
# File 'lib/lol/request.rb', line 29 def self.api_version "v1.1" end |
Instance Method Details
#api_base_url ⇒ Object
41 42 43 |
# File 'lib/lol/request.rb', line 41 def api_base_url "https://#{region}.api.pvp.net" end |
#api_query_string(params = {}) ⇒ Object
45 46 47 |
# File 'lib/lol/request.rb', line 45 def api_query_string params = {} URI.encode_www_form params.merge api_key: api_key end |
#api_url(path, params = {}) ⇒ String
Returns a full url for an API call
36 37 38 39 |
# File 'lib/lol/request.rb', line 36 def api_url path, params = {} url = "#{api_base_url}/api/lol/#{region}/#{self.class.api_version}/#{path}" "#{url}?#{api_query_string params}" end |
#cached? ⇒ Boolean
Returns true if the request should be cached.
83 84 85 |
# File 'lib/lol/request.rb', line 83 def cached? cache_store[:cached] end |
#clean_url(url) ⇒ String
Returns just a path from a full api url
51 52 53 54 55 |
# File 'lib/lol/request.rb', line 51 def clean_url(url) uri = URI.parse(url) uri.query = CGI.parse(uri.query).reject { |k| k == 'api_key' }.to_query uri.to_s end |
#perform_request(url) ⇒ String
Calls the API via HTTParty and handles errors
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/lol/request.rb', line 60 def perform_request url if cached? && result = store.get(clean_url(url)) return JSON.parse(result) end response = self.class.get(url) if response.respond_to?(:code) && !(200...300).include?(response.code) raise NotFound.new("404 Not Found") if response.not_found? raise TooManyRequests.new('429 Rate limit exceeded') if response.code == 429 raise InvalidAPIResponse.new(url, response) end store.setex clean_url(url), ttl, response.to_json if cached? response end |
#store ⇒ Redis
Returns the cache store
78 79 80 |
# File 'lib/lol/request.rb', line 78 def store cache_store[:redis] end |
#ttl ⇒ Fixnum
Returns the ttl to apply to cached keys.
88 89 90 |
# File 'lib/lol/request.rb', line 88 def ttl cache_store[:ttl] end |