Class: GeoClue::Cache

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

Constant Summary collapse

EXPIRATION =
900

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



9
10
11
12
# File 'lib/geoclue/cache.rb', line 9

def initialize
  @data = {}
  read if valid?
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/geoclue/cache.rb', line 5

def data
  @data
end

Instance Method Details

#addressObject



31
32
33
# File 'lib/geoclue/cache.rb', line 31

def address
  @data.slice(*Client::ADDR_KEYS)
end

#coordinatesObject



27
28
29
# File 'lib/geoclue/cache.rb', line 27

def coordinates
  @data.slice(*Client::COORD_KEYS)
end

#exists?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/geoclue/cache.rb', line 47

def exists?
  File.exist?(filepath)
end

#filepathObject



59
60
61
62
63
64
65
66
67
# File 'lib/geoclue/cache.rb', line 59

def filepath
  File.expand_path(
    File.join(
      ENV.fetch('XDG_CACHE_HOME',
                File.join(ENV.fetch('HOME', '~/'),'.cache')),
      'geoclue-cache.json'
    )
  )
end

#full?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/geoclue/cache.rb', line 55

def full?
  File.stat(filepath).size > 0
end

#has_address?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/geoclue/cache.rb', line 39

def has_address?
  valid? and Client::ADDR_KEYS.all? { |key| @data.key?(key) }
end

#has_coordinates?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/geoclue/cache.rb', line 35

def has_coordinates?
  valid? and Client::COORD_KEYS.all? { |key| @data.key?(key) }
end

#readObject



14
15
16
# File 'lib/geoclue/cache.rb', line 14

def read
  @data = JSON.parse(File.read(filepath))
end

#recent?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/geoclue/cache.rb', line 51

def recent?
  (Time.now - File.mtime(filepath)) < EXPIRATION
end

#save(data) ⇒ Object



18
19
20
21
# File 'lib/geoclue/cache.rb', line 18

def save(data)
  @data.merge!(data)
  write!
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/geoclue/cache.rb', line 43

def valid?
  exists? and recent? and full?
end

#write!Object



23
24
25
# File 'lib/geoclue/cache.rb', line 23

def write!
  File.write(filepath, JSON.dump(@data))
end