Class: FCC::Station::LmsData

Inherits:
Object
  • Object
show all
Defined in:
lib/fcc/station/lms_data.rb

Constant Summary collapse

BASE_URI =
'https://enterpriseefiling.fcc.gov/dataentry/api/download/dbfile'

Instance Method Summary collapse

Instance Method Details

#common_station_dataObject



63
64
65
# File 'lib/fcc/station/lms_data.rb', line 63

def common_station_data
  @common_station_data ||= read(:common_station)
end

#facilitiesObject



59
60
61
# File 'lib/fcc/station/lms_data.rb', line 59

def facilities
  @facilities ||= read(:facility)
end

include HTTParty



14
15
16
17
18
# File 'lib/fcc/station/lms_data.rb', line 14

def find_all_related(call_sign: )
  stations = find_related_stations(call_sign: call_sign)
  translators = find_translators_for(call_sign: stations.keys)
  stations.merge(translators)
end

#find_call_signs(call_signs:) ⇒ Object



79
80
81
82
83
84
85
# 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: []) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# 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


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fcc/station/lms_data.rb', line 40

def find_related_stations(call_sign: )
  call_signs = [call_sign].flatten

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

  correlated_app_ids = records.map { |m| m['eeo_application_id'] }
  matches = common_station_data.entries.select do |entry|
    correlated_app_ids.include?(entry['eeo_application_id'])
  end

  {}.tap do |hash|
    matches.each do |record|
      hash[record['callsign']] = record['facility_id']
    end
  end
end

#find_translators_for(call_sign:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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