Class: Flyday

Inherits:
Object
  • Object
show all
Defined in:
lib/flyday.rb,
lib/flight.rb

Overview

The Flyday class contains the #search method for finding southwest flights between two airports on a date. You can only search for one way tickets.

Defined Under Namespace

Classes: Flight

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFlyday

Returns a new instance of Flyday.



10
11
12
13
14
15
# File 'lib/flyday.rb', line 10

def initialize
  @airline = 'southwest'
  @mechanize = Mechanize.new
  @mechanize.log = Logger.new 'flyday.log'
  @mechanize.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

Instance Attribute Details

#airlineObject (readonly)

Returns the value of attribute airline.



8
9
10
# File 'lib/flyday.rb', line 8

def airline
  @airline
end

Instance Method Details

#headersObject



29
30
31
32
33
34
35
# File 'lib/flyday.rb', line 29

def headers
  {
    'X-API-Key' => 'l7xx8d8bfce4ee874269bedc02832674129b',
    'Content-Type' => 'application/vnd.swacorp.com.accounts.login-v1.0+json',
    'User-Agent' => 'Southwest/3.1.100 (iPad; iOS 8.3; Scale/2.00)'
  }
end

#params(orig, dest, date) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flyday.rb', line 17

def params(orig, dest, date)
  {
    'currency-type' => 'Dollars',
    'number-adult-passengers' => '1',
    'number-senior-passengers' => '0',
    'promo-code' => '',
    'origination-airport' => orig,
    'destination-airport' => dest,
    'departure-date' => date.strftime('%Y-%m-%d')
  }
end

#search(orig: 'MDW', dest: 'ATL', date: Date.today) ⇒ Object



37
38
39
40
41
42
# File 'lib/flyday.rb', line 37

def search(orig:'MDW', dest:'ATL', date:Date.today)
  url = 'https://api-extensions.southwest.com/v1/mobile/flights/products'
  resp = @mechanize.get(url, params(orig, dest, date), nil, headers)
  fail 'Request Failed' if resp.code != '200'
  JSON.parse(resp.body)['trips'][0]['airProducts'].map { |l| Flight.new(l) }
end