Last active
          November 3, 2019 22:39 
        
      - 
      
- 
        Save movstox/05b84aabef5383271ac0b058aa1aa7cb to your computer and use it in GitHub Desktop. 
    lookup airport by code via wikipedia
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | require 'rubygems' | |
| require 'nokogiri' | |
| require 'open-uri' | |
| def find_airport_info(lookup_iata_code) | |
| page = Nokogiri::HTML(open("https://en.wikipedia.org/wiki/List_of_airports_by_IATA_code:_#{lookup_iata_code[0]}")) | |
| page.yield_self { |page| page.search('.wikitable tr:contains("%s")' % lookup_iata_code)[0] }.yield_self do |row| | |
| airport_name_row = row.children[5] | |
| airport_info = { | |
| name: airport_name_row&.text, | |
| wiki_page_url: (airport_name_row ? 'https://en.wikipedia.org%s' % airport_name_row.children[0]['href'] : nil), | |
| iata_code: row.children[1]&.text, | |
| location: row.children[7].text | |
| } | |
| airport_info | |
| end | |
| end | |
| p find_airport_info 'CDG' | |
| # {:name=>"Charles de Gaulle Airport (Roissy Airport)", :wiki_page_url=>"https://en.wikipedia.org//wiki/Charles_de_Gaulle_Airport", :iata_code=>"CDG", :location=>"Paris, Île-de-France, France"} | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment