class FCC::Station::LmsData

Constants

BASE_URI

Public Instance Methods

common_station_data() click to toggle source
# File lib/fcc/station/lms_data.rb, line 63
def common_station_data
  @common_station_data ||= read(:common_station)
end
facilities() click to toggle source
# File lib/fcc/station/lms_data.rb, line 59
def facilities
  @facilities ||= read(:facility)
end
find_call_signs(call_signs:) click to toggle source
# File lib/fcc/station/lms_data.rb, line 79
def find_call_signs(call_signs:)
  common_station_data.entries.select do |entry|
    call_signs.any? do |call_sign|
      call_signs_match?(call_sign, entry['callsign'])
    end
  end
end
find_facilities(facility_ids:, call_signs: []) click to toggle source
# File lib/fcc/station/lms_data.rb, line 67
def find_facilities(facility_ids:, call_signs: [])
  matched_facilities = facilities.entries.select do |facility|
    facility_ids.include?(facility['primary_station']) || facility_ids.include?(facility['facility_id']) || call_signs.include?(facility['callsign'])
  end

  {}.tap do |hash|
    matched_facilities.each do |record|
      hash[record['callsign']] = record['facility_id']
    end
  end
end
find_translators_for(call_sign: ) click to toggle source
# File lib/fcc/station/lms_data.rb, line 20
def find_translators_for(call_sign: )
  call_signs = [call_sign].flatten

  records = common_station_data.entries.select do |entry|
    call_signs.any? { |call_sign| call_signs_match?(call_sign, entry['callsign']) }
  end 

  facility_ids = records.map { |r| r['facility_id'] }.uniq.compact

  matched_facilities = facilities.entries.select do |facility|
    facility_ids.include?(facility['primary_station']) #{}|| facility_ids.include?(facility['facility_id'])
  end

  {}.tap do |hash|
    matched_facilities.each do |record|
      hash[record['callsign']] = record['facility_id']
    end
  end
end
read(file_name) click to toggle source
# File lib/fcc/station/lms_data.rb, line 87
def read(file_name)
  key = "#{lms_date}-#{file_name}"
  remote_url = URI("#{BASE_URI}/#{lms_date}/#{file_name}.zip")
  FCC.log remote_url
  contents = FCC.cache.fetch key do
    begin
      temp_file = http_download_uri(remote_url)
      break if temp_file.empty?

      contents = ""
      Zip::File.open_buffer(temp_file) do |zf| 
        contents = zf.read(zf.entries.first)
        break
      end

      value = contents
    rescue Exception => e
      FCC.error(e.message)
      value = nil
    ensure
      value
    end
  end

  if contents
    CSV.parse(contents, col_sep: '|', headers: true)
  end
end

Protected Instance Methods

call_signs_match?(ours, theirs) click to toggle source
# File lib/fcc/station/lms_data.rb, line 118
def call_signs_match?(ours, theirs)
  theirs.to_s.upcase.to_s == ours.to_s.upcase.to_s || theirs.to_s.upcase =~ Regexp.new("^#{ours.to_s.upcase}[-—–][A-Z0-9]+$")
end
http_download_uri(uri) click to toggle source
# File lib/fcc/station/lms_data.rb, line 122
def http_download_uri(uri)
  FCC.log 'Downloading ' + uri.to_s
  begin
    Tempfile.create { HTTParty.get(uri)&.body }
  rescue Exception => e
    FCC.error "=> Exception: '#{e}'. Skipping download."

    raise e
    return false
  end
end
lms_date() click to toggle source
# File lib/fcc/station/lms_data.rb, line 134
def lms_date
  Date.today.strftime('%m-%d-%Y')
end