Module: A0::TZMigration::Util

Defined in:
lib/a0/tzmigration/util.rb

Class Method Summary collapse

Class Method Details

.compact_ranges!(range_list) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/a0/tzmigration/util.rb', line 8

def self.compact_ranges!(range_list) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  index = 0

  while index < range_list.count
    curr_range = range_list[index]
    next_range = range_list[index + 1]

    if next_range && curr_range[:fin] == next_range[:ini] && curr_range[:off] == next_range[:off]
      curr_range[:fin] = next_range[:fin]
      curr_range[:fin_str] = next_range[:fin_str]
      range_list.delete_at(index + 1)
    else
      index += 1
    end
  end

  range_list
end

.load_from_file(path) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/a0/tzmigration/util.rb', line 77

def self.load_from_file(path)
  conf = A0::TZMigration.config.data_dir
  file = File.join(conf, path)

  raise "File #{path} not found at #{conf}" unless File.exist? file

  JSON.parse File.read(file)
end

.load_from_network(path) ⇒ Object



71
72
73
74
75
# File 'lib/a0/tzmigration/util.rb', line 71

def self.load_from_network(path)
  url = "#{A0::TZMigration.config.base_url}/#{path}"

  JSON.parse RestClient.get(url)
end

.load_from_network_or_file(path) ⇒ Object



86
87
88
89
90
91
# File 'lib/a0/tzmigration/util.rb', line 86

def self.load_from_network_or_file(path)
  load_from_network(path)
rescue StandardError => error
  warn "Unable to fetch from network, using local files (error was: #{error})"
  load_from_file(path)
end

.next_index(index, range_list, timestamp) ⇒ Object



48
49
50
51
52
# File 'lib/a0/tzmigration/util.rb', line 48

def self.next_index(index, range_list, timestamp)
  index += 1 while range_list[index + 1] && range_list[index][:ini] < timestamp && range_list[index][:fin] <= timestamp

  index
end

.offset_to_str(offset) ⇒ Object



27
28
29
30
31
32
# File 'lib/a0/tzmigration/util.rb', line 27

def self.offset_to_str(offset)
  str = Time.at(offset.abs).utc.strftime('%H:%M:%S')
  sig = offset.negative? ? '-' : '+'

  "#{sig}#{str}"
end

.range_item(ini, fin, off) ⇒ Object



44
45
46
# File 'lib/a0/tzmigration/util.rb', line 44

def self.range_item(ini, fin, off)
  { ini_str: timestamp_to_str(ini), fin_str: timestamp_to_str(fin), off_str: offset_to_str(off), ini: ini, fin: fin, off: off }
end

.split_ranges(range_list, timestamps) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/a0/tzmigration/util.rb', line 54

def self.split_ranges(range_list, timestamps) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  range_list = Marshal.load(Marshal.dump(range_list))
  index = 0

  timestamps.each do |timestamp|
    index = next_index(index, range_list, timestamp)
    range = range_list[index]

    if index < range_list.count && timestamp > range[:ini] && timestamp < range[:fin]
      range_list.insert index + 1, range.merge(ini: timestamp)
      range_list[index][:fin] = timestamp
    end
  end

  range_list
end

.timestamp_to_str(timestamp) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/a0/tzmigration/util.rb', line 34

def self.timestamp_to_str(timestamp)
  if timestamp == -Float::INFINITY
    '-∞'
  elsif timestamp == +Float::INFINITY
    ''
  else
    Time.at(timestamp).utc.to_s
  end
end