module NDBC::StationTable

Public Class Methods

station_table_data() click to toggle source
# File lib/ndbc/station_table.rb, line 4
def station_table_data
  response = Connection.new.get(NDBC.config[:urls][:station_table])
  rows = response.split("\n").drop(2)
  rows.map do |station|
    station_parts = cleanup_parts!(station.split('|'))

    {
      id:         station_parts[0],
      owner:      station_parts[1],
      type:       station_parts[2],
      hull:       station_parts[3],
      name:       station_parts[4],
      payload:    station_parts[5],
      location:   extract_location(station_parts[6]),
      time_zone:  station_parts[7],
      forecast:   station_parts[8],
      note:       station_parts[9],
      active:     active?(station_parts),
      tide_station_id: tide_station_id(station_parts[4])
    }
  end
end

Private Class Methods

active?(parts) click to toggle source
# File lib/ndbc/station_table.rb, line 43
def active?(parts)
  note = parts[9]
  return true unless note
  !note.match(/disestablished|discontinued|inoperative|decommissioned/)
end
cleanup_parts!(station_parts) click to toggle source
# File lib/ndbc/station_table.rb, line 34
def cleanup_parts!(station_parts)
  station_parts.map! do |part|
    part = part.gsub(' ', ' ')
    part = part.strip unless part.nil?
    part = (part == '' || part == '?') ? nil : part
    part
  end
end
extract_location(raw_location) click to toggle source
# File lib/ndbc/station_table.rb, line 49
def extract_location(raw_location)
  lat_lon = { latitude: nil, longitude: nil }
  match = raw_location.match(/(\d+\.\d+)\s(N|S)\s(\d+.\d+)\s(E|W)/)
  return lat_lon unless match
  parts = match.captures

  unsigned_lat = parts[0].to_f
  lat_hemisphere = parts[1]
  unsigned_lon = parts[2].to_f
  lon_hemisphere = parts[3]
  lat_lon[:latitude] = lat_hemisphere == 'N' ? unsigned_lat : -unsigned_lat
  lat_lon[:longitude] = lon_hemisphere == 'E' ? unsigned_lon : -unsigned_lon

  lat_lon
end
tide_station_id(name) click to toggle source
# File lib/ndbc/station_table.rb, line 29
def tide_station_id(name)
  return '' unless name
  name.match(/\d{7}/).to_s
end