Class: GeoClue::Cache
- Inherits:
-
Object
- Object
- GeoClue::Cache
- Defined in:
- lib/geoclue/cache.rb
Constant Summary collapse
- EXPIRATION =
900
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #address ⇒ Object
- #coordinates ⇒ Object
- #exists? ⇒ Boolean
- #filepath ⇒ Object
- #full? ⇒ Boolean
- #has_address? ⇒ Boolean
- #has_coordinates? ⇒ Boolean
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #read ⇒ Object
- #recent? ⇒ Boolean
- #save(data) ⇒ Object
- #valid? ⇒ Boolean
- #write! ⇒ Object
Constructor Details
#initialize ⇒ Cache
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
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/geoclue/cache.rb', line 5 def data @data end |
Instance Method Details
#address ⇒ Object
31 32 33 |
# File 'lib/geoclue/cache.rb', line 31 def address @data.slice(*Client::ADDR_KEYS) end |
#coordinates ⇒ Object
27 28 29 |
# File 'lib/geoclue/cache.rb', line 27 def coordinates @data.slice(*Client::COORD_KEYS) end |
#exists? ⇒ Boolean
47 48 49 |
# File 'lib/geoclue/cache.rb', line 47 def exists? File.exist?(filepath) end |
#filepath ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/geoclue/cache.rb', line 59 def filepath File.( File.join( ENV.fetch('XDG_CACHE_HOME', File.join(ENV.fetch('HOME', '~/'),'.cache')), 'geoclue-cache.json' ) ) end |
#full? ⇒ Boolean
55 56 57 |
# File 'lib/geoclue/cache.rb', line 55 def full? File.stat(filepath).size > 0 end |
#has_address? ⇒ 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
35 36 37 |
# File 'lib/geoclue/cache.rb', line 35 def has_coordinates? valid? and Client::COORD_KEYS.all? { |key| @data.key?(key) } end |
#read ⇒ Object
14 15 16 |
# File 'lib/geoclue/cache.rb', line 14 def read @data = JSON.parse(File.read(filepath)) end |
#recent? ⇒ 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
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 |