class Flight

Attributes

airline_iata[R]
arriving_airport_city[R]
arriving_airport_iata[R]
arriving_country_iata[R]
departure_airport_city[R]
departure_airport_iata[R]
departure_country_iata[R]
departure_date[R]
flight_number[R]
multiple_segments[R]
raw_airline_iata[R]
route[R]
route_array[R]
segment_1[R]
segment_2[R]
selected_segment[R]
status[R]
title[R]
url[R]

Public Class Methods

new(flight_number, departure_date) click to toggle source

Initialize

# File lib/flightcheck.rb, line 11
def initialize(flight_number, departure_date)
  # Flight number
  # e.g. "BA2589"
  @flight_number = flight_number

  # Departure date
  # e.g. "2014-05-29"
  @departure_date = departure_date

  # FlightStats URL
  @url = "http://www.flightstats.com/go/FlightStatus/flightStatusByFlight.do?airline=&flightNumber=#{flight_number}&departureDate=#{departure_date}"
end

Public Instance Methods

check() click to toggle source

Use Mechanize to scrape all flight information

# File lib/flightcheck.rb, line 25
def check
  # Instantiate a new Mechanize object
  a = Mechanize.new

  # Visit the flightstats URL to scrape information
  a.get(@url) do |page|
    # Check if flight has multiple segments
    # Returns a boolean value
    @multiple_segments = page.search("//*[@id='mainAreaLeftColumn']/div/div/text()").text.strip === "This flight has multiple segments. Please select a segment from the list." ? true : false

    # If there are multiple segments, ask the user to select the correct one
    if @multiple_segments == true

      # Extract segment information into the correct variables
      @segment_1 = page.search("//*[@id='mainAreaLeftColumn']/div/div/p[1]/a").text.strip
      @segment_2 = page.search("//*[@id='mainAreaLeftColumn']/div/div/p[2]/a").text.strip

      # Ask the user to select the correct flight segment
      puts   "This flight has two flight segments,"
      puts   "please select the correct one"
      puts   "========================================"
      puts   "Segment 1: #{@segment_1}"
      puts   "Segment 2: #{@segment_2}"
      puts   "----------------------------------------"
      print  "1 or 2? Make your choice: "
      @selected_segment = gets.chomp

      # Based on user input, get the flight information
      if @selected_segment == "1"
        @segment_1 = page.links[0].click

        # Page title contains the basic information for the flight
        # e.g. "(BA) British Airways 2589 Flight Status"
        @title = @segment_1.search("head > title").text.strip

        # Flight status
        # e.g. "Landed - Delayed 103 minutes"
        @status = @segment_1.search("#mainAreaLeftColumn > div.uiComponent674.flightStatusByFlightBlock > div > div.statusBlock > div.statusType").text.strip

        # Flight route
        # e.g. "(VCE) Venice, IT to (LGW) London, EN, GB"
        @route = @segment_1.search("#mainAreaLeftColumn > div.uiComponent674.flightStatusByFlightBlock > div > div.route").text.strip

      # Based on user input, get the flight information
      elsif @selected_segment == "2"
        @segment_2 = page.links[1].click

        # Page title contains the basic information for the flight
        # e.g. "(BA) British Airways 2589 Flight Status"
        @title = @segment_2.search("head > title").text.strip

        # Flight status
        # e.g. "Landed - Delayed 103 minutes"
        @status = @segment_2.search("#mainAreaLeftColumn > div.uiComponent674.flightStatusByFlightBlock > div > div.statusBlock > div.statusType").text.strip

        # Flight route
        # e.g. "(VCE) Venice, IT to (LGW) London, EN, GB"
        @route = @segment_2.search("#mainAreaLeftColumn > div.uiComponent674.flightStatusByFlightBlock > div > div.route").text.strip

      else
        puts "The input was unknown. Please use '1' for segment 1 and '2' for segment 2"
      end

    # When the flight has a single segment, fetch the information
    else
      # Page title contains the basic information for the flight
      # e.g. "(BA) British Airways 2589 Flight Status"
      @title = page.search("head > title").text.strip

      # Flight status
      # e.g. "Landed - Delayed 103 minutes"
      @status = page.search("#mainAreaLeftColumn > div.uiComponent674.flightStatusByFlightBlock > div > div.statusBlock > div.statusType").text.strip

      # Flight route
      # e.g. "(VCE) Venice, IT to (LGW) London, EN, GB"
      @route = page.search("#mainAreaLeftColumn > div.uiComponent674.flightStatusByFlightBlock > div > div.route").text.strip
    end
  end

  ## Airline IATA
  # Extract raw airline iata and convert to string to avoid
  # TypeError: no implicit conversion of MatchData intro String
  @raw_airline_iata = /\(\w+\)/.match(@title).to_s

  # Extract the airline IATA without the parentheses
  @airline_iata = /\w+/.match(@raw_airline_iata)

  ## Flight route
  # Cleanup the scraped route information

  # Delete all "\t" from the string
  @route.delete!("\t")

  # Substitue all "\n\n" with " "
  @route.gsub!("\n\n", " ")

  # Substitue all "\n" with " "
  @route.gsub!("\n", " ")

  # Substitue all "  " with " "
  @route.gsub!("  ", " ")

  ## Departure airport
  # Extract departure airport information from @route

  # Split route string into an array
  @route_array = @route.split

  # Put information inside the corresponding variables
  @departure_airport_iata = @route_array[0].delete!("(   )")
  @departure_airport_city = @route_array[1].delete!(",")
  @departure_country_iata = @route_array[2]

  @arriving_airport_iata  = @route_array[4].delete!("(   )")
  @arriving_airport_city  = @route_array[5].delete!(",")
  @arriving_country_iata  = @route_array[6]

  ## Information
  # Print all relevant information to te console
  puts @title
  puts @status
  puts @route
end