Class: Lol::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/lol/request.rb

Overview

Encapsulates common methods for all requests Request classes inherit from this

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, region, cache_store = {}) ⇒ Request

Initializes a new Request

Parameters:

  • api_key (String)

    the Riot Games API key

  • region (String)

    the region you want to use in API calls

  • cache_store (Hash) (defaults to: {})
  • cacche_store (Hash)

    a customizable set of options

Options Hash (cache_store):

  • :redis (Redis)

    Redis instance to use

  • :cached (Boolean)

    should the request be cached

Raises:



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_keyString (readonly)

Returns api_key.

Returns:

  • (String)

    api_key



16
17
18
# File 'lib/lol/request.rb', line 16

def api_key
  @api_key
end

#cache_storeObject (readonly)

@!attribute cache_store

Returns:

  • (Object)

    the cache_store



25
26
27
# File 'lib/lol/request.rb', line 25

def cache_store
  @cache_store
end

#regionString

Returns region.

Returns:

  • (String)

    region



21
22
23
# File 'lib/lol/request.rb', line 21

def region
  @region
end

Class Method Details

.api_versionString

Stub method. Each subclass should have its own api version

Returns:

  • (String)

    api version



29
30
31
# File 'lib/lol/request.rb', line 29

def self.api_version
  "v1.1"
end

Instance Method Details

#api_base_urlObject



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

Parameters:

  • path (String)

    API path to call

Returns:

  • (String)

    full fledged url



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.

Returns:

  • (Boolean)

    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

Returns:

  • (String)


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

Parameters:

  • url (String)

    the url to call

  • verb (Symbol) (defaults to: :get)

    HTTP verb to use. Defaults to :get

  • body (Hash) (defaults to: {})

    Body for POST request

  • options (Hash) (defaults to: {})

    Options passed to HTTParty

Returns:

  • (String)

    raw response of the call



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 = {}, options = {}
  if cached? && result = store.get("#{clean_url(url)}#{options.inspect}")
    return JSON.parse(result)
  end

  params = verb == :post ? [url, options.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)}#{options.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

#storeRedis

Returns the cache store

Returns:

  • (Redis)

    returns the cache store



94
95
96
# File 'lib/lol/request.rb', line 94

def store
  cache_store[:redis]
end

#ttlFixnum

Returns the ttl to apply to cached keys.

Returns:

  • (Fixnum)

    the ttl to apply to cached keys



104
105
106
# File 'lib/lol/request.rb', line 104

def ttl
  cache_store[:ttl]
end