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, TournamentProviderRequest
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, verb = :get, body = {}, options = {}) ⇒ String
Calls the API via HTTParty and handles errors.
- #post_api_url(path, params = {}) ⇒ Object
-
#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
116 117 118 119 120 121 |
# File 'lib/lol/request.rb', line 116 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
53 54 55 |
# File 'lib/lol/request.rb', line 53 def api_base_url "https://#{region}.api.pvp.net" end |
#api_query_string(params = {}) ⇒ Object
57 58 59 |
# File 'lib/lol/request.rb', line 57 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.
99 100 101 |
# File 'lib/lol/request.rb', line 99 def cached? cache_store[:cached] end |
#clean_url(url) ⇒ String
Returns just a path from a full api url
63 64 65 66 67 |
# File 'lib/lol/request.rb', line 63 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, verb = :get, body = {}, options = {}) ⇒ String
Calls the API via HTTParty and handles errors
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/lol/request.rb', line 75 def perform_request url, verb = :get, body = {}, = {} if cached? && result = store.get("#{clean_url(url)}#{.inspect}") return JSON.parse(result) end params = verb == :post ? [url, .merge({body: body.to_json})] : url response = self.class.send(verb, *params) 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)}#{.inspect}", ttl, response.to_json if cached? response end |
#post_api_url(path, params = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lol/request.rb', line 41 def post_api_url path, params = {} { url: api_url(path, params), options: { headers: { "X-Riot-Token" => api_key, "Content-Type" => "application/json" } } } end |
#store ⇒ Redis
Returns the cache store
94 95 96 |
# File 'lib/lol/request.rb', line 94 def store cache_store[:redis] end |
#ttl ⇒ Fixnum
Returns the ttl to apply to cached keys.
104 105 106 |
# File 'lib/lol/request.rb', line 104 def ttl cache_store[:ttl] end |