class Lol::Request

Encapsulates common methods for all requests Request classes inherit from this

Attributes

api_key[R]

@!attribute [r] api_key @return [String] api_key

cache_store[R]

@!attribute cache_store @return [Object] the cache_store

rate_limiter[R]

@!attribute rate_limiter @return [Object] the rate limiter, if one exists (else nil)

region[RW]

@!attribute [rw] region @return [String] region

Public Class Methods

api_version() click to toggle source

Returns the supported API Version. @return [String] v3

# File lib/lol/request.rb, line 33
def self.api_version
  "v3"
end
new(api_key, region, cache_store = {}) click to toggle source

Initializes a new Request @param api_key [String] the Riot Games API key @param region [String] the region you want to use in API calls @param cache_store [Hash] @option cache_store [Redis] :redis Redis instance to use @option cache_store [Boolean] :cached should the request be cached @option cacche_store [Integer] :ttl ttl for cache keys @return [Request]

# 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
platforms() click to toggle source
# 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

Public Instance Methods

api_base_path() click to toggle source

Returns the API base path, which is everything between the domain and the request-specific path @return [String] API path

# File lib/lol/request.rb, line 95
def api_base_path
  "/lol/platform/#{api_version}"
end
api_base_url() click to toggle source

Returns the API base domain @return [String] path domain

# File lib/lol/request.rb, line 89
def api_base_url
  "https://#{platform}.api.riotgames.com"
end
api_query_string(params = {}) click to toggle source
# 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 = {}) click to toggle source

Returns a full url for an API call @param path [String] API path to call @return [String] full fledged url

# 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() click to toggle source

Returns the supported API Version. @return [String] v3

# File lib/lol/request.rb, line 75
def api_version
  self.class.api_version
end
cached?() click to toggle source

@return [Boolean] true if the request should be cached

# File lib/lol/request.rb, line 166
def cached?
  cache_store[:cached]
end
clean_url(url) click to toggle source

Returns just a path from a full api url @return [String]

# 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 = {}) click to toggle source
# File lib/lol/request.rb, line 129
def perform_rate_limited_request url, verb = :get, body = nil, options = {}
  return perform_uncached_request(url, verb, body, options) unless rate_limiter
  @rate_limiter.times 1 do
    return perform_uncached_request(url, verb, body, options)
  end
end
perform_request(url, verb = :get, body = nil, options = {}) click to toggle source

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 @param url [String] the url to call @param verb [Symbol] HTTP verb to use. Defaults to :get @param body [Hash] Body for POST request @param options [Hash] Options passed to HTTParty @return [String] raw response of the call

# File lib/lol/request.rb, line 118
def perform_request url, verb = :get, body = nil, options = {}
  options_id = options.inspect
  can_cache = [:post, :put].include?(verb) ? false : cached?
  if can_cache && result = store.get("#{clean_url(url)}#{options_id}")
    return JSON.parse(result)
  end
  response = perform_rate_limited_request(url, verb, body, options)
  store.setex "#{clean_url(url)}#{options_id}", ttl, response.to_json if can_cache
  response
end
perform_uncached_request(url, verb = :get, body = nil, options = {}) click to toggle source
# File lib/lol/request.rb, line 136
def perform_uncached_request url, verb = :get, body = nil, options = {}
  options[:headers] ||= {}
  options[:headers].merge!({
    "Content-Type" => "application/json",
    "Accept"       => "application/json"
  })
  if [:post, :put].include?(verb)
    options[:body] = body.to_json if body
    options[:headers]['X-Riot-Token'] = api_key
  end
  response = self.class.send(verb, url, options)
  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() click to toggle source
# File lib/lol/request.rb, line 69
def platform
  self.class.platforms[region.downcase.to_sym]
end
store() click to toggle source

@return [Redis] returns the cache store

# File lib/lol/request.rb, line 161
def store
  cache_store[:redis]
end
ttl() click to toggle source

@return [Integer] the ttl to apply to cached keys

# File lib/lol/request.rb, line 171
def ttl
  cache_store[:ttl]
end