Class: GeoLocLog

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

Overview

options:

file: used to store the place names and the coordinates
wait: records can only be added after the given wait time in seconds
labels: Maps the returned place name with a user-defined label (optional)
dbfile: sqlite database file used to store an addresses with a position
timeout: number of seconds to wait before the Geocoder gem 
         timesout when accessing the Google API

Instance Method Summary collapse

Constructor Details

#initialize(file = 'geoloclog.xml', wait: 60, labels: nil, dbfile: 'glw.db', timeout: 10) ⇒ GeoLocLog

Returns a new instance of GeoLocLog.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/geoloclog.rb', line 21

def initialize(file='geoloclog.xml', wait: 60, labels: nil, dbfile: 'glw.db', 
              timeout: 10)

  @filename, @wait = file, wait
 
  @labels = labels ? YAML.load(RXFHelper.read(labels).first) : {}

  @dx = if File.exists? file then
    DynarexDaily.new file
  else
    dx = DynarexDaily.new 'geoloc[title, date]/location(place, label, ' + 
             'entered, lastseen, address, coords)', filename: @filename
    dx.default_key = 'uid'
    dx
  end

  @glw = Glw.new dbfile, timeout: timeout

end

Instance Method Details

#add(lat, lon) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/geoloclog.rb', line 41

def add(lat, lon)

  return if location and  (Time.now - Time.parse(location.created) < @wait)

  begin
    h = @glw.locate lat, lon
  rescue
    puts 'geoloclog::add warning: ' + ($!).inspect
    return
  end

  record = {
    place: h[:route],
    label: @labels[h[:route]].to_s,
    entered: Time.now,
    lastseen: Time.now,
    address: h[:address],
    coords: [lat, lon].join(', ')
  }

  @dx.create record
  @dx.save @filename

end

#last_locationObject Also known as: location



66
67
68
# File 'lib/geoloclog.rb', line 66

def last_location()
  @dx.all.any? ? @dx.all.first : nil
end