geoclue 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 274f5899e09763cc0f4824b24d12bf4f65b4c92176f117f32aeaa4d3236fe3af
4
+ data.tar.gz: 835eba7f05e7f8de523cc8fc8b947ecc1ef90306eb9abe2a0e5d42c33d8fd602
5
+ SHA512:
6
+ metadata.gz: 1586f95753c3af230e65b810137f03470fcbe898aa4b35e10dd3823074ada5a7f7451978f4fa3b3436bb66f83a8b70086dc21f19ef3dd5e2afddc11caea39bf3
7
+ data.tar.gz: 4125ccfcebc3cfd00586c54032cc97ce15ab77d6b5534a571128ed82795ec5fb39169a8b761704b74eecee159bed5d2ade0656845ee344f0ef6a607f3870aaf5
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to the project will be documented here.
4
+
5
+ ## [0.1.0] - 2019-06-08
6
+
7
+ Initial implementation
8
+
9
+ ### Added
10
+ - Fetch coordinates from GeoClue via DBus
11
+ - Reverse geocode coordinates to an address
12
+ - CLI to access this functionality
13
+ - Tests, mostly related to cache
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in geoclue.gemspec
6
+ gemspec
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ geoclue (0.1.0)
5
+ ruby-dbus (~> 0.15.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ minitest (5.11.3)
11
+ rake (10.5.0)
12
+ ruby-dbus (0.15.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.17)
19
+ geoclue!
20
+ minitest (~> 5.0)
21
+ rake (~> 10.0)
22
+
23
+ BUNDLED WITH
24
+ 1.17.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Jacob Evan Shreve
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # GeoClue Ruby Client
2
+
3
+ This library allows you to interact with GeoClue via D-Bus. This allows you to get the physical location of your computer just based on your WiFi connection.
4
+
5
+ For further reference:
6
+
7
+ * [GeoClue Project Description](https://gitlab.freedesktop.org/geoclue/geoclue/wikis/home)
8
+ * [The GeoClue D-Bus API](https://www.freedesktop.org/software/geoclue/docs/)
9
+
10
+ This project is currently only developed and tested on Arch Linux, with plans to port to Ubuntu in the near future. It exists on Arch first because the Ubuntu GeoClue provider is so easy to use, I originally used it from [a simple bash script](https://github.com/shreve/dotfiles/blob/master/bin/location).
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'geoclue'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install geoclue
27
+
28
+ Make sure that you have GeoClue installed and running:
29
+
30
+ $ sudo systemctl status geoclue
31
+
32
+ If not, it should be available from your distro's package manager:
33
+
34
+ $ pacman -S geoclue
35
+
36
+ ## Usage
37
+
38
+ Run the executable to get your coordinates
39
+
40
+ $ geoclue
41
+ 42.12345,-83.12345
42
+
43
+ ## Development
44
+
45
+ I recommend having a D-Bus debugger tool like [d-feet](https://wiki.gnome.org/Apps/DFeet) in order to inspect the service API. This tool relies entirely on the `org.freedesktop.GeoClue2` system service.
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shreve/geoclue-ruby.
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "geoclue"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "geoclue"
5
+
6
+ if (ARGV & %w[-h --help]).any?
7
+ puts "Usage:"
8
+ puts " geoclue [option flags]"
9
+ puts ""
10
+ puts " Running with no options will print comma-separated coordinates."
11
+ puts ""
12
+ puts " -h --help\t\t print this message"
13
+ puts ""
14
+ puts "INFORMATION OPTIONS"
15
+ puts " -f --full\t\t fetch all location information"
16
+ puts " -a --address\t\t fetch address information (requires fetching coordinates)"
17
+ puts " -c --coordinates\t fetch your coordinates from GeoClue"
18
+ puts ""
19
+ puts "CONFIGURATION OPTIONS"
20
+ puts " -t --timeout [int]\t set the timeout for each operation"
21
+ puts " -n --no-cache\t\t don't use the cache"
22
+ puts " -d --print-config\t print out all configuration"
23
+ puts " --format [fmt]\t output format of data in named printf format"
24
+ puts " \t named options include \"tab\" and \"pipe\""
25
+ puts " \t default is \"%<key>s: %<val>s\\n\""
26
+ exit 0
27
+ end
28
+
29
+ def has_arg(*names)
30
+ (ARGV & names).any?
31
+ end
32
+
33
+ def fetch_arg(*names, default)
34
+ names.each do |name|
35
+ index = ARGV.index(name)
36
+ next unless index
37
+ return ARGV[index + 1] || default
38
+ end
39
+ default
40
+ end
41
+
42
+ GeoClue.config do |c|
43
+ c.skip_cache = has_arg('-n', '--no-cache')
44
+ c.timeout = fetch_arg('-t', '--timeout', 10).to_i
45
+ c.format = fetch_arg('--format', "%<key>s: %<val>s\n")
46
+ c.format = case c.format
47
+ when "tab"
48
+ "%<key>s\t%<val>s\n"
49
+ when "pipe"
50
+ "%<key>s|%<val>s\n"
51
+ else
52
+ c.format
53
+ end
54
+
55
+ end
56
+
57
+ if has_arg('-d', '--print-config')
58
+ puts GeoClue.config.inspect
59
+ puts ""
60
+ end
61
+
62
+ print_pair = -> (key, val) {
63
+ printf GeoClue.config.format, { key: key, val: val }
64
+ }
65
+
66
+ begin
67
+ if has_arg('-f', '--full')
68
+ GeoClue.full_location.each_pair(&print_pair)
69
+ elsif has_arg('-a', '--address')
70
+ GeoClue.address.each_pair(&print_pair)
71
+ elsif has_arg('-c', '--coordinates')
72
+ GeoClue.coordinates.each_pair(&print_pair)
73
+ else
74
+ data = GeoClue.coordinates
75
+ puts "#{data["latitude"]},#{data["longitude"]}"
76
+ end
77
+ rescue => e
78
+ puts e.message
79
+ exit 1
80
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,44 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "geoclue/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "geoclue"
8
+ spec.version = GeoClue::VERSION
9
+ spec.authors = ["Jacob Evan Shreve"]
10
+ spec.email = ["github@shreve.io"]
11
+
12
+ spec.summary = %q{Access your computer's geographic coordinates}
13
+ spec.description = %q{Use GeoClue to get geographic coordinates for your computer based soely on your WiFi connection}
14
+ spec.homepage = "https://github.com/shreve/geoclue-ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = spec.homepage
24
+ spec.metadata["changelog_uri"] = spec.homepage + "/blob/master/CHANGELOG.md"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency "ruby-dbus", "~> 0.15.0"
40
+
41
+ spec.add_development_dependency "bundler", "~> 1.17"
42
+ spec.add_development_dependency "rake", "~> 10.0"
43
+ spec.add_development_dependency "minitest", "~> 5.0"
44
+ end
@@ -0,0 +1,31 @@
1
+ require "geoclue/version"
2
+ require "geoclue/cache"
3
+ require "geoclue/config"
4
+ require "geoclue/client"
5
+ require "geoclue/reverse_geocoder"
6
+
7
+ module GeoClue
8
+ class << self
9
+ def coordinates
10
+ client.coordinates
11
+ end
12
+
13
+ def address
14
+ client.address
15
+ end
16
+
17
+ def full_location
18
+ client.full_location
19
+ end
20
+
21
+ def client
22
+ @client ||= Client.new
23
+ end
24
+
25
+ def config(&block)
26
+ @config ||= Config.new
27
+ @config.evaluate(&block)
28
+ @config
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,67 @@
1
+ require 'json'
2
+
3
+ module GeoClue
4
+ class Cache
5
+ attr_accessor :data
6
+
7
+ def initialize
8
+ @data = {}
9
+ read if valid?
10
+ end
11
+
12
+ def read
13
+ @data = JSON.parse(File.read(filepath))
14
+ end
15
+
16
+ def save(data)
17
+ @data.merge!(data)
18
+ write!
19
+ end
20
+
21
+ def write!
22
+ File.write(filepath, JSON.dump(@data))
23
+ end
24
+
25
+ def coordinates
26
+ @data.slice(*Client::COORD_KEYS)
27
+ end
28
+
29
+ def address
30
+ @data.slice(*Client::ADDR_KEYS)
31
+ end
32
+
33
+ def has_coordinates?
34
+ valid? and Client::COORD_KEYS.all? { |key| @data.key?(key) }
35
+ end
36
+
37
+ def has_address?
38
+ valid? and Client::ADDR_KEYS.all? { |key| @data.key?(key) }
39
+ end
40
+
41
+ def valid?
42
+ exists? and recent? and full?
43
+ end
44
+
45
+ def exists?
46
+ File.exist?(filepath)
47
+ end
48
+
49
+ def recent?
50
+ (Time.now - File.mtime(filepath)) < 600
51
+ end
52
+
53
+ def full?
54
+ File.stat(filepath).size > 0
55
+ end
56
+
57
+ def filepath
58
+ File.expand_path(
59
+ File.join(
60
+ ENV.fetch('XDG_CACHE_HOME',
61
+ File.join(ENV.fetch('HOME', '~/'),'.cache')),
62
+ 'geoclue-cache.json'
63
+ )
64
+ )
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,82 @@
1
+ require 'dbus'
2
+
3
+ module GeoClue
4
+ class Client
5
+ COORD_KEYS = %w[latitude longitude accuracy].freeze
6
+ ADDR_KEYS = %w[house_number road city county state country postcode country_code].freeze
7
+
8
+ def initialize
9
+ @data = {}
10
+ end
11
+
12
+ def full_location
13
+ coordinates.merge(address)
14
+ end
15
+
16
+ def coordinates(skip_cache: GeoClue.config.skip_cache)
17
+ return cache.coordinates if !skip_cache && cache.has_coordinates?
18
+
19
+ client.Start
20
+ start = Time.now
21
+ loop do
22
+ sleep 0.1
23
+ break if @data.key?("accuracy") and @data["accuracy"] < 500
24
+ break if Time.now - start > GeoClue.config.timeout
25
+ end
26
+
27
+ result = @data.slice(*COORD_KEYS)
28
+ raise "Unable to find location" if result.empty?
29
+
30
+ cache.save(@data)
31
+ result
32
+ end
33
+
34
+ def address(skip_cache: GeoClue.config.skip_cache)
35
+ return cache.address if !skip_cache && cache.has_address?
36
+
37
+ addr = ReverseGeocoder.geocode(coordinates).slice(*ADDR_KEYS)
38
+ cache.save(addr)
39
+ addr
40
+ end
41
+
42
+ def cache
43
+ @cache ||= Cache.new
44
+ end
45
+
46
+ def service
47
+ @service ||= DBus.system_bus["org.freedesktop.GeoClue2"]
48
+ end
49
+
50
+ def manager
51
+ @manager ||= begin
52
+ object = service["/org/freedesktop/GeoClue2/Manager"]
53
+ interface = object["org.freedesktop.GeoClue2.Manager"]
54
+ object
55
+ end
56
+ end
57
+
58
+ def client_id
59
+ @client_id ||= manager.CreateClient
60
+ end
61
+
62
+ def client
63
+ @client ||= begin
64
+ object = service[client_id]
65
+ interface = object["org.freedesktop.GeoClue2.Client"]
66
+ interface["DesktopId"] = "geoclue-ruby"
67
+ interface["RequestedAccuracyLevel"] = ['u', 8]
68
+ interface.on_signal("LocationUpdated") do
69
+ handle_location_update(interface["Location"])
70
+ end
71
+ Thread.new { interface["Location"] }
72
+ object
73
+ end
74
+ end
75
+
76
+ def handle_location_update(location_id)
77
+ location = service[location_id]
78
+ interface = location["org.freedesktop.GeoClue2.Location"]
79
+ @data = location.GetAll("org.freedesktop.GeoClue2.Location").transform_keys(&:downcase)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,21 @@
1
+ module GeoClue
2
+ class Config
3
+ attr_accessor :timeout, :format, :skip_cache
4
+
5
+ def initialize
6
+ self.timeout = 10
7
+ self.skip_cache = false
8
+ end
9
+
10
+ def evaluate(&block)
11
+ return unless block_given?
12
+ instance_eval(&block)
13
+ end
14
+
15
+ def inspect
16
+ %i[timeout format skip_cache].each.map do |var|
17
+ "#{var}: #{send(var).inspect}"
18
+ end.join("\n")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ require 'net/http'
2
+
3
+ module GeoClue
4
+ module ReverseGeocoder
5
+ class << self
6
+ def geocode(coords)
7
+ url = "https://locationiq.com/v1/reverse_sandbox.php?format=json&lat=#{coords["latitude"]}&lon=#{coords["longitude"]}"
8
+ uri = URI(url)
9
+ result = Net::HTTP.get(uri)
10
+ JSON.parse(result)["address"]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module GeoClue
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geoclue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Evan Shreve
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-dbus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.15.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.15.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: Use GeoClue to get geographic coordinates for your computer based soely
70
+ on your WiFi connection
71
+ email:
72
+ - github@shreve.io
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - CHANGELOG.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/geoclue
87
+ - bin/setup
88
+ - geoclue.gemspec
89
+ - lib/geoclue.rb
90
+ - lib/geoclue/cache.rb
91
+ - lib/geoclue/client.rb
92
+ - lib/geoclue/config.rb
93
+ - lib/geoclue/reverse_geocoder.rb
94
+ - lib/geoclue/version.rb
95
+ homepage: https://github.com/shreve/geoclue-ruby
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ homepage_uri: https://github.com/shreve/geoclue-ruby
100
+ source_code_uri: https://github.com/shreve/geoclue-ruby
101
+ changelog_uri: https://github.com/shreve/geoclue-ruby/blob/master/CHANGELOG.md
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.0.3
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Access your computer's geographic coordinates
121
+ test_files: []