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.



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

def endpoints
  @endpoints
end

.polling_frequencyObject

Returns the value of attribute polling_frequency.



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

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.



37
38
39
# File 'lib/infostrada/endpoints_poller.rb', line 37

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.



32
33
34
# File 'lib/infostrada/endpoints_poller.rb', line 32

def set_frequency(frequency)
  @polling_frequency = frequency
end

Instance Method Details

#process_endpoints(endpoints) ⇒ Object

Raises:



58
59
60
# File 'lib/infostrada/endpoints_poller.rb', line 58

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.



47
48
49
50
51
52
53
54
55
56
# File 'lib/infostrada/endpoints_poller.rb', line 47

def run
  EM.run do
    EM.add_periodic_timer(self.class.polling_frequency) do
      endpoints = EndpointChecker.new(self.class.endpoints)

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