Class: ATCTools::Airport

Inherits:
Object
  • Object
show all
Defined in:
lib/atc-tools/airport.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code = nil, **kvargs) ⇒ Airport

Params: :code, :name, :variance



22
23
24
25
26
# File 'lib/atc-tools/airport.rb', line 22

def initialize(code = nil, **kvargs)
  @code     = code || (kvargs.fetch :code, '')
  @name     = kvargs.fetch :name, ''
  @variance = kvargs.fetch :variance, 17.0 # TODO: Detect this intelligently.
end

Instance Attribute Details

#codeObject

Airport’s ICAO code.



9
10
11
# File 'lib/atc-tools/airport.rb', line 9

def code
  @code
end

#heading_uriObject (readonly)

URI of the web page used for the last heading lookup.



18
19
20
# File 'lib/atc-tools/airport.rb', line 18

def heading_uri
  @heading_uri
end

#nameObject

Airport’s full name.



29
30
31
32
# File 'lib/atc-tools/airport.rb', line 29

def name
  discover_name if @name.empty?
  @name
end

#name_uriObject (readonly)

URI of the web page used for the last airport name lookup.



16
17
18
# File 'lib/atc-tools/airport.rb', line 16

def name_uri
  @name_uri
end

#varianceObject

Airport’s magnetic variance from true north.



13
14
15
# File 'lib/atc-tools/airport.rb', line 13

def variance
  @variance
end

Instance Method Details

#discover_nameObject

Discover the airport’s full name based on ICAO code.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/atc-tools/airport.rb', line 36

def discover_name
  @name_uri = "http://www.airnav.com/airport/#{@code.to_s.downcase}"
  response = Net::HTTP.get URI name_uri
  
  l = response.scan %r{(?i:<title>)(?:AirNav:\s*\w*\s*-\s*)?(.*)(?i:</title>)}
  
  raise ATCTools::NameDiscoveryError, "Could not discover name for #{@code.to_s.upcase}" \
    unless l.count > 0
    
  @name = l.flatten.first
end

#discover_varianceObject

Discover the airport’s magnetic variance based on ICAO code.



50
51
# File 'lib/atc-tools/airport.rb', line 50

def discover_variance
end

#magnetic_heading_from(departure) ⇒ Object

Calculate the magnetic heading from the specified airport. Takes an ICAO code or Airport object.



80
81
82
# File 'lib/atc-tools/airport.rb', line 80

def magnetic_heading_from(departure)
  true_to_magnetic true_heading_from(departure)
end

#magnetic_heading_to(arrival) ⇒ Object

Calculate the magnetic heading to the specified airport. Takes an ICAO code or Airport object.



74
75
76
# File 'lib/atc-tools/airport.rb', line 74

def magnetic_heading_to(arrival)
  true_to_magnetic true_heading_to(arrival)
end

#magnetic_to_true(heading) ⇒ Object

Convert a magnetic heading to true based on the airport’s magnetic variance.



92
93
94
# File 'lib/atc-tools/airport.rb', line 92

def magnetic_to_true(heading)
  heading + @variance
end

#to_sObject

Print the Airport ICAO code. Allows the object to act as a direct replacement for string input.



98
99
100
# File 'lib/atc-tools/airport.rb', line 98

def to_s
  @code.to_s.upcase
end

#true_heading_from(departure) ⇒ Object

Calculate the true heading from the specified airport. Takes an ICAO code or Airport object.



68
69
70
# File 'lib/atc-tools/airport.rb', line 68

def true_heading_from(departure)
  360.0 - true_heading_to(departure)
end

#true_heading_to(arrival) ⇒ Object

Calculate the true heading to the specified airport. Takes an ICAO code or Airport object.



55
56
57
58
59
60
61
62
63
64
# File 'lib/atc-tools/airport.rb', line 55

def true_heading_to(arrival)
  @heading_uri = "http://www6.landings.com/cgi-bin/nph-dist_apt?airport1=#{@code.downcase}&airport2=#{arrival.to_s.strip.downcase}"
  response = Net::HTTP.get URI heading_uri
  r = response.scan /(?:heading:)\s*([\d\.]+)\s+/
  
  raise ATCTools::HeadingDiscoveryError, "Heading from #{@code.upcase} to #{arrival.to_s.upcase} not found." \
    unless r.count > 0
  
  true_hdg = r.flatten.first.to_f
end

#true_to_magnetic(heading) ⇒ Object

Convert a true heading to magnetic based on the airport’s magnetic variance.



86
87
88
# File 'lib/atc-tools/airport.rb', line 86

def true_to_magnetic(heading)
  heading - @variance
end