Class: JetBluePlaneTracker::Airport

Inherits:
Object
  • Object
show all
Defined in:
lib/jetblue_plane_tracker/airport.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iata, icao, name, city, country, latitude, longitude, altitude, timezone, dst) ⇒ Airport

Returns a new instance of Airport.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/jetblue_plane_tracker/airport.rb', line 7

def initialize(iata, icao, name, city, country, latitude, longitude, altitude, timezone, dst)
  self.iata           =   iata
  self.icao           =   icao
  self.name           =   name
  self.city           =   city
  self.country        =   country
  self.longitude      =   longitude.to_f
  self.latitude       =   latitude.to_f
  self.altitude       =   altitude
  self.timezone       =   timezone
  self.dst            =   dst
end

Instance Attribute Details

#altitudeObject

Returns the value of attribute altitude.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def altitude
  @altitude
end

#cityObject

Returns the value of attribute city.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def city
  @city
end

#countryObject

Returns the value of attribute country.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def country
  @country
end

#dstObject

Returns the value of attribute dst.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def dst
  @dst
end

#iataObject

Returns the value of attribute iata.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def iata
  @iata
end

#icaoObject

Returns the value of attribute icao.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def icao
  @icao
end

#latitudeObject

Returns the value of attribute latitude.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def longitude
  @longitude
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def name
  @name
end

#timezoneObject

Returns the value of attribute timezone.



5
6
7
# File 'lib/jetblue_plane_tracker/airport.rb', line 5

def timezone
  @timezone
end

Class Method Details

.allObject



67
68
69
70
71
72
73
74
# File 'lib/jetblue_plane_tracker/airport.rb', line 67

def all
  data = get_json
  airports = []
  data.each do |airport_data|
    airports << new_object_instance(airport_data[1])
  end
  airports
end

.find_by_iata(iata_code) ⇒ Object



51
52
53
54
55
# File 'lib/jetblue_plane_tracker/airport.rb', line 51

def find_by_iata(iata_code)
  data = get_json
  airport_data = data[iata_code]
  new_object_instance(airport_data)
end

.get_jsonObject



76
77
78
79
# File 'lib/jetblue_plane_tracker/airport.rb', line 76

def get_json
  file = File.read(File.expand_path('../../data/airports.json', __FILE__))
  JSON.parse(file)
end

.new_object_instance(airport_data) ⇒ Object



81
82
83
84
85
86
# File 'lib/jetblue_plane_tracker/airport.rb', line 81

def new_object_instance(airport_data)
  new(airport_data["iata"], airport_data["icao"], airport_data["name"], 
      airport_data["city"], airport_data["country"], airport_data["latitude"], 
      airport_data["longitude"], airport_data["altitude"], airport_data["timezone"], 
      airport_data["dst"])
end

.parse_airport_iata(airport_name) ⇒ Object



47
48
49
# File 'lib/jetblue_plane_tracker/airport.rb', line 47

def parse_airport_iata(airport_name)
  airport_name[/[(][a-zA-Z]*[)]/, 0].gsub(/[()]/, "").strip.to_s
end

.search(search_text) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/jetblue_plane_tracker/airport.rb', line 57

def search(search_text)
  data = get_json
  airports = []
  data.each do |d|
    airport_data = d[1]
    airports << new_object_instance(airport_data) if airport_data.has_value?(search_text)
  end
  airports
end

Instance Method Details

#to_jsonObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jetblue_plane_tracker/airport.rb', line 30

def to_json
  {
    iata: self.iata,
    icao: self.icao,
    name: self.name,
    city: self.city,
    country: self.country,
    latitude: self.latitude,
    longitude: self.longitude,
    altitude: self.altitude,
    timezone: self.timezone,
    dst: self.dst
  }
end

#to_sObject



20
21
22
23
24
25
26
27
28
# File 'lib/jetblue_plane_tracker/airport.rb', line 20

def to_s
  str = ""
  self.instance_variables.each do |var| 
    name = var.to_s.gsub!("@", "")
    key = send(name)
    str += "#{name}: #{key}, "
  end
  str[0..str.length-3]
end