Class: Lol::Request
Overview
Encapsulates common methods for all requests Request classes inherit from this
Direct Known Subclasses
ChampionRequest, GameRequest, LeagueRequest, 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_url(path, params = {}) ⇒ String
Returns a full url for an API call.
-
#cached? ⇒ Boolean
True if the request should be cached.
-
#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
81 82 83 84 85 86 |
# File 'lib/lol/request.rb', line 81 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.
13 14 15 |
# File 'lib/lol/request.rb', line 13 def api_key @api_key end |
#cache_store ⇒ Object (readonly)
@!attribute cache_store
22 23 24 |
# File 'lib/lol/request.rb', line 22 def cache_store @cache_store end |
#region ⇒ String
Returns region.
18 19 20 |
# File 'lib/lol/request.rb', line 18 def region @region end |
Class Method Details
.api_version ⇒ String
Stub method. Each subclass should have its own api version
26 27 28 |
# File 'lib/lol/request.rb', line 26 def self.api_version "v1.1" end |
Instance Method Details
#api_url(path, params = {}) ⇒ String
Returns a full url for an API call
33 34 35 36 |
# File 'lib/lol/request.rb', line 33 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 |
#cached? ⇒ Boolean
Returns true if the request should be cached.
64 65 66 |
# File 'lib/lol/request.rb', line 64 def cached? cache_store[:cached] end |
#perform_request(url) ⇒ String
Calls the API via HTTParty and handles errors
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lol/request.rb', line 41 def perform_request url if cached? && result = store.get(url) return result end 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"] if cached? store.set url, response store.expire url, ttl end response end |
#store ⇒ Redis
Returns the cache store
59 60 61 |
# File 'lib/lol/request.rb', line 59 def store cache_store[:redis] end |
#ttl ⇒ Fixnum
Returns the ttl to apply to cached keys.
69 70 71 |
# File 'lib/lol/request.rb', line 69 def ttl cache_store[:ttl] end |