Class: Firefly

Inherits:
Flight show all
Defined in:
lib/teecket/firefly.rb

Instance Attribute Summary

Attributes inherited from Flight

#date, #fares, #from, #to

Instance Method Summary collapse

Methods inherited from Flight

#initialize

Constructor Details

This class inherits a constructor from Flight

Instance Method Details

#getObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/teecket/firefly.rb', line 4

def get

  uri = URI('https://m.fireflyz.com.my/')

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  req = Net::HTTP::Get.new(uri.path)

  res = http.request(req)

  cookie = res['Set-Cookie']

  uri = URI('https://m.fireflyz.com.my/Search')

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  req = Net::HTTP::Get.new(uri.path, initheader = { 'Cookie' => cookie})

  cookie = res['Set-Cookie']

  uri = URI('https://m.fireflyz.com.my/Search')

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  req = Net::HTTP::Post.new(uri.path, initheader = { 'Cookie' => cookie})
  req.body = "type=2&departure_station=#{from}&arrival_station=#{to}&departure_date=#{date}&return_date=30%2F06%2F2015&adult=1&infant=0"

  res = http.request(req)

  if res['location']
    uri = URI(res['location'])

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    req = Net::HTTP::Get.new(uri.path, initheader = { 'Cookie' => cookie})

    res = http.request(req)

    doc = Nokogiri::HTML(res.body)

    doc.css('div.market1').each_with_index do |elem, i|
      depart_at     = doc.css('div.market1')[i].css('div.visible-xs').css('table')[1].css('td')[0].text.strip
      arrive_at     = doc.css('div.market1')[i].css('div.visible-xs').css('table')[1].css('td')[1].text.strip
      fare          = doc.css('div.market1')[i].css('div.visible-xs > div').text.strip
      flight_number = doc.css('div.market1')[i].css('div.visible-xs').css('table')[0].text.strip
      origin        = doc.css('div.market1')[i]['onclick'].scan(/~[A-Z]{3}~/)[0].gsub('~', '')
      destination   = doc.css('div.market1')[i]['onclick'].scan(/~[A-Z]{3}~/)[1].gsub('~', '')

      depart_at     = DateTime.parse("#{date} #{depart_at.gsub(/\t/, '').match(/^(.*?)(AM|PM)/).to_s}").strftime('%I:%M %p')
      arrive_at     = DateTime.parse("#{date} #{arrive_at.gsub(/\t/, '').match(/^(.*?)(AM|PM)/).to_s}").strftime('%I:%M %p')
      fare          = fare.gsub(/ MYR/, '')
      flight_number = flight_number.gsub(/ /, '').gsub(/FLIGHTNO\./, '')

      fares << { flight_name: 'Firefly',
                 flight_number: flight_number,
                 origin: origin,
                 destination: destination,
                 depart_at: depart_at,
                 arrive_at: arrive_at,
                 fare: fare }
    end
  end
end