Class: Infostrada::EndpointsPoller

Inherits:
Object
  • Object
show all
Defined in:
lib/infostrada/endpoints_poller.rb

Overview

This is the base class that you should subclass to make a new endpoints poller. You should also call the EndpointsPoller.set_frequency and EndpointsPoller.listen_to and define a process_endpoints(endpoints) that will be called when there are updates on the list of endpoints defined via listen_to. EndpointsPoller will use CallRefresh behind the scenes to fetch the updated endpoints.

Example of a minimal poller:

module Infostrada

class GamesPoller < EndpointsPoller
  # What's the request frequency, in seconds.
  set_frequency 15

  # What are the endpoints that we're interested in?
  listen_to %w(GetMatchActionList_All)

  def process_endpoints(endpoints)
  end
end

end

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.endpointsObject

Returns the value of attribute endpoints.



26
27
28
# File 'lib/infostrada/endpoints_poller.rb', line 26

def endpoints
  @endpoints
end

.polling_frequencyObject

Returns the value of attribute polling_frequency.



26
27
28
# File 'lib/infostrada/endpoints_poller.rb', line 26

def polling_frequency
  @polling_frequency
end

Class Method Details

.listen_to(endpoints) ⇒ Object

Also called on the cubclasses to set what are the endpoints that we will parse.



34
35
36
# File 'lib/infostrada/endpoints_poller.rb', line 34

def listen_to(endpoints)
  @endpoints = endpoints
end

.set_frequency(frequency) ⇒ Object

Called on the sublcasses to set what’s the interval (in seconds) for the requests.



29
30
31
# File 'lib/infostrada/endpoints_poller.rb', line 29

def set_frequency(frequency)
  @polling_frequency = frequency
end

Instance Method Details

#process_endpoints(endpoints) ⇒ Object

Raises:

  • (InfostradaError)


54
55
56
# File 'lib/infostrada/endpoints_poller.rb', line 54

def process_endpoints(endpoints)
  raise InfostradaError, 'You have to override process_endpoints method!'
end

#runObject

Main cycle that calls EM.run and will then use the EndpointsPoller#process_endpoints method to process the updated endpoints.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/infostrada/endpoints_poller.rb', line 41

def run
  EM.run do
    EM.add_periodic_timer(self.class.polling_frequency) do
      puts "[#{Time.now}] Refresh since #{CallRefresh.last_update}"

      endpoints = EndpointChecker.new(self.class.endpoints)

      endpoints.callback { |endpoints| process_endpoints(endpoints) }
      endpoints.errback { |error| puts "ERROR: #{error}" }
    end
  end
end