class CompassPoint

Constants

COMPASS_BEARING_REGEX
POINTS
VERSION

Public Class Methods

azimuth(s) click to toggle source
# File lib/compass_point.rb, line 42
def azimuth(s)
  input = normalize_input(s)
  if point = find_point(input)
    point[:mid]
  elsif match = input.match(COMPASS_BEARING_REGEX)
    azimuth_from_match(match)
  end
end
back_azimuth(s) click to toggle source
# File lib/compass_point.rb, line 51
def back_azimuth(s)
  azm = azimuth(s)
  return if azm.nil?

  azm > 180 ? azm - 180 : azm + 180
end
compass_quadrant_bearing(bearing) click to toggle source
# File lib/compass_point.rb, line 78
def compass_quadrant_bearing(bearing)
  b = bearing.round
  case b
  when 0, 360 then 'N'
  when 90     then 'E'
  when 180    then 'S'
  when 270    then 'W'
  else
    generate_compass_quadrant_bearing(b)
  end
end
max(s) click to toggle source
# File lib/compass_point.rb, line 63
def max(s)
  point = find_point(normalize_input(s))
  point && point[:max]
end
min(s) click to toggle source
# File lib/compass_point.rb, line 58
def min(s)
  point = find_point(normalize_input(s))
  point && point[:min]
end
min_max(s) click to toggle source
# File lib/compass_point.rb, line 68
def min_max(s)
  point = find_point(normalize_input(s))
  point && [point[:min], point[:max]]
end
name(s) click to toggle source
# File lib/compass_point.rb, line 73
def name(s)
  point = find_point(normalize_input(s))
  point && point[:name]
end

Private Class Methods

azimuth_from_match(match) click to toggle source
# File lib/compass_point.rb, line 92
def azimuth_from_match(match)
  base = if match[1] == 'n'
    match[3] == 'w' ? 360 : 0
  else
    180
  end

  adjust = match[2].to_i

  operation = if (match[1] == 'n' && match[3] == 'w') || (match[1] == 's' && match[3] == 'e')
    :-
  else
    :+
  end

  base.send(operation, adjust)
end
east_or_west(bearing) click to toggle source
# File lib/compass_point.rb, line 128
def east_or_west(bearing)
  b = bearing.round
  (180..360).cover?(b.to_i) ? 'W' : 'E'
end
find_point(s) click to toggle source
# File lib/compass_point.rb, line 133
def find_point(s)
  find_point_by_abbrev(s) || find_point_by_name(s)
end
find_point_by_abbrev(s) click to toggle source
# File lib/compass_point.rb, line 137
def find_point_by_abbrev(s)
  POINTS[s.to_sym]
end
find_point_by_name(s) click to toggle source
# File lib/compass_point.rb, line 141
def find_point_by_name(s)
  POINTS.values.find { |v| v[:name].downcase == s }
end
generate_compass_quadrant_bearing(b) click to toggle source
# File lib/compass_point.rb, line 110
def generate_compass_quadrant_bearing(b)
  s = []
  s << north_or_south(b)
  s << if north_or_south(b) == 'N'
    east_or_west(b) == 'W' ? (360 - b).abs : b
  else
    (180 - b).abs
  end.to_s
  s.last << '°'
  s << east_or_west(b)
  s.join(' ')
end
normalize_input(name) click to toggle source
# File lib/compass_point.rb, line 145
def normalize_input(name)
  name.to_s.strip.squeeze(' ').downcase
end
north_or_south(bearing) click to toggle source
# File lib/compass_point.rb, line 123
def north_or_south(bearing)
  b = bearing.round
  (0..90).cover?(b.to_i) || (270..360).cover?(b.to_i) ? 'N' : 'S'
end