Class: PLA::Movements

Inherits:
Object
  • Object
show all
Defined in:
lib/pla/movements.rb

Direct Known Subclasses

Arrivals, Departures

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMovements

Returns a new instance of Movements.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pla/movements.rb', line 18

def initialize
  @url = self.class.url
  @root = 'div.newsMainArea table tr td table tr'  # It gets worse

  @records = []
  @ships = get_ships_data

  set_headers
  set_records

  @records
end

Class Attribute Details

.urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/pla/movements.rb', line 10

def url
  @url
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



16
17
18
# File 'lib/pla/movements.rb', line 16

def headers
  @headers
end

#recordsObject (readonly)

Returns the value of attribute records.



16
17
18
# File 'lib/pla/movements.rb', line 16

def records
  @records
end

Class Method Details

.set_url(u) ⇒ Object



11
12
13
# File 'lib/pla/movements.rb', line 11

def set_url u
  @url = u
end

Instance Method Details

#get_ships_dataObject



31
32
33
# File 'lib/pla/movements.rb', line 31

def get_ships_data
  Nokogiri::HTML(open(@url)).css(@root)
end

#set_headersObject



35
36
37
38
# File 'lib/pla/movements.rb', line 35

def set_headers
  @headers = normalize_and_map(@ships.first.css('th'))
  @headers[-1] = 'notes'  # pla returns this as empty. We infer it means notes, though usually it says 'Pilot required'
end

#set_recordsObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pla/movements.rb', line 40

def set_records
  @ships[1..-1].each do |t|
    record = {}
    fields = normalize_and_map(t.css('td'))
    fields.each_with_index do |f,i|
      record[ @headers[i].to_sym ] = f
    end

    movement = PLA::Movement.new(record)
    @records << movement.to_h
  end
end