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.
-
#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
86 87 88 89 90 91 |
# File 'lib/lol/request.rb', line 86 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.
15 16 17 |
# File 'lib/lol/request.rb', line 15 def api_key @api_key end |
#cache_store ⇒ Object (readonly)
@!attribute cache_store
24 25 26 |
# File 'lib/lol/request.rb', line 24 def cache_store @cache_store end |
#region ⇒ String
Returns region.
20 21 22 |
# File 'lib/lol/request.rb', line 20 def region @region end |
Class Method Details
.api_version ⇒ String
Stub method. Each subclass should have its own api version
28 29 30 |
# File 'lib/lol/request.rb', line 28 def self.api_version "v1.1" end |
Instance Method Details
#api_url(path, params = {}) ⇒ String
Returns a full url for an API call
35 36 37 38 |
# File 'lib/lol/request.rb', line 35 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.
69 70 71 |
# File 'lib/lol/request.rb', line 69 def cached? cache_store[:cached] end |
#clean_url(url) ⇒ String
Returns just a path from a full api url
42 43 44 |
# File 'lib/lol/request.rb', line 42 def clean_url(url) URI.parse(url).path end |
#perform_request(url) ⇒ String
Calls the API via HTTParty and handles errors
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/lol/request.rb', line 49 def perform_request url if cached? && result = store.get(clean_url(url)) return JSON.parse(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"] store.setex clean_url(url), ttl, response.to_json if cached? response end |
#store ⇒ Redis
Returns the cache store
64 65 66 |
# File 'lib/lol/request.rb', line 64 def store cache_store[:redis] end |
#ttl ⇒ Fixnum
Returns the ttl to apply to cached keys.
74 75 76 |
# File 'lib/lol/request.rb', line 74 def ttl cache_store[:ttl] end |