class Locman::Manager

This wraps CLLocationManager in more Ruby way.

Constants

ACCURACY_MAP

@!visibility private

AUTHORIZED_CONSTS

@!visibility private

NOT_AUTHORIZED_CONSTS

@!visibility private

Attributes

accuracy[RW]

@return [Symbol] Desired accuracy of the location data.

background[RW]

@return [Boolean] Set whether location should be updated in the background or not.

distance_filter[RW]

@return [Integer] The minimum horizontal distance threshold for on_update event.

on_error[RW]

@return [Proc] Proc function that will be called when there is an error while retrieving the location.

on_update[RW]

@return [Proc] Proc function that will be called when there is a new location retrieval.

on_visit[RW]

@return [Proc] Proc function that will be called when there is a new visit event.

Public Class Methods

new(params = {}) click to toggle source

Creates a new Locman::Location instance. @param options [Hash] Attributes that will be assigned on instance creation @return [Locman::Manager]

# File lib/locman/manager.rb, line 49
def initialize(params = {})
  params.each { |key, value| send("#{key}=", value) }

  @accuracy ||= :best
  @distance_filter ||= 0

  self
end

Public Instance Methods

accuracy=(accuracy) click to toggle source

Sets a desired accuracy. @param accuracy [Symbol] Desired accuracy of the location data.

# File lib/locman/manager.rb, line 60
def accuracy=(accuracy)
  fail(ArgumentError, "Invalid accuracy: #{accuracy}") if ACCURACY_MAP[accuracy].nil?
  manager.desiredAccuracy = ACCURACY_MAP[accuracy]
  @accuracy = accuracy
end
after_authorize=(after_authorize) click to toggle source
# File lib/locman/manager.rb, line 82
def after_authorize=(after_authorize)
  fail(ArgumentError, "Must provide proc") unless after_authorize.is_a?(Proc)
  @after_authorize = after_authorize
end
authorize!() click to toggle source
# File lib/locman/manager.rb, line 102
def authorize!
  return true unless CLLocationManager.authorizationStatus == KCLAuthorizationStatusNotDetermined
  manager.requestAlwaysAuthorization
end
authorized?(status = nil) click to toggle source
# File lib/locman/manager.rb, line 107
def authorized?(status = nil)
  status ||= CLLocationManager.authorizationStatus

  if AUTHORIZED_CONSTS.include? status
    return true
  elsif NOT_AUTHORIZED_CONSTS.include? status
    return false
  end

  nil
end
background=(background) click to toggle source

Sets whether location should be updated on the background or not.

# File lib/locman/manager.rb, line 73
def background=(background)
  if !background.is_a?(TrueClass) && !background.is_a?(FalseClass)
    fail(ArgumentError, "Background should be boolean")
  end

  manager.allowsBackgroundLocationUpdates = background
  @background = background
end
distance_filter=(distance_filter) click to toggle source
# File lib/locman/manager.rb, line 66
def distance_filter=(distance_filter)
  fail(ArgumentError, "Distance filter should be integer") unless distance_filter.is_a?(Integer)
  manager.distanceFilter = distance_filter
  @distance_filter = distance_filter
end
locationManager(manager, didChangeAuthorizationStatus: status) click to toggle source

Delegates

# File lib/locman/manager.rb, line 149
def locationManager(manager, didChangeAuthorizationStatus: status)
  @after_authorize.call(authorized?(status)) unless @after_authorize.nil?
end
on_error=(on_error) click to toggle source
# File lib/locman/manager.rb, line 92
def on_error=(on_error)
  fail(ArgumentError, "Must provide proc") unless on_error.is_a?(Proc)
  @on_error = on_error
end
on_update=(on_update) click to toggle source
# File lib/locman/manager.rb, line 87
def on_update=(on_update)
  fail(ArgumentError, "Must provide proc") unless on_update.is_a?(Proc)
  @on_update = on_update
end
on_visit=(on_visit) click to toggle source
# File lib/locman/manager.rb, line 97
def on_visit=(on_visit)
  fail(ArgumentError, "Must provide proc") unless on_visit.is_a?(Proc)
  @on_visit = on_visit
end
stop_update!() click to toggle source
# File lib/locman/manager.rb, line 127
def stop_update!
  manager.stopUpdatingLocation
end
stop_update_significant!() click to toggle source
# File lib/locman/manager.rb, line 135
def stop_update_significant!
  manager.stopMonitoringSignificantLocationChanges
end
stop_update_visits!() click to toggle source
# File lib/locman/manager.rb, line 143
def stop_update_visits!
  manager.stopMonitoringVisits
end
update!() click to toggle source
# File lib/locman/manager.rb, line 119
def update!
  if CLLocationManager.authorizationStatus != KCLAuthorizationStatusAuthorized
    fail(Exception, "Location permission is not authorized by user")
  end

  manager.startUpdatingLocation
end
update_significant!() click to toggle source
# File lib/locman/manager.rb, line 131
def update_significant!
  manager.startMonitoringSignificantLocationChanges
end
update_visits!() click to toggle source
# File lib/locman/manager.rb, line 139
def update_visits!
  manager.startMonitoringVisits
end

Private Instance Methods

manager() click to toggle source
# File lib/locman/manager.rb, line 173
def manager
  @manager ||= begin
    manager = CLLocationManager.new
    manager.delegate = self

    manager
  end
end