Class: PLA::Movement

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

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Movement

Returns a new instance of Movement.



5
6
7
8
# File 'lib/pla/movement.rb', line 5

def initialize record
  # Take a movement record from records and do stuff with it
  @record = record
end

Instance Method Details

#gen_date(year, month, day, hour, minute) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/pla/movement.rb', line 51

def gen_date year, month, day, hour, minute
  # Because the pla doesn't provide a year we run the risk of providing
  # a date in the past if the data wraps around the end of the year.
  #
  # The pla doesn't, luckily, have data years in advance or this'd be proper
  # fucked
  DateTime.new(year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i, 0)
end

#normalise_fields!Object



47
48
49
# File 'lib/pla/movement.rb', line 47

def normalise_fields!
  @record.delete(:at)
end

#normalise_timestamp!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pla/movement.rb', line 18

def normalise_timestamp!
  # The record format from pla has a 'date' field as dd/mm and a time as 'hhmm'
  # We'd prefer a combined timestamp in a standard format

  date = @record.delete :date
  time = @record.delete :time
  now = DateTime.now

  year = now.year
  day,month = date.split('/')
  hour = time[0..1]
  minute = time[2..3]

  date = gen_date year, month, day, hour, minute
  if now > date
    date = gen_date year+1, month, day, hour, minute
  end

  @record[:timestamp] = date
end

#normalise_vessel!Object



39
40
41
42
43
44
45
# File 'lib/pla/movement.rb', line 39

def normalise_vessel!
  name = @record.delete(:'vessel name')
  country = @record.delete(:nationality)
  agent = @record.delete(:agent)

  @record[:vessel] = PLA::Vessel.new(name, country, agent).to_h
end

#to_hObject



10
11
12
13
14
15
16
# File 'lib/pla/movement.rb', line 10

def to_h
  normalise_timestamp!
  normalise_vessel!
  normalise_fields!

  @record
end