util.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require "date"
  2. module Util
  3. def self.parse_epoch_ms(v)
  4. return nil if v.nil? || v.to_s.strip.empty?
  5. begin
  6. Time.at(v.to_i / 1000).to_date
  7. rescue ArgumentError, RangeError, TypeError
  8. nil
  9. end
  10. end
  11. def self.parse_aus_date(text)
  12. s = text.to_s.strip
  13. return nil if s.empty?
  14. ["%d/%m/%Y", "%d/%m/%y"].each do |fmt|
  15. begin
  16. return Date.strptime(s, fmt)
  17. rescue ArgumentError, Date::Error
  18. next
  19. end
  20. end
  21. Date.parse(s)
  22. rescue ArgumentError, Date::Error
  23. nil
  24. end
  25. COUNCIL_MAP = {
  26. "BREAK_ODAY" => "da_break_oday",
  27. "BRIGHTON" => "da_brighton",
  28. "BURNIE" => "da_burnie",
  29. "CENTRAL_COAST" => "da_centralcoast",
  30. "CENTRAL_HIGHLANDS" => "da_centralhighlands",
  31. "CIRCULAR_HEAD" => "da_circularhead",
  32. "CLARENCE" => "da_clarence",
  33. "DERWENT_VALLEY" => "da_derwentvalley",
  34. "DEVONPORT" => "da_devonportcity",
  35. "DORSET" => "da_dorset",
  36. "FLINDERS" => "da_flinders_council",
  37. "GEORGE_TOWN" => "da_georgetown",
  38. "GLAMORGAN-SPRING_BAY" => "da_glamorgan",
  39. "GLENORCHY" => "da_glenorchy",
  40. "HOBART" => "da_hobartcity",
  41. "HUON_VALLEY" => "da_huonvalley",
  42. "KENTISH" => "da_kentish",
  43. "KINGBOROUGH" => "da_kingborough",
  44. "KING_ISLAND" => "da_kingisland",
  45. "LATROBE" => "da_latrobe",
  46. "LAUNCESTON" => "da_launceston",
  47. "MEANDER_VALLEY" => "da_meandervalley",
  48. "NORTHERN_MIDLANDS" => "da_northernmidlands",
  49. "SORELL" => "da_sorell",
  50. "SOUTHERN_MIDLANDS" => "da_southernmidlands",
  51. "TASMAN" => "da_tasman",
  52. "WARATAH-WYNYARD" => "da_waratah_wynyard",
  53. "WEST_COAST" => "da_westcoast",
  54. "WEST_TAMAR" => "da_westtamar"
  55. }
  56. PLANBUILD_CODE_MAP = {
  57. "BOD" => "BREAK_ODAY",
  58. "BRI" => "BRIGHTON",
  59. "BUR" => "BURNIE",
  60. "CCO" => "CENTRAL_COAST",
  61. "CHD" => "CIRCULAR_HEAD",
  62. "CEH" => "CENTRAL_HIGHLANDS",
  63. "CLA" => "CLARENCE",
  64. "DER" => "DERWENT_VALLEY",
  65. "DEV" => "DEVONPORT",
  66. "DOR" => "DORSET",
  67. "FLI" => "FLINDERS",
  68. "GEO" => "GEORGE_TOWN",
  69. "GLC" => "GLENORCHY",
  70. "GSP" => "GLAMORGAN-SPRING_BAY",
  71. "HOB" => "HOBART",
  72. "HUO" => "HUON_VALLEY",
  73. "KEN" => "KENTISH",
  74. "KIN" => "KINGBOROUGH",
  75. "KIS" => "KING_ISLAND",
  76. "LAT" => "LATROBE",
  77. "LAU" => "LAUNCESTON",
  78. "MEA" => "MEANDER_VALLEY",
  79. "NMI" => "NORTHERN_MIDLANDS",
  80. "SOR" => "SORELL",
  81. "SOM" => "SOUTHERN_MIDLANDS",
  82. "TAS" => "TASMAN",
  83. "WAR" => "WARATAH-WYNYARD",
  84. "WCO" => "WEST_COAST",
  85. "WTA" => "WEST_TAMAR"
  86. }
  87. # returns the table name
  88. def self.ref_to_table(ref)
  89. code = ref.split("-")[1].to_s.upcase
  90. council_key = PLANBUILD_CODE_MAP[code]
  91. raise "Unknown council code: #{code}" unless council_key
  92. # make sure the key exists in COUNCIL_MAP
  93. table = COUNCIL_MAP[council_key]
  94. raise "No table mapping for council: #{council_key}" unless table
  95. table
  96. end
  97. # returns the folder name for PDFs
  98. def self.ref_to_folder(ref)
  99. code = ref.split("-")[1].to_s.upcase
  100. council_name = PLANBUILD_CODE_MAP[code]
  101. raise "Unknown council code: #{code}" unless council_name
  102. council_name # use full council name for folder
  103. end
  104. end