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



56
57
58
59
60
61
62
63
# File 'lib/pla/movement.rb', line 56

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



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

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

#normalise_notes!Object



52
53
54
# File 'lib/pla/movement.rb', line 52

def normalise_notes!
  @record[:notes].empty? ? @records[:notes] = nil : nil
end

#normalise_timestamp!Object



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

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



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

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
17
# File 'lib/pla/movement.rb', line 10

def to_h
  normalise_timestamp!
  normalise_vessel!
  normalise_notes!
  normalise_fields!

  @record
end