Class: Autoluv::SouthwestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/autoluv/southwestclient.rb

Constant Summary collapse

DEFAULT_HEADERS =

minimum required headers for all API calls

{
  "Content-Type": "application/json",
  "X-API-Key": "l7xx0a43088fe6254712b10787646d1b298e",
  "X-Channel-ID": "MWEB", # required now for viewing a reservation
}
CHECK_IN_URL =
"https://mobile.southwest.com/api/mobile-air-operations/v1/mobile-air-operations/page/check-in"
RESERVATION_URL =
"https://mobile.southwest.com/api/mobile-air-booking/v1/mobile-air-booking/page/view-reservation"
TIME_ZONES_PATH =
File.expand_path("../../data/airport_time_zones.json", __dir__)

Class Method Summary collapse

Class Method Details

.check_in(confirmation_number, first_name, last_name, to = nil, bcc = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/autoluv/southwestclient.rb', line 36

def self.check_in(confirmation_number, first_name, last_name, to = nil, bcc = nil)
  check_in = attempt = nil

  # try checking in multiple times in case the our server time is out of sync with Southwest's.
  num_attempts = 10

  start_time = Time.now

  num_attempts.times do |x|
    begin
      attempt = x + 1
      post_data = self.check_in_post_data(confirmation_number, first_name, last_name)
      check_in = RestClient.post("#{CHECK_IN_URL}", post_data.to_json, self.headers)
      break
    rescue RestClient::ExceptionWithResponse => ewr
      sleep(1)
      next unless x == num_attempts - 1

      raise
    end
  end

  end_time = Time.now
  boarding_positions = ""

  check_in_json = JSON.parse(check_in)
  flights = check_in_json["checkInConfirmationPage"]["flights"]

  # make the output more user friendly
  flights.each_with_index do |flight, x|
    boarding_positions << flight["originAirportCode"] << "\n"

    flight["passengers"].each do |passenger|
      boarding_positions << "- #{passenger["name"]} (#{passenger["boardingGroup"]}#{passenger["boardingPosition"]})" << "\n"
    end

    boarding_positions << "\n" unless x == flights.size - 1
  end

   = {
    end_time: end_time.strftime("%I:%M.%L"),
    elapsed_time: (end_time - start_time).round(2),
    attempts: attempt,
  }

  Autoluv::notify_user(true, confirmation_number, first_name, last_name, { to: to, bcc: bcc, boarding_positions: boarding_positions, metadata:  })
end

.schedule(confirmation_number, first_name, last_name, to = nil, bcc = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/autoluv/southwestclient.rb', line 21

def self.schedule(confirmation_number, first_name, last_name, to = nil, bcc = nil)
  flights = self.departing_flights(confirmation_number, first_name, last_name)

  flights.each_with_index do |flight, x|
    check_in_time = self.check_in_time(flight)

    puts "Scheduling flight departing #{flight[:airport_code]} at #{flight[:departure_time]} on #{flight[:departure_date]}."

    command = "echo 'autoluv checkin #{confirmation_number} #{Shellwords.shellescape(first_name)} #{Shellwords.shellescape(last_name)} #{to} #{bcc}' | at #{check_in_time.strftime('%I:%M %p %m/%d/%y')}"
    `#{command}`

    puts unless x == flights.size - 1
  end
end