Class: GeoClue::Client
- Inherits:
-
Object
- Object
- GeoClue::Client
- 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
- #address(skip_cache: GeoClue.config.skip_cache) ⇒ Object
- #cache ⇒ Object
- #client ⇒ Object
- #client_id ⇒ Object
- #coordinates(skip_cache: GeoClue.config.skip_cache) ⇒ Object
- #full_location ⇒ Object
- #handle_location_update(location_id) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #manager ⇒ Object
- #service ⇒ Object
Constructor Details
#initialize ⇒ Client
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 |
#client ⇒ Object
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_id ⇒ Object
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_location ⇒ Object
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 |
#manager ⇒ Object
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 |
#service ⇒ Object
46 47 48 |
# File 'lib/geoclue/client.rb', line 46 def service @service ||= DBus.system_bus["org.freedesktop.GeoClue2"] end |