motion-locman 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6f0fe017e53fd5fd42b7e8da965f5bba50c2121
4
+ data.tar.gz: d5f238ed5ec67fb62ae601e1b7583531c53d5006
5
+ SHA512:
6
+ metadata.gz: f9754fb1ce8504df4425f02607d52a294a7bdca82e09da8f001aaefeaaf90bc51913cb51fc0f43066f1800e57f333132001270290024f9f7a9b770c6fddc8662
7
+ data.tar.gz: d329b7070fddcb2db215b4c07181663efd1e0f9b3527acb944b336272b885537d1079061407dcd66da61eeaa16e2b1652ef74e29ddfb93a634b9cc291e986c13
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # motion-locman
2
+
3
+ Simple location library for Rubymotion
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "motion-locman"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install motion-locman
18
+
19
+ ## Usage
20
+
21
+ Initialize a new **Locman::Manager**, request for user authorization:
22
+
23
+ ```ruby
24
+ @manager = Locman::Manager.new(
25
+ accuracy: :ten_meters,
26
+ distance_filter: 20 # in meter
27
+ )
28
+
29
+ @manager.after_authorize = lambda do |authorized|
30
+ puts "Authorized!" if authorized
31
+ end
32
+
33
+ @manager.authorize!
34
+ ```
35
+
36
+ Start receiving location updates:
37
+
38
+ ```ruby
39
+ @manager.on_update = lambda do |locations|
40
+ locations.each { |loc| puts "(#{loc.latitude}, #{loc.longitude})" }
41
+ end
42
+
43
+ @manager.start!
44
+ ```
45
+
46
+ ## License
47
+
48
+ MIT
data/lib/locman.rb ADDED
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ unless defined?(Motion::Project::Config)
4
+ raise "This file must be required within a RubyMotion project Rakefile."
5
+ end
6
+
7
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
8
+ Motion::Project::App.setup do |app|
9
+ app.files.unshift(Dir.glob(File.join(lib_dir_path, "locman/**/*.rb")))
10
+ app.frameworks += %w(CoreLocation)
11
+ end
@@ -0,0 +1,23 @@
1
+ module Locman
2
+ class Location
3
+ attr_accessor :latitude, :longitude, :altitude, :floor,
4
+ :accuracy, :altitude_accuracy,
5
+ :determined_at
6
+
7
+ def self.create_from_cl_location(cl_location)
8
+ Locman::Location.new(
9
+ latitude: cl_location.coordinate.latitude,
10
+ longitude: cl_location.coordinate.longitude,
11
+ altitude: cl_location.altitude,
12
+ floor: cl_location.floor.nil? ? nil : cl_location.floor.level,
13
+ accuracy: cl_location.horizontalAccuracy,
14
+ altitude_accuracy: cl_location.verticalAccuracy,
15
+ determined_at: cl_location.timestamp
16
+ )
17
+ end
18
+
19
+ def initialize(params = {})
20
+ params.each { |key, value| send("#{key}=", value) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,124 @@
1
+ module Locman
2
+ class Manager
3
+ attr_accessor :accuracy, :distance_filter, :on_update, :on_error
4
+
5
+ AUTHORIZED_CONSTS = [
6
+ KCLAuthorizationStatusAuthorized,
7
+ KCLAuthorizationStatusAuthorizedAlways,
8
+ KCLAuthorizationStatusAuthorizedWhenInUse
9
+ ]
10
+
11
+ NOT_AUTHORIZED_CONSTS = [
12
+ KCLAuthorizationStatusNotDetermined,
13
+ KCLAuthorizationStatusRestricted,
14
+ KCLAuthorizationStatusDenied
15
+ ]
16
+
17
+ ACCURACY_MAP = {
18
+ navigation: KCLLocationAccuracyBestForNavigation,
19
+ best: KCLLocationAccuracyBest,
20
+ ten_meters: KCLLocationAccuracyNearestTenMeters,
21
+ hundred_meters: KCLLocationAccuracyHundredMeters,
22
+ kilometer: KCLLocationAccuracyKilometer,
23
+ three_kilometers: KCLLocationAccuracyThreeKilometers
24
+ }
25
+
26
+ def initialize(params = {})
27
+ params.each { |key, value| send("#{key}=", value) }
28
+
29
+ @accuracy ||= :best
30
+ @distance_filter ||= 0
31
+ end
32
+
33
+ def accuracy=(accuracy)
34
+ fail(ArgumentError, "Invalid accuracy: #{accuracy}") if ACCURACY_MAP[accuracy].nil?
35
+ @accuracy = accuracy
36
+ end
37
+
38
+ def authorize!
39
+ return true unless CLLocationManager.authorizationStatus == KCLAuthorizationStatusNotDetermined
40
+ manager.requestAlwaysAuthorization
41
+ end
42
+
43
+ def authorized?(status = nil)
44
+ status ||= CLLocationManager.authorizationStatus
45
+
46
+ if AUTHORIZED_CONSTS.include? status
47
+ return true
48
+ elsif NOT_AUTHORIZED_CONSTS.include? status
49
+ return false
50
+ end
51
+
52
+ nil
53
+ end
54
+
55
+ def after_authorize=(after_authorize)
56
+ fail(ArgumentError, "Must provide proc") unless after_authorize.is_a?(Proc)
57
+ @after_authorize = after_authorize
58
+ end
59
+
60
+ def start!(params = {})
61
+ params[:background] ||= false
62
+
63
+ if CLLocationManager.authorizationStatus != KCLAuthorizationStatusAuthorized
64
+ fail(Exception, "Location permission is not authorized by user")
65
+ end
66
+
67
+ manager.desiredAccuracy = ACCURACY_MAP[@accuracy]
68
+ manager.distanceFilter = @distance_filter
69
+ manager.allowsBackgroundLocationUpdates = true if params[:background]
70
+ manager.startUpdatingLocation
71
+ end
72
+
73
+ def stop!
74
+ manager.stopUpdatingLocation
75
+ end
76
+
77
+ def start_monitor!
78
+ manager.startMonitoringSignificantLocationChanges
79
+ end
80
+
81
+ def stop_monitor!
82
+ manager.stopMonitoringSignificantLocationChanges
83
+ end
84
+
85
+ def on_update=(on_update)
86
+ fail(ArgumentError, "Must provide proc") unless on_update.is_a?(Proc)
87
+ @on_update = on_update
88
+ end
89
+
90
+ def on_error=(on_error)
91
+ fail(ArgumentError, "Must provide proc") unless on_error.is_a?(Proc)
92
+ @on_error = on_error
93
+ end
94
+
95
+ # Delegates
96
+
97
+ def locationManager(manager, didChangeAuthorizationStatus: status)
98
+ @after_authorize.call(authorized?(status)) unless @after_authorize.nil?
99
+ end
100
+
101
+ def locationManager(manager, didFailWithError: error)
102
+ @on_error.call(error) unless @on_error.nil?
103
+ end
104
+
105
+ def locationManager(manager, didUpdateLocations: cl_locations)
106
+ locations = cl_locations.map do |cl_location|
107
+ Locman::Location.create_from_cl_location(cl_location)
108
+ end
109
+
110
+ @on_update.call(locations) unless @on_update.nil?
111
+ end
112
+
113
+ private
114
+
115
+ def manager
116
+ @manager ||= begin
117
+ manager = CLLocationManager.new
118
+ manager.delegate = self
119
+
120
+ manager
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1 @@
1
+ require "locman"
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-locman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Minku Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Simple location library for Rubymotion
28
+ email:
29
+ - premist@me.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - lib/locman.rb
36
+ - lib/locman/location.rb
37
+ - lib/locman/manager.rb
38
+ - lib/motion-locman.rb
39
+ homepage: https://github.com/premist/motion-locman
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.4.8
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Simple location library for Rubymotion. Wraps CLLocationManager and more.
63
+ test_files: []