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:



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

Returns api_key.

Returns:

  • (String)

    api_key



13
14
15
# File 'lib/lol/request.rb', line 13

def api_key
  @api_key
end

#cache_storeObject (readonly)

@!attribute cache_store

Returns:

  • (Object)

    the cache_store



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

def cache_store
  @cache_store
end

#regionString

Returns region.

Returns:

  • (String)

    region



18
19
20
# File 'lib/lol/request.rb', line 18

def region
  @region
end

Class Method Details

.api_versionString

Stub method. Each subclass should have its own api version

Returns:

  • (String)

    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

Parameters:

  • path (String)

    API path to call

Returns:

  • (String)

    full fledged url



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.

Returns:

  • (Boolean)

    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

Parameters:

  • url (String)

    the url to call

Returns:

  • (String)

    raw response of the call

Raises:



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

#storeRedis

Returns the cache store

Returns:

  • (Redis)

    returns the cache store



59
60
61
# File 'lib/lol/request.rb', line 59

def store
  cache_store[:redis]
end

#ttlFixnum

Returns the ttl to apply to cached keys.

Returns:

  • (Fixnum)

    the ttl to apply to cached keys



69
70
71
# File 'lib/lol/request.rb', line 69

def ttl
  cache_store[:ttl]
end