Class: Bluemoon::Location

Inherits:
Struct
  • Object
show all
Defined in:
lib/bluemoon/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#coordinatesObject

Returns the value of attribute coordinates

Returns:

  • (Object)

    the current value of coordinates



2
3
4
# File 'lib/bluemoon/location.rb', line 2

def coordinates
  @coordinates
end

Class Method Details

.currentObject



4
5
6
# File 'lib/bluemoon/location.rb', line 4

def current
  @@current ||= self.new(JSON.parse(`get-location -f json`)['coordinates'])
end

.debugObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bluemoon/location.rb', line 8

def debug
  puts "Querying location. This generally takes 10-20 seconds. Press any key to skip."

  Thread.new do
    display_debug_message(
      Area.all.join(', '),
      Area::PROXIMITY,
      current.to_s,
    )

    Kernel.exit
  end

  if $stdin.gets.length > 0
    display_debug_message(OFFICE.join(', '), PROXIMITY)
  end
end

.display_debug_message(office, proximity, location = 'Skipped', distance = '?', in_range = 'Maybe') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bluemoon/location.rb', line 26

def display_debug_message(office, proximity, location = 'Skipped', distance = '?', in_range = 'Maybe')
   puts <<-DEBUG

    Office:     #{office}
    Proximity:  You must be #{proximity} meters from the office to enable bluetooth.
    Location:   #{location}
    Distance:   #{distance} meters
    In range:   #{in_range}

    DEBUG
end

Instance Method Details

#distance(from) ⇒ Object

Kindly stolen wholesale from stackoverflow.com/a/12969617/615412



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bluemoon/location.rb', line 41

def distance(from)
  a, b = coordinates.dup, from

  rad_per_deg = Math::PI/180  # PI / 180
  rkm = 6371                  # Earth radius in kilometers
  rm = rkm * 1000             # Radius in meters

  dlon_rad = (b[1]-a[1]) * rad_per_deg  # Delta, converted to rad
  dlat_rad = (b[0]-a[0]) * rad_per_deg

  lat1_rad, lon1_rad = a.map! {|i| i * rad_per_deg }
  lat2_rad, lon2_rad = b.map! {|i| i * rad_per_deg }

  a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2
  c = 2 * Math.asin(Math.sqrt(a))

  rm * c # Delta in meters
end

#openObject



60
61
62
# File 'lib/bluemoon/location.rb', line 60

def open
  `open #{url}`
end

#to_sObject



64
65
66
# File 'lib/bluemoon/location.rb', line 64

def to_s
  "#{coordinates.join(', ')}"
end

#urlObject



68
69
70
# File 'lib/bluemoon/location.rb', line 68

def url
  "https://www.google.com/maps/@#{coordinates.join(',')},15z"
end