Class: DNSOMatic::IPStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsomatic/iplookup.rb

Overview

This class handles actually fetching the remotely (not necessarily internet) visible IP for a provided URL. It manages both a minimum delay between checks (default 30m) and a maximum time before it reports a CHANGED status regardless of the result from the remote ip lookup url.

Constant Summary collapse

CHANGED =
true
UNCHANGED =
false
@@opts =
Opts.instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ IPStatus

A URL must be provided that returns the IP of the system that requests the URL. A commonly used example is www.whatismyip.org



18
19
20
21
22
23
24
# File 'lib/dnsomatic/iplookup.rb', line 18

def initialize(url)
  @url = url
  @status = CHANGED #all new lookups are changed.
  @ip = getip()
  @last_update = Time.now
  Logger::log("Fetched new IP #{@ip} from #{@url}.")
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



13
14
15
# File 'lib/dnsomatic/iplookup.rb', line 13

def ip
  @ip
end

Instance Method Details

#changed?Boolean

Tell a client whether or not we have a different IP than the last time we were asked. This may be faked in the case where we’ve reached the the maximum time we allow before forcing an update.

Returns:

  • (Boolean)


29
30
31
# File 'lib/dnsomatic/iplookup.rb', line 29

def changed?
  @status
end

#updateObject

Calling this method requests that we retrieve our IP from the remote URL and potentially alter our status as returned by changed?.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dnsomatic/iplookup.rb', line 35

def update
  if min_elapsed?
    Logger::log("Returned cached IP #{@ip} from #{@url}.")
    @status = UNCHANGED
  else
    ip = getip()
    @last_update = @status ? Time.now : @last_update

    if !@ip.eql?(ip)
      Logger::log("Detected IP change (#{@ip} -> #{ip}) from #{@url}.")
    else
      Logger::log("No IP change detected from #{@url}.")
    end

    @status = (max_elapsed? or !@ip.eql?(ip)) ? CHANGED : UNCHANGED
    @ip = ip
  end
end