Class: SteamMist::Connector Abstract

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/steam_mist/connector.rb

Overview

This class is abstract.

Subclass and implement #each to create a connector usable by SteamMist.

TODO:

Test caching.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_uri) ⇒ Connector

This initializes the connector.

Parameters:

  • request_uri (RequestUri)

    the request uri to connect to.



39
40
41
42
43
# File 'lib/steam_mist/connector.rb', line 39

def initialize(request_uri)
  @request_uri = request_uri
  @headers = {}
  @cache = false
end

Instance Attribute Details

#dataHash? (readonly)

This is the data that the connector received from Steam. It may be null if the connector is lazy (in terms of when it grabs data).

Returns:

  • (Hash, nil)


18
19
20
# File 'lib/steam_mist/connector.rb', line 18

def data
  @data
end

#headersHash (readonly)

This is a hash of headers that will be sent upon request.

Returns:

  • (Hash)


34
35
36
# File 'lib/steam_mist/connector.rb', line 34

def headers
  @headers
end

#made_requestBoolean (readonly)

Whether or not the connector has made the request to the API.

Returns:

  • (Boolean)


29
30
31
# File 'lib/steam_mist/connector.rb', line 29

def made_request
  @made_request
end

#request_uriRequestUri (readonly)

This is the request that the connector used to grab information from the steam api.

Returns:



24
25
26
# File 'lib/steam_mist/connector.rb', line 24

def request_uri
  @request_uri
end

Instance Method Details

#[](_) ⇒ Object

Retrieve data from #data. This is normally used to force the connector (if it’s lazy) to grab data.

Parameters:

  • _ (Object)

    the key for data access.

Returns:

  • (Object)

Raises:

  • NotImplementedError if the subclass hasn’t implemented the method.



51
52
53
# File 'lib/steam_mist/connector.rb', line 51

def [](_)
  raise NotImplementedError
end

#cache?Boolean

Whether or not this connector will cache the response.

Returns:

  • (Boolean)


85
86
87
# File 'lib/steam_mist/connector.rb', line 85

def cache?
  !!@cache
end

#disable_cachingfalse

Disables caching on this connector.

Returns:

  • (false)


92
93
94
# File 'lib/steam_mist/connector.rb', line 92

def disable_caching
  @cache = false
end

#eachvoid

This method returns an undefined value.

This loops through the data. It should be implented for enumerable access.

Raises:

  • NotImplementedError if the subclass hasn’t implemented the method.



60
61
62
# File 'lib/steam_mist/connector.rb', line 60

def each
  raise NotImplementedError
end

#enable_caching(path) ⇒ Object

Enables caching on this connector.

Parameters:

  • path (String)

    the path to the cache file.

Returns:

  • (Object)


78
79
80
# File 'lib/steam_mist/connector.rb', line 78

def enable_caching(path)
  @cache = path
end

#force_request!(force = false) ⇒ Hash

If the connector is lazy, this should force the connector to make the request to Steam.

Parameters:

  • force (Boolean) (defaults to: false)

    whether or not to force the request to return fresh data.

Returns:

  • (Hash)

    the data from the request to steam.



70
71
72
# File 'lib/steam_mist/connector.rb', line 70

def force_request!(force = false)
  @data = with_cache(force) { Oj.load(request, :mode => :strict) }
end