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.



7
8
9
10
# File 'lib/pla/movement.rb', line 7

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



62
63
64
65
66
67
68
69
# File 'lib/pla/movement.rb', line 62

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_country!Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/pla/movement.rb', line 47

def normalise_country!
  country_code = @record.delete(:nationality)
  begin
    country = IsoCountryCodes.find(country_code).name
  rescue IsoCountryCodes::UnknownCodeError
    country = country_code
  end

  @record[:country] = country
end

#normalise_fields!Object



58
59
60
# File 'lib/pla/movement.rb', line 58

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

#normalise_ship!Object



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

def normalise_ship!
  name = @record.delete(:'vessel name')
  @record['vessel'] = PLA::Ship.new(name).to_h
end

#normalise_timestamp!Object



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

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.to_s
end

#to_hObject



12
13
14
15
16
17
18
19
# File 'lib/pla/movement.rb', line 12

def to_h
  normalise_timestamp!
  normalise_ship!
  normalise_country!
  normalise_fields!

  @record
end