Class: FCC::Station::Parsers::ExtendedInfo

Inherits:
HTTParty::Parser
  • Object
show all
Defined in:
lib/fcc/station/parsers/extended_info.rb

Instance Method Summary collapse

Instance Method Details

#parseObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fcc/station/parsers/extended_info.rb', line 7

def parse
  results = []
  body.each_line do |row|
    attrs = {}
    attrs[:raw] = row
    fields = row.split('|').slice(1...-1).collect(&:strip).map { |v| v == '-' ? "" : v }

    attrs[:call_sign]                = fields[0]
    attrs[:frequency]                = parse_frequency(fields[1])
    attrs[:band]                     = fields[2]
    attrs[:channel]                  = fields[3]
    attrs[:antenna_type]             = fields[4] # Directional Antenna (DA) or NonDirectional (ND)
    attrs[:operating_hours]          = fields[5] if fields[5] && attrs[:band]&.upcase == "AM" # (Only used for AM)
    attrs[:station_class]            = fields[6]
    attrs[:region_2_station_class]   = fields[7] if fields[7] && attrs[:band]&.upcase == "AM" # (only used for AM)
    attrs[:status]                   = fields[8]
    attrs[:city]                     = fields[9]
    attrs[:state]                    = fields[10]
    attrs[:country]                  = fields[11]
    attrs[:file_number]              = fields[12]  #File Number (Application, Construction Permit or License) or
    attrs[:signal_strength]          = parse_signal_strength(fields[13]) # Effective Radiated Power --
    attrs[:effective_radiated_power] = parse_signal_strength(fields[14]) # Effective Radiated Power -- vertically polarized (maximum)
    attrs[:haat_horizontal]          = fields[15] # Antenna Height Above Average Terrain (HAAT) -- horizontal polarization
    attrs[:haat_vertical]            = fields[16] # Antenna Height Above Average Terrain (HAAT) -- vertical polarization
    attrs[:fcc_id]                   = fields[17] # Facility ID Number (unique to each station)
    attrs[:latitude]                 = parse_latitude(fields[18], fields[19], fields[20], fields[21])
    attrs[:longitude]                = parse_longitude(fields[22], fields[23], fields[24], fields[25])
    attrs[:licensed_to]              = fields[26] # Licensee or Permittee

    results << attrs
  end

  results
end

#parse_frequency(freq) ⇒ Object



72
73
74
# File 'lib/fcc/station/parsers/extended_info.rb', line 72

def parse_frequency(freq)
  freq.to_f
end

#parse_latitude(direction, degrees, minutes, seconds) ⇒ Object



48
49
50
51
52
# File 'lib/fcc/station/parsers/extended_info.rb', line 48

def parse_latitude(direction, degrees, minutes, seconds)
  decimal_degrees = degrees.to_i + (minutes.to_f / 60) + (seconds.to_f / 3600)

  "#{direction =~ /S/ ? '-' : ''}#{decimal_degrees}"
end

#parse_longitude(direction, degrees, minutes, seconds) ⇒ Object



42
43
44
45
46
# File 'lib/fcc/station/parsers/extended_info.rb', line 42

def parse_longitude(direction, degrees, minutes, seconds)
  decimal_degrees = degrees.to_i + (minutes.to_f / 60) + (seconds.to_f / 3600)

  "#{direction =~ /W/ ? '-' : ''}#{decimal_degrees}"
end

#parse_signal_strength(power_string) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fcc/station/parsers/extended_info.rb', line 54

def parse_signal_strength(power_string)
  return unless power_string.present?

  number, unit = power_string.strip.scan(/^([0-9.]+)\s+(\w+)$?/).flatten
  multiplier   = case unit&.downcase
                  when "w"
                    1
                  when "kw"
                    1000
                  when "mw"
                    1000000
                  else
                    1
                 end
      
  number.to_f * multiplier
end