Module: MailParser

Defined in:
lib/mailParser.rb

Class Method Summary collapse

Class Method Details

.receive_mail(message) ⇒ Object



2
3
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
75
76
# File 'lib/mailParser.rb', line 2

def self.receive_mail(message)
    
    puts "++++++++++++++++++++++++++++++++++"
    puts message.body.inspect
    puts "++++++++++++++++++++++++++++++++++"
    
    @email = message
    
    #take from DB
    airplane = ["plane", "airplane", "flight"]
    train = ["train", "rail"]
    bus = ["bus"]
    
    airlines = ["aer lingus", "asl airlines ireland", "cityjet", "norwegian air international", "ryanair", "stobard air"]
    railcarriers = ["dart", "northern commuter", "south eastern commuter", "south western commuter", "western commuter"]
    buscarriers = ["bus eireann", "matthews"]

    trip = Trip.new
    
    airplane.each do |word|
        if @email.body.include?(word)
            trip.category = "plane"
        end
    end
    train.each do |word|
        if @email.body.include?(word)
            trip.category = "train"
        end  
    end
    bus.each do |word|
        if @email.body.include?(word)
            trip.category = "bus"
        end
    end
 
    if trip.category == "plane"
        airlines.each do |word|
            if @email.body.include?(word)
                trip.carrier = word
            end
        end

    elsif trip.category == "train"
        railcarriers.each do |word|
            if @email.body.include?(word)
                trip.carrier = word
            end
        end
        
    elsif trip.category == "bus"
        buscarriers.each do |word|
            if @email.body.include?(word)
                trip.carrier = word
            end
        end
    end
    
    trip.confirmationNumber = @email.body.match(/[a-zA-Z]{3}\d{4}/)[0]
    string = @email.body.to_s
    
    times = string.scan(/(\d:\d\d[a|p]|\d\d:\d\d[a|p])/)
    trip.startTime = times[0][0]
    trip.endTime = times[1][0]

    locations = string.scan(/[A-Z]{3}\b/)
    trip.startLocation = locations[0]
    trip.endLocation = locations[1]
    
    dates = string.scan(/\d{2}\/\d{2}\/\d{2}/)
    trip.startDate = dates[0]
    trip.endDate = dates[1]
    
    return trip

end