Class: Lol::Request
Overview
Encapsulates common methods for all requests Request classes inherit from this
Direct Known Subclasses
ChampionMasteryRequest, ChampionRequest, LeagueRequest, LolStatusRequest, MasteriesRequest, MatchRequest, RunesRequest, SpectatorRequest, StaticRequest, SummonerRequest, TournamentRequest
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
Api_key.
-
#cache_store ⇒ Object
readonly
@!attribute cache_store.
-
#rate_limiter ⇒ Object
readonly
@!attribute rate_limiter.
-
#region ⇒ String
Region.
Class Method Summary collapse
-
.api_version ⇒ String
Returns the supported API Version.
- .platforms ⇒ Object
Instance Method Summary collapse
-
#api_base_path ⇒ String
Returns the API base path, which is everything between the domain and the request-specific path.
-
#api_base_url ⇒ String
Returns the API base domain.
- #api_query_string(params = {}) ⇒ Object
-
#api_url(path, params = {}) ⇒ String
Returns a full url for an API call.
-
#api_version ⇒ String
Returns the supported API Version.
-
#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 = {}, rate_limiter = nil) ⇒ Request
constructor
Initializes a new Request.
- #perform_rate_limited_request(url, verb = :get, body = nil, options = {}) ⇒ Object
-
#perform_request(url, verb = :get, body = nil, options = {}) ⇒ String
Calls the API via HTTParty and handles errors caching it if a cache is enabled and rate limiting it if a rate limiter is configured.
- #perform_uncached_request(url, verb = :get, body = nil, options = {}) ⇒ Object
- #platform ⇒ Object
-
#store ⇒ Redis
Returns the cache store.
-
#ttl ⇒ Integer
The ttl to apply to cached keys.
Constructor Details
#initialize(api_key, region, cache_store = {}, rate_limiter = nil) ⇒ Request
Initializes a new Request
61 62 63 64 65 66 67 |
# File 'lib/lol/request.rb', line 61 def initialize api_key, region, cache_store = {}, rate_limiter = nil @cache_store = cache_store @rate_limiter = rate_limiter 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 |
#rate_limiter ⇒ Object (readonly)
@!attribute rate_limiter
29 30 31 |
# File 'lib/lol/request.rb', line 29 def rate_limiter @rate_limiter 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
Returns the supported API Version.
33 34 35 |
# File 'lib/lol/request.rb', line 33 def self.api_version "v3" end |
.platforms ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lol/request.rb', line 37 def self.platforms { :br => 'br1', :eune => 'eun1', :euw => 'euw1', :jp => 'jp1', :kr => 'kr', :lan => 'la1', :las => 'la2', :na => 'na1', :oce => 'oc1', :ru => 'ru', :tr => 'tr1', } end |
Instance Method Details
#api_base_path ⇒ String
Returns the API base path, which is everything between the domain and the request-specific path
95 96 97 |
# File 'lib/lol/request.rb', line 95 def api_base_path "/lol/platform/#{api_version}" end |
#api_base_url ⇒ String
Returns the API base domain
89 90 91 |
# File 'lib/lol/request.rb', line 89 def api_base_url "https://#{platform}.api.riotgames.com" end |
#api_query_string(params = {}) ⇒ Object
99 100 101 |
# File 'lib/lol/request.rb', line 99 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
82 83 84 85 |
# File 'lib/lol/request.rb', line 82 def api_url path, params = {} url = File.join File.join(api_base_url, api_base_path), path "#{url}?#{api_query_string params}" end |
#api_version ⇒ String
Returns the supported API Version.
75 76 77 |
# File 'lib/lol/request.rb', line 75 def api_version self.class.api_version end |
#cached? ⇒ Boolean
Returns true if the request should be cached.
166 167 168 |
# File 'lib/lol/request.rb', line 166 def cached? cache_store[:cached] end |
#clean_url(url) ⇒ String
Returns just a path from a full api url
105 106 107 108 109 |
# File 'lib/lol/request.rb', line 105 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_rate_limited_request(url, verb = :get, body = nil, options = {}) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/lol/request.rb', line 129 def perform_rate_limited_request url, verb = :get, body = nil, = {} return perform_uncached_request(url, verb, body, ) unless rate_limiter @rate_limiter.times 1 do return perform_uncached_request(url, verb, body, ) end end |
#perform_request(url, verb = :get, body = nil, options = {}) ⇒ String
Calls the API via HTTParty and handles errors caching it if a cache is enabled and rate limiting it if a rate limiter is configured
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/lol/request.rb', line 118 def perform_request url, verb = :get, body = nil, = {} = .inspect can_cache = [:post, :put].include?(verb) ? false : cached? if can_cache && result = store.get("#{clean_url(url)}#{}") return JSON.parse(result) end response = perform_rate_limited_request(url, verb, body, ) store.setex "#{clean_url(url)}#{}", ttl, response.to_json if can_cache response end |
#perform_uncached_request(url, verb = :get, body = nil, options = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/lol/request.rb', line 136 def perform_uncached_request url, verb = :get, body = nil, = {} [:headers] ||= {} [:headers].merge!({ "Content-Type" => "application/json", "Accept" => "application/json" }) if [:post, :put].include?(verb) [:body] = body.to_json if body [:headers]['X-Riot-Token'] = api_key end response = self.class.send(verb, 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 if response.respond_to?(:parsed_response) response.parsed_response else response end end |
#platform ⇒ Object
69 70 71 |
# File 'lib/lol/request.rb', line 69 def platform self.class.platforms[region.downcase.to_sym] end |
#store ⇒ Redis
Returns the cache store
161 162 163 |
# File 'lib/lol/request.rb', line 161 def store cache_store[:redis] end |
#ttl ⇒ Integer
Returns the ttl to apply to cached keys.
171 172 173 |
# File 'lib/lol/request.rb', line 171 def ttl cache_store[:ttl] end |