Class: StayAwake::Strategies::EmHttpRequest

Inherits:
Object
  • Object
show all
Includes:
Singleton, StayAwake::Strategy
Defined in:
lib/stay_awake/strategies/em_http_request.rb

Class Method Summary collapse

Methods included from StayAwake::Strategy

included

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/stay_awake/strategies/em_http_request.rb', line 6

def self.available?
  defined?(EventMachine::HttpRequest) ? true : false
end

.buzz(url) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stay_awake/strategies/em_http_request.rb', line 10

def self.buzz(url)
  StayAwake.logger.info 'Starting buzzing using EventMachine::HttpRequest.'

  EM.next_tick do
    @timer = EM::PeriodicTimer.new(StayAwake.config.interval) do
      uuid = SecureRandom.uuid
      request = EventMachine::HttpRequest.new(url)
      StayAwake.logger.debug "Starting HTTP Request (#{uuid})."
      request = request.send(StayAwake.config.request_method)

      request.callback do
        StayAwake.logger.debug "HTTP Response arrived (#{uuid})."
      end

      request.errback do
        StayAwake.logger.error "HTTP Request failed (#{uuid})."
      end

      # TODO: Timeout?
    end
  end
end

.shut_offObject



33
34
35
36
37
# File 'lib/stay_awake/strategies/em_http_request.rb', line 33

def self.shut_off
  @timer.cancel
  @timer = nil
  StayAwake.logger.info 'Stopped buzzing.'
end