Class: DNSOMatic::IPLookup

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

Overview

Any callers wishing to use IPStatus objects should request them via an instance of IPLookup. IPLookup is a singleton object that will cache IPStatus objects across sessions by persisting them to a YAML file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIPLookup

Because we’re a singleton, we don’t take any initialization arguments.



96
97
98
99
100
101
102
# File 'lib/dnsomatic/iplookup.rb', line 96

def initialize
  fn = 'dnsomatic-' + Process.uid.to_s + '.cache'
  @cache_file = File.join(ENV['TEMP'] || '/tmp', fn)

  @cache = Hash.new
  @persist = true
end

Instance Attribute Details

#persistObject

Allow a caller to toggle the cache on and off.



93
94
95
# File 'lib/dnsomatic/iplookup.rb', line 93

def persist
  @persist
end

Instance Method Details

#ip_from_url(url) ⇒ Object

This is the method a caller would use to request and IPStatus object. The url is passed to new() when the object is created or returned from the cache.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dnsomatic/iplookup.rb', line 118

def ip_from_url(url)
  load()
  #implement a simple cache to prevent making multiple http requests
  #to the same remote agent (in the case where a user defines multiple
  #updater stanzas that use the same ip fetch url).
  #because an access for a key that doesn't exist returns and inserts
  #a new IPStatus object, we don't differntiate between seen and unseen
  #here.
  if @cache[url]
    @cache[url].update
  else
    @cache[url] = IPStatus.new(url)
  end

  save()  #ensure that we get spooled to disk.
  @cache[url]
end

#setcachefile(file) ⇒ Object

This allows callers to alter which file we will persist our cache of IPStatus objects to.



106
107
108
109
110
111
112
113
# File 'lib/dnsomatic/iplookup.rb', line 106

def setcachefile(file)
  if !File.writable?(File.dirname(file))
    raise(DNSOMatic::Error, "Unwritable cache file directory.")
  elsif File.exists?(file) and !File.writable(file)
    raise(DNSOMatic::Error, "Unwritable cache file")
  end
  @cache_file = file
end