Class: GeoClue::Client

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

Constant Summary collapse

COORD_KEYS =
%w[latitude longitude accuracy].freeze
ADDR_KEYS =
%w[house_number road settlement city town village county state country postcode country_code].freeze

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



8
9
10
# File 'lib/geoclue/client.rb', line 8

def initialize
  @data = {}
end

Instance Method Details

#address(skip_cache: GeoClue.config.skip_cache) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/geoclue/client.rb', line 34

def address(skip_cache: GeoClue.config.skip_cache)
  return cache.address if !skip_cache && cache.has_address?

  addr = ReverseGeocoder.geocode(coordinates).slice(*ADDR_KEYS)
  cache.save(addr)
  addr
end

#cacheObject



42
43
44
# File 'lib/geoclue/client.rb', line 42

def cache
  @cache ||= Cache.new
end

#clientObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/geoclue/client.rb', line 62

def client
  @client ||= begin
                object = service[client_id]
                interface = object["org.freedesktop.GeoClue2.Client"]
                interface["DesktopId"] = "geoclue-ruby"
                interface["RequestedAccuracyLevel"] = ['u', 8]
                interface.on_signal("LocationUpdated") do
                  begin
                    handle_location_update(interface["Location"])
                  rescue DBus::InvalidPacketException
                    retry
                  end
                end
                Thread.new { interface["Location"] }
                object
              end
end

#client_idObject



58
59
60
# File 'lib/geoclue/client.rb', line 58

def client_id
  @client_id ||= manager.CreateClient
end

#coordinates(skip_cache: GeoClue.config.skip_cache) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/geoclue/client.rb', line 16

def coordinates(skip_cache: GeoClue.config.skip_cache)
  return cache.coordinates if !skip_cache && cache.has_coordinates?

  client.Start
  start = Time.now
  loop do
    sleep 0.1
    break if @data.key?("accuracy") and @data["accuracy"] < 500
    break if Time.now - start > GeoClue.config.timeout
  end

  result = @data.slice(*COORD_KEYS)
  raise "Unable to find location" if result.empty?

  cache.save(@data)
  result
end

#full_locationObject



12
13
14
# File 'lib/geoclue/client.rb', line 12

def full_location
  coordinates.merge(address)
end

#handle_location_update(location_id) ⇒ Object



80
81
82
83
84
# File 'lib/geoclue/client.rb', line 80

def handle_location_update(location_id)
  location = service[location_id]
  location["org.freedesktop.GeoClue2.Location"]
  @data = location.GetAll("org.freedesktop.GeoClue2.Location").transform_keys(&:downcase)
end

#managerObject



50
51
52
53
54
55
56
# File 'lib/geoclue/client.rb', line 50

def manager
  @manager ||= begin
                 object = service["/org/freedesktop/GeoClue2/Manager"]
                 object["org.freedesktop.GeoClue2.Manager"]
                 object
               end
end

#serviceObject



46
47
48
# File 'lib/geoclue/client.rb', line 46

def service
  @service ||= DBus.system_bus["org.freedesktop.GeoClue2"]
end