Class: StayAwake::Strategies::Httparty

Inherits:
Object
  • Object
show all
Includes:
Singleton, StayAwake::Strategy
Defined in:
lib/stay_awake/strategies/httparty.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/httparty.rb', line 6

def self.available?
  defined?(HTTParty) ? 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
32
33
# File 'lib/stay_awake/strategies/httparty.rb', line 10

def self.buzz(url)
  require 'timeout'
  StayAwake.logger.info 'Starting buzzing using HTTParty.'

  @thread = Thread.new do
    loop do
      begin
        Timeout.timeout(30) do
          uuid = SecureRandom.uuid
          StayAwake.logger.debug "Starting HTTP Request (#{uuid})."
          HTTParty.send(StayAwake.config.request_method, url)
          StayAwake.logger.debug "HTTP Response arrived (#{uuid})."
          # TODO: Failing request?
        end
      rescue Timeout::Error
        StayAwake.logger.error "HTTP Timeout Error after 30 seconds (#{uuid})."
      rescue StandardError => e
        StayAwake.logger.error e.message
      end

      sleep StayAwake.config.interval
    end
  end
end

.shut_offObject



35
36
37
38
39
# File 'lib/stay_awake/strategies/httparty.rb', line 35

def self.shut_off
  @thread.kill
  @thread = nil
  StayAwake.logger.info 'Stopped buzzing.'
end